我希望有一种易于使用的方式来编写如下代码:
#include <iostream>
int main (){
std::cout << "hello, world!\n";
}
但是支持 i18n。这是一个使用 gettext()
的例子:
#include <libintl.h>
#include <iostream>
int main (){
std::cout << gettext("hello, world!\n");
}
这可以由 xgettext 处理以生成可以使用的消息目录文件 由翻译人员创建各种版本。这些额外的文件可以在目标上处理 允许用户以首选语言进行交互的系统。
我想改写这样的代码:
#include <i18n-iostream>
int main (){
i18n::cout << "hello, world!\n";
}
在构建时,引用的字符串将由 xgettext 之类的程序检查以生成
基本消息目录文件。 <<
带参数的运算符 i18n::cout
会带一个字符串
文字作为查找要从消息目录中使用的运行时文本的键。
它存在于某处吗?
最佳答案
At build time the quoted strings would be examined by a program like xgettext to produce the base message catalog file. << operator with argument i18n::cout would take a string literal as the key to lookup the run-time text to use from a message catalog.
您尝试像单个实例一样转换字符串,但事实并非如此/
重点是,您不想要这样的东西。想想:
if(n=1)
i18n::cout << "I need one apple"
else
i18n::cout << "I need " << n << " apples" ;
那么为什么这是行不通的,因为“n=1”或“n!=1”只适用于英语,许多其他语言有不止一种复数形式,而且它需要翻译“我需要 X 苹果”作为一个实例。
我建议你只学习处理gettext,它很简单而且功能强大,很多人都想过。
还有一点,你通常不调用gettext而是调用
#include <libintl.h>
#include <iostream>
#define _(x) gettext(x)
int main (){
std::cout << _("hello, world!\n");
}
这使得代码更简洁,使用“_”作为 gettext 别名也是一个相当“标准”的特性。
在尝试制作“更好”的 API 之前,只需学习如何使用它。顺便提一下,gettext API 是许多语言的事实上的标准,不仅仅是 C。
关于c++ - 如何使用 C++ 的特性来更干净地使用 i18n 库,而不是直接使用 C 的 gettext(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/977146/