与本地目录使用的gettext不起作用

问题描述:

following an official i8n example本地目录库的gettext有:与本地目录使用的gettext不起作用

#include <libintl.h> 
#include <iostream> 
#define _(str) gettext(str) 

int 
main() 
{ 
    setlocale (LC_ALL, "pt_PT"); 
    bindtextdomain ("messages", "/home/pasousa/temp/i18n_test/lang/"); 
    textdomain ("messages"); 

    std::cout << _("Hello world!") << std::endl; 
    return 0; 
} 

葡萄牙语翻译字符串包含在./lang/pt_PT/LC_MESSAGES/messages.mo。但是,如果使用这些代码我没有得到Hello World消息翻译。

+0

欢迎来到*!一定要包括所有的信息,因此这将意味着你的'messages.mo'文件*的内容(应修剪成刚的“Hello World!”为例子的缘故)*。另外,如果你检查,看看是什么调用的返回结果也可能是有益的。我注意到,'bindtextdomain'可能会返回一个指向'字符*'一个目录或NULL - 例如,和套如果出现问题,请输入“errno”。 – HostileFork

你需要确保你设置的语言环境是有效的 - 如果你不setlocale设定一个有效的区域设置,然后glibc就假设你在CPOSIX语言环境中运行,而忽略了指定的值在bindtextdomain并简单地返回未本地化值。这似乎是gettext的一些有些无法证明的行为。

你可能希望将区域设置为pt_PT.UTF-8而不是简单地pt_PT

在我的情况,当我尝试,我得到:

char *from_setlocale = setlocale(LC_ALL, "pt_PT"); 
char *checked_setlocale = setlocale(LC_ALL, NULL); 
std::cout << (from_setlocale ? from_setlocale : "NULL") << std::endl << (checked_setlocale ? checked_setlocale : "NULL") << std::endl; 

但那是因为我没有安装葡萄牙人区域做。当我设置的是我已经安装了一个区域(en_IE.UTF-8)然后我得到了一些有用的输出:

char *from_setlocale = setlocale(LC_ALL, "en_IE.UTF-8"); 
char *checked_setlocale = setlocale(LC_ALL, NULL); 
std::cout << (from_setlocale ? from_setlocale : "NULL") << std::endl << (checked_setlocale ? checked_setlocale : "NULL") << std::endl; 

,另外,我从我已生成的消息文件的本地化值。