Qt Linguist - set translator for application

2019-08-27 19:32发布

问题:

how can i start creating a ts file that my application will use?

in the options of Linguist i can't create a new one with my own words and translation.... i can only translate a ts file that is allready exist.

回答1:

First, in your project, you need to write texts with tr() if you wanna translate them. For example

QPushButton button(tr("Button"));

In your pro file, add

TRANSLATIONS = chinese.ts\ italian.ts

These files will be created automatically later.

Second, go to Qt Command Prompt. Go to your project directory from command prompt. Then, write

lupdate yourprojectname.pro

and press enter key. It will automatically create "ts" files ("chinese.ts" and "italian.ts") for you.

After that, you need to open Qt Linguist and open your "ts" file. Add translation for your project. When you finish it, release it to get "qm" file.

When you want to translate your application, load the "qm" file.

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

   QTranslator *translator = new QTranslator;
   translator->load("chinese.qm");

   a.installTranslator(translator);
}


回答2:

I think this can be done by the qt command line tool lupdate.

It will scan your code for strings you marked as translatable (tr() macro) and collect them in a *.ts file.

See the documentation for more information.



标签: qt qt4