c++ - 如何在运行时给出 "hello world"?

标签 c++ internationalization g++ locale gettext

我正在尝试用本地语言打印“hello world”程序并且我成功执行了...但是现在我正在尝试通过输入 hello world @run time 来做同样的事情...这可能吗?如何? 我遵循了以下代码示例...

cat >hellogt.cxx <<EOF
// hellogt.cxx
#include <libintl.h>
#include <locale.h>
#include <iostream>
#include <cstdlib>
int main (){
    char* cwd = getenv("PWD");
    std::cout << "getenv(PWD): " << (cwd?cwd:"NULL") << std::endl;
    char* l = getenv("LANG");
    std::cout << "getenv(LANG): " << (l?l:"NULL") << std::endl;
    char* s = setlocale(LC_ALL, "");
    std::cout << "setlocale(): " << (s?s:"NULL") << std::endl;
    std::cout << "bindtextdomain(): " << bindtextdomain("hellogt", cwd) << std::endl;
    std::cout << "textdomain(): " << textdomain( "hellogt") << std::endl;
    std::cout << gettext("hello, world!") << std::endl;
}
EOF
g++ -ohellogt hellogt.cxx
xgettext -d hellogt -o hellogt.pot hellogt.cxx
msginit --no-translator -l es_MX -o hellogt_spanish.po -i hellogt.pot
sed --in-place hellogt_spanish.po --expression='/#: /,$ s/""/"hola mundo"/'
sed --in-place hellogt_spanish.po --expression='s/PACKAGE VERSION/hellogt 1.0/'
mkdir -p ./es_MX/LC_MESSAGES
msgfmt -c -v -o ./es_MX/LC_MESSAGES/hellogt.mo hellogt_spanish.po
export LANG=es_MX
ls -l $PWD/es_MX/LC_MESSAGES/hellogt.mo
./hellogt
strace -e trace=open ./hellogt

最佳答案

gettext 是一个运行时函数。参见 here .

也许试试这个:

int main(int argc, char** argv) {
   if(argc > 1)
       printf("%s\n", gettext(argv[1]));
}

关于c++ - 如何在运行时给出 "hello world"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19490347/

相关文章:

c++ - 如何通过命令行参数分隔botan加密和解密操作?

delphi - Delphi 2010 IDE 的默认控件字体在国际化时如何工作?

java - Spring Boot中的MessageSource是否允许使用同一语言的多个属性文件?

c++ - 忽略函数默认参数

C++/CLI (.NET) 相当于 native C++ 将结构写入网络

c++ 双向简单运算符重载

php - .po 到 .mo 在 php 中的转换?

c++ - 如何将 g++ 编译器的版本从 4.1.2 更改为 4.5?

g++ - 如何通过几行代码禁用 g++ 中的所有警告

c++ - 与 C stdio 库相比,C++ iostream 必须提供什么?