简体   繁体   English

使用setlocale()和gettext()在Ubuntu上测试语言环境

[英]Testing locale on Ubuntu using setlocale() and gettext()

I am trying to write a program that prints an English message in French based on locale. 我正在尝试编写一个根据语言环境以法语打印英文消息的程序。

I wrote the following sample code: 我编写了以下示例代码:

#include <stdio.h>
#include <libintl.h>
#include <locale.h>

int main()
{
    char *str = setlocale(LC_ALL, "fr_FR.UTF-8");
    if(str == NULL)
        printf("setlocale() cannot be honored");
    else
        printf("setlocale() return = %s \n", str);

    bindtextdomain("helloworld","/usr/share/locale");
    textdomain("helloworld");
    printf("%s", gettext("Hello!\n"));

    return 0; 
}

I checked the file /usr/share/i18n/SUPPORTED and can see fr is there: 我检查了文件/usr/share/i18n/SUPPORTED ,可以看到fr在那里:

fr_FR.UTF-8 UTF-8
fr_FR ISO-8859-1
fr_FR@euro ISO-8859-15

I also tried running sudo locale-gen fr-FR.UTF-8 . 我也尝试运行sudo locale-gen fr-FR.UTF-8 When I run locale on the command line, I see: 在命令行上运行locale时,我看到:

LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

When I run my program, setlocale() returns NULL . 当我运行程序时, setlocale()返回NULL Can anyone tell what I'm missing ? 谁能告诉我我所缺少的吗?

Edit 编辑

When trying to for zh-CN , I also checked the contents of directory /usr/share/locale/zh-CN/LC_MESSAGES . 尝试使用zh-CN ,我还检查了目录/usr/share/locale/zh-CN/LC_MESSAGES The following listing appears: 出现以下清单:

apt.mo              gnome-panel-3.0.mo  iso_639.mo
debconf.mo          iso_15924.mo        language-selector.mo
dpkg.mo             iso_3166_2.mo       libapt-inst1.4.mo
e2fsprogs.mo        iso_3166.mo         libapt-pkg4.12.mo
example-content.mo  iso_4217.mo         update-notifier.mo
ghex-3.0.mo         iso_639_3.mo        xdg-user-dirs.mo

If locale -a doesn't show fr_FR locale, you do not have one installed on your system. 如果locale -a没有显示fr_FR语言环境,则您的系统上没有安装一个语言环境。

The best/easiest way to do it may slightly depends on the distro. 最好/最简单的方法可能会稍微取决于发行版。 This topic answers your question: https://askubuntu.com/questions/76013/how-do-i-add-locale-to-ubuntu-server 本主题回答您的问题: https : //askubuntu.com/questions/76013/how-do-i-add-locale-to-ubuntu-server

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM