How to resolve accentuation problems when using the setlocale function?

Asked

Viewed 12,681 times

5

I am trying to define the language of the program to Portuguese, using the setlocale(LC_ALL,"Portuguese"), however, much of the program was done on Ubuntu. When the other programmer sent me the files and when I will compile, using mingw32-make to use Makefile, I have problems with the accent, but when I compile using Ubuntu I do not have this problem.

Being the library locale.h standard C language, this setlocale should work on both Windows and Ubuntu. Anyone has any idea what the problem is?

2 answers

4

This has to do with file encoding and output encoding of your program.

The problem won’t just go away alone - you need to understand Unicode. I suggest you read this article - the most classic piece on Nicode I’ve seen, in circulation since 2003.

This happens because the "locale" of the program depends on an internal encoding of the accent (which the character code "ç"? In the terminal of Ubuntu will be one, in the terminal (cmd) of Windows will be another, and in a graphical window in the same Windows will be a third value) - understand Unicode is to know that you need:

  1. Normalize all your input data, from the internet, the database, user input, external sensors, etc... for a single itnerna encoding that is used by your text-based libraries (UTF-16 and UTF32 are usually used - but if I’m not mistaken the gobject system uses utf-8 even)
  2. Perform all your processing: comparisons, interpolation, classification, etc...with this encoding
  3. Encode back the text to the encoding of the target media when saving - and nothing obliges it to be the same encoding for each output - you can output to the Windows terminal with one encoding, to the database with another, and still generate an HTML output with another. (But the most recommended is to configure everything that communicates with your program to use utf-8 - makes things much easier)

2

In function setLocale put as second parameter the string "" (double quotes), this may solve because that way the location is configured according to the OS location.

example:

setLocale(LC_ALL,"");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.