在glib中打印utf8 [英] printing utf8 in glib

查看:171
本文介绍了在glib中打印utf8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不能通过glib函数打印utf8符号?

Why utf8 symbols cannot be printed via glib functions?

源代码:

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

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

像这样构建它:

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

您可能会看到glib无法打印utf8,而fprintf可以:

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

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

推荐答案

fprint函数假定您使用它们打印的每个字符串都经过正确编码,以匹配终端的当前编码. g_print()并不假定那样,并且会在认为必要时转换编码.当然,如果编码以前实际上是正确的,那么这是一个坏主意,因为这很可能会破坏编码.终端的语言环境设置是什么?

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?

您可以在大多数系统上通过环境变量设置正确的语言环境,也可以使用setlocale函数以编程方式进行设置.区域设置名称取决于系统(不是POSIX标准的一部分),但是在大多数系统上,以下方法将起作用:

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");

除了LC_ALL之外,您还只能设置某些操作的语言环境(例如,"en_US"将导致英语数字和日期格式格式化,但是您可能不希望这样格式化数字/日期).要引用setlocale手册页:

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设置整个语言环境 一般而言.

LC_ALL Set the entire locale generically.

LC_COLLATE设置字符串的语言环境 整理例程.这个控制 字母顺序 strcoll()和strxfrm().

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

LC_CTYPE设置语言环境 ctype(3)和multibyte(3)函数. 这控制了对 大写和小写字母或非字母 字符等等.

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设置消息的语言环境 目录,请参见catopen(3)函数.

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

LC_MONETARY设置语言环境 格式化货币价值;这 影响localeconv()函数.

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

LC_NUMERIC设置语言环境 格式化数字.这控制了 小数点格式 函数中浮点数的输入和输出 例如printf()和scanf() 以及localeconv()返回的值.

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设置语言环境 使用 strftime()函数.

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

在所有系统上始终唯一可用的两个语言环境值是"C","POSIX"和".

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

默认情况下,仅定义三个语言环境:空字符串"(表示本机环境) 以及"C"和"POSIX"语言环境(表示C语言环境). NULL的语言环境参数 使setlocale()返回当前语言环境.默认情况下,C程序在"C"语言环境中启动.这 库中设置语言环境的唯一函数是setlocale();语言环境永远不会改变 其他常规程序的效果.

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.

这篇关于在glib中打印utf8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆