gpt4 book ai didi

c++ - 将 gettext 与本地目录一起使用无效

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:46:30 24 4
gpt4 key购买 nike

我是following an official i8n example对于带有 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 消息。

最佳答案

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

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

关于c++ - 将 gettext 与本地目录一起使用无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37256305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com