在本地目录中使用gettext没有任何效果
Using gettext with local catalog has no effect
我下面是一个带有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将假设您在C
或POSIX
区域设置中运行,并忽略在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++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如何在不产生任何垃圾的情况下获得C中的像素
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- 等<thing>效于char32_t
- C++映射有2个键,这样任何1个键都可以用来获取值
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- RtlCaptureStackBackTrace未捕获任何帧
- 链表c++插入,所有情况都已检查,但没有任何工作
- C++模板函数,用于比较任何无符号整数和有符号整数
- Arduino millis() - millis() 怎么能等于 0 以外的任何东西?
- 尝试摆脱任何堆内存分配
- 是否有任何C++功能可以对地图进行排序?
- 打印时有二叉树问题.用户输入不打印任何内容
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 在C++行尾写一个分号或多个分号是否会改变任何内容
- C++类型特征,以查看是否可以<uint32_t>对类型"K"的任何变量调用"static_cast(k)"
- C++ 将 CIN 值存储到任何类型的数组中
- 为什么瓦尔格林德在不释放恶意内存后没有报告任何问题?
- 在本地目录中使用gettext没有任何效果