![]() It is possible to place some icons there (language.png) which is used as icon in the menu (e.g. In this example, all text files are located in the sub folder "languages". The advantage of this solution is, you can deliver any translation later on, and it will just work after application restart. The language menu is created dynamically during application start, depending on the existing translation files. QString m_langPath // Path of language files. QString m_currLang // contains the currently loaded language ![]() QTranslator m_translatorQt // contains the translations for qt QTranslator m_translator // contains the translations for this application Ui::MainWindow ui // ui definition from designer creates the language menu dynamically from the content of m_langPath Void loadLanguage(const QString& rLanguage) loads a language by the given language shortcur (e.g. Void slotLanguageChanged(QAction* action) this slot is called by the language menu actions ![]() this event is called, when a new translator is loaded or the system language is changed The current language is stored to suppress events, if the user tries to load the same language twice. For each translation file, that is needed, a QTranslator instance is created (in our example 2, on for the application texts, one for qt). In the class MainWindow, the virtual method changeEvent(QEvent*) is overwritten. The translated text files of the application (TranslationExample_*.qm, where * could be de, en, etc.).For each installed language, there is an (optional) image of the size 16x16 pixel with a flag (e.g.If the user opened the language menu, there is a selection of languages, which is created on startup of the application, dependent on the existing language files.įile system structure of the application: In this example, we create a main window with a menu Language and some widgets. 2 File system structure of the application:Ĭreate a standard application, e.g.The rules (byte code) are loaded by QTranslator at runtime and executed (like in a virtual machine) to determine which plural form to pick up. How does QTranslator determine which plural form to pick at runtime? The numerus rules are actually embedded inside the. All the plural form strings are embedded into the final. When n is encountered in a string, linguist offers the translator to provide various plural forms (depending on the translation language). To understand this feature, please read Plural rules. Linguist supports plural forms and translates correctly based on the language. Besides, looking up the original string given the translatable string is a very expensive operation and there is no API to reverse-translate. qs file doesn't actually contain source strings (it gets mangled since comment is appended to it). The context+source_string+comment is (and has to be) stored in the same encoding as CODECFORTR (otherwise lookups won't work). To summarize, Qt translates together to look up the translated text. For example: 0 files removed, 1 file removed, 2 files removed (notice plurality of 'file' based on the number) A number for plural support - In some languages, the wording changes depending on a number. Comments are arbitrary strings and are optional.Ĥ. Think of this as a micro-context or a sub-context. It could also be that "OK" has different translations within the same class methods itself. lupdate uses the name of the class (along with the namespace) in which the tr() is used as the context.ģ. For example, "OK" in a InformationDialog is probably different from the "OK" in FileRemoveDialog. The string "OK" could be translated to different words in different contexts. A source string that needs to be translated. In Qt, for a string to be translated we need the following bits of information:ġ. Let's go through the internals of each of the above steps. Result : All tr() invocations of step1 automatically get translations made in step 3. QCoreApplication::installTranslator is used to install the QTranslator.Ħ. qm file(s) depending on the locale/settings.ĥ. Application uses QTranslator is used to load. qm format that can be loaded into an application at runtime.Ĥ. ts which contains translated strings and converts them into a binary. ts file contains both the string to be translated and the translation.ģ. A translator provides translations by opening the. ts file contains only strings that are meant to be translated.ģ. lupdate - A tool that scans the source files for tr() and places the strings in a. The strings meant for translation are marked using tr() in source code.Ģ. The Qt Internationalization manual provides a comprehensive overview of the i18n support in Qt.
0 Comments
Leave a Reply. |