Qt 之国际化翻译--浅析 英文--->中文

QTranslator  搬运一下FAQ:

The QTranslator class provides internationalization support for text output.

An object of this class contains a set of translations from a source language to a target language. QTranslator provides functions to look up translations in a translation file. Translation files are created using Qt Linguist.

The most common use of QTranslator is to: load a translation file, install it using QCoreApplication::installTranslator(), and use it via QObject::tr(). Here's an example main() function using the QTranslator:


  int main(int argc, char *argv[])
  {
      QApplication app(argc, argv);

      QTranslator translator;
      // look up e.g. :/translations/myapp_de.qm
      if (translator.load(QLocale(), QLatin1String("myapp"), QLatin1String("_"), QLatin1String(":/translations")))
          app.installTranslator(&translator);

      QPushButton hello(QCoreApplication::translate("main", "Hello world!"));
      hello.resize(100, 30);

      hello.show();
      return app.exec();
  }

Note that the translator must be created before the application's widgets.

Most applications will never need to do anything else with this class. The other functions provided by this class are useful for applications that work on translator files.

详细说明到手册上都有,简述一下使用很简单的.

简述英文---->中文【其他语言同理】

1、首先生成.ts文件右键   VS工程【注意字符串需要用tr("")

 2、在解决方案里找到 Translation Files,生成的ts文件在这里,双击会用语言家(Qt Linguist)打开

 保存之后,右键.ts文件 lupdate更新

                    右键.ts文件lrelease编译

然后运行工程,即可看到翻译。

原文地址:https://www.cnblogs.com/liuruoqian/p/12974313.html