tags:

views:

129

answers:

4

Hi.

Why utf8 symbols cannot be printed via glib functions?

Source code:

#include "glib.h"
#include <stdio.h>

int main() {
    g_print("марко\n");
    fprintf(stdout, "марко\n");
}

Build it like this:

gcc main.c -o main $(pkg-config glib-2.0 --cflags --libs)

You could see that glib can't print utf8 and fprintf can:

[marko@marko-work utf8test]$ ./main 
?????
марко
+1  A: 

The string passed from g_print() to glibc is not necessarily in UTF-8 encoding since g_print() does character set conversion to the charset specified by the locale.

Luca Matteis
+3  A: 

fprint functions assume that every string you print with them is correctly encoded to match the current encoding of your terminal. g_print() does not assume that and will convert the encoding if it thinks that is necessary; of course this is a bad idea, if the encoding was actually correct before, since that will most likely destroy the encoding. What is the locale setting of your terminal?

You can either set the correct locale by environment variables on most systems or you can do it programatically using the setlocale function. The locale names are system dependent (not part of the POSIX standard), but on most systems the following will work:

#include <locale.h>

:

setlocale(LC_ALL, "en_US.utf8");

Instead of LC_ALL you can also only set the locale for certain operations (e.g. "en_US" will cause English number and date formatting, but maybe you don't want numbers/dates to be formatted that way). To quote from the setlocale man page:

LC_ALL Set the entire locale generically.

LC_COLLATE Set a locale for string collation routines. This controls alphabetic ordering in strcoll() and strxfrm().

LC_CTYPE Set a locale for the ctype(3) and multibyte(3) functions. This controls recognition of upper and lower case, alphabetic or non-alphabetic characters, and so on.

LC_MESSAGES Set a locale for message catalogs, see catopen(3) function.

LC_MONETARY Set a locale for formatting monetary values; this affects the localeconv() function.

LC_NUMERIC Set a locale for formatting numbers. This controls the formatting of decimal points in input and output of floating point numbers in functions such as printf() and scanf(), as well as values returned by localeconv().

LC_TIME Set a locale for formatting dates and times using the strftime() function.

The only two locale values that are always available on all systems are "C", "POSIX" and "".

Only three locales are defined by default: the empty string "" (which denotes the native environment) and the "C" and "POSIX" locales (which denote the C-language environment). A locale argument of NULL causes setlocale() to return the current locale. By default, C programs start in the "C" locale. The only function in the library that sets the locale is setlocale(); the locale is never changed as a side effect of some other routine.

Mecki
After setlocale(LC_ALL, "en_US.UTF-8") everything works, but without it and with LANG=en_US.UTF-8 ./main it does not work. Why is this?System default is en_US.UTF-8.
Marko Kevac
Don't you have to export the variable to be visible to the sub-process? Also the variables are named as shown on the man page, try `export LC_ALL=en_US.utf8 maybe it is also enough to set LC_CTYPE for string printing only.
Mecki
You need export if you want to 'save' variable. If you want it just for one application, than it's enough to put it before program name. Anyway, I have done export for LANG, LC_ALL and LC_CTYPE. Nothing. Still don't work. Strange...
Marko Kevac
Use `setlocale(LC_CTYPE, "")` !! the important thing is to always use the `""` string for setlocale, *not* a hardcoded locale..
kaizer.se
If you set it to "" then it won't be necessarily UTF8 and it might again not print correctly, because "" means no locale and no locale means that anything other than ASCII is not defined for strings.
Mecki
A: 

Usually it is not recommended to use anything other than ASCII inside text files. You should use tools like gettext in order to translate words from different languages. If this is out of the question then you should store your string in UTF-8 in your code.

Try printing this one (it's the hexadecimal representation of your string):

char hex_marco[]={0xD0, 0xBC, 0xD0, 0xB0, 0xD1, 0x80, 0xD0, 0xBA, 0xD0, 0xBE, 0};

This works for me in printf (cannot test here with glib):

#include <stdio.h>

char hex_marco[]={0xD0, 0xBC, 0xD0, 0xB0, 0xD1, 0x80, 0xD0, 0xBA, 0xD0, 0xBE, 0};

int main(void)
{
    printf("%s\n",hex_marco);
    return 0;
}

Redirect the output to file and see it as UTF-8.

Hope it helps.

Iulian Şerbănoiu
"marko" in *.c file was just for example. I am not using UTF-8 inside source code. Right answer was already given. Thank you anyway!
Marko Kevac
A: 

You need to initialize the locale's encoding by calling setlocale at your program's start.

setlocale(LC_CTYPE, "")

This is normally carried out for you if you use some initialization function like gtk_init(..) or similar.

kaizer.se