在本地目录中使用gettext没有任何效果

Using gettext with local catalog has no effect

本文关键字:gettext 任何效      更新时间:2023-10-16

我下面是一个带有gettext的本地目录存储库的官方i8n示例:

#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消息。

您需要确保您设置的区域设置是有效的-如果您没有在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;

此外,我还从生成的消息文件中获得了一个本地化的值。