QML language internationalization

Qt, QML translation, language internationalization process and preparation of the operation code probably is written to be translated string with translation logo, ts generated by the configuration file required project file, modify the ts files, publish qm file generation needs and then load the settings file translation needs in the CPP. Specific instructions below

First, add the project file

TRANSLATIONS += \
internationalization/os_language_English.ts \
    internationalization/os_language_German.ts \
internationalization/os_language_ChineseTraditional.ts \
    Internationalization / os_language_ChineseSimpilifed.ts \

// represents the birth of ts when updating the translation file, note the path


Second, add in the project file
lupdate_only {
    SOURCES + = \
*.qml\
. Setting / QML * \
}

// add the need to translate documents indicate

Third, after the project file configuration, run lupdate, click (Tools -> External -> Qt Linguist -> Update translation (lupdate));
this time from application program, the project file (lupdate_only) translation of documents added, after extracting all qsTr () identifies over string (qsTr ( "hello")) , generated suffix .ts file, the file name and path of the project file configuration.

The use of open Qt Linguist .ts translation of documents to be translated, and then generate the corresponding release qm file (File -> Publish) at Linguist; or use QT (Tools -> External -> Qt Linguist -> Update translation (lrelease) ) released qm generate the corresponding file.

Note debugging running on the computer, do not need to add the qrc resource file is also possible, if not, we need to add the file to the qrc qm resource file. (I can not seem to say, the focus is to allow the program to find the location qm)


Five, four steps above only provides the basis for the translation of the above we have produced a document translation needs qm, now we need to load the translation of documents we need, at this time we need to use a language class QTranslator Qt, the need to include headers #include <QTranslator>.

QTranslator translator;
ret = translator.load(("internationalization/os_language_English.qm"), ".");

if(ret)qApp->installTranslator(&translator); 

If unsuccessful, noting that the above three steps in before the window is created, after app creation.


Sixth, although the above operations to meet our load a different translation, but noted that was loaded before the window is created, and this condition we need to be switched after the window is loaded in multiple languages do not match. So we need to be refreshed after loading the display language, according to a trigger signal qt refresh mechanism, we need to add a variable of type string behind the translated strings, and the value of this variable read function returns a null character, so it will not affect our text. But the way this variable is defined and differentiated before.

public: Q_PROPERTY (QString emptyString the READ getEmptyString the NOTIFY languageChanged)
QString getEmptyString () {// languageChanged triggered, the refresh
    return "";
}
QML wording: text: qsTr ( "oven_status: ") + myovenbar.emptyString;

idea: languageChanged need to be defined, the trigger signal is required after switching language, refresh the screen display.
We need to learn more about macros Q_PROPERTY: http: //blog.csdn.net/dreamsongo/article/details/77585470



seven summary
1.Qt, translation principle QML is actually corresponding replacement operation according to our fifth above the set point selected file translation. All translation is done according to each file. This means that when the same word translated in a.qml, the same does not display a word translation on b.qml, of course, if you re-execute the translation may publish in accordance with qt will automatically help you on b.qml a.qml the contents of the given translation.

2. The above mentioned have updated translation file is generated based on the string ts have not been qsTr () identity, but this is not the only way to identify, ListElement Listview in qml ListModel data pattern can be used in QT_TR_NOOP () to identify .

 ListElement {menuName: QT_TR_NOOP("NONE");}

May be used qstr () in the QML, qsTranslate (), QT_TR_NOOP () function and the like QT_TRANSLATE_NOOP strings marked as translated (not verified 2,4)


3, yet when it comes to the point is, we do not want to use the translation file is loaded, we can use
 qApp-> removeTranslator (translator); to remove out.

4, there is a noteworthy that, in line help for QTranslator :: load this sentence
The data is not copied. The caller must be able to guarantee that data will not be deleted or modifiled
  this passage clear instructions a, QTranslator after load, and not to copy the data in a file qm, but when you need to query string. If qm been deleted or modified during this period, the program are influential. Open to expansion, QTranslator must be guaranteed to remain in effect if local variables defined in the function, the function is automatically released after the end of fall, then the translation work can not be carried out normally. It is proposed that the definition of member variables in QTranslator * app_translator in private; to ensure the correctness of the translation of the entire work (this view has not been verified)


Linguist Open ts file renderings:







Guess you like

Origin blog.csdn.net/DreamSonGO/article/details/77586631