Warning, /sdk/cutehmi/extensions/CuteHMI/Examples/I18N.0/README.md is written in an unsupported language. File is not indexed.
0001 # Internationalization Example 0002 0003 Internationalization example. This example shows how to support multiple languages and how to use CuteHMI.Internationalizer 0004 singleton. 0005 0006 To run the example use [cutehmi.view.4](../../../../tools/cutehmi.view.4/) tool. 0007 0008 ``` 0009 cutehmi.view.4 CuteHMI.Examples.I18N.0 0010 ``` 0011 0012 Option `--lang` can be used to specify the initial language. 0013 0014 ``` 0015 cutehmi.view.4 CuteHMI.Examples.I18N.0 --lang eo 0016 ``` 0017 0018 0019 ## Preparation 0020 0021 The necessary step to provide a multilingual user interface in Qt application is to prepare translation files. These files can be 0022 in fact conveniently prepared with two toolchains: gettext + KDE internationalization framework or Qt internationalization tools. 0023 0024 The gettext + KDE approach is described [here](https://techbase.kde.org/Development/Tutorials/Localization/i18n_Build_Systems). 0025 Don't forget to pass `--qt` option to `msgfmt` program, when using this method. 0026 0027 This examples uses Qt-based approach. Standard internationalization process with tools shipped by Qt is described 0028 [here](https://doc.qt.io/qt/internationalization.html). 0029 0030 To signal that extension supports internationalization one should set `i18n` property to `true` in `project.qbs` file. 0031 0032 ``` 0033 i18n: true 0034 ``` 0035 0036 This is important, because CuteHMI.Internationalizer relies on product metadata, when loading translations of dependent products. 0037 0038 ## Translation files 0039 0040 The example comes with two `.ts` files, which reside in `i18n` subdirectory. They provide translations for US English and Esperanto. 0041 In order to generate such files one has to call `lupdate` program. Once `.ts` files are added to the project, `Qt.core` Qbs module 0042 calls `lrelease` program to generate `.qm` files. These are installed to `translations` directory, which is formally defined by 0043 `translationsInstallSubdir` property of `cutehmi.dirs` Qbs module. 0044 0045 ### Generating translation files from Qbs 0046 0047 To generate `.ts` files from Qbs, `cutehmi.i18n` Qbs module can be used. Unfortunately, because `Qt.core` Qbs module calls 0048 `lrelease` on every `*.ts` file in the product and `lrelease` triggers error if these files are empty, you can't simply add empty 0049 `.ts` files to the project. Instead, one has to use `additionalTranslations` property to generate new translation files. After they 0050 are generated, they should be added to the project files. Again, this property should be used only when generating new 0051 translations - do not use it for `.ts` files, which are already in the project files. 0052 0053 ``` 0054 Depends { name: "cutehmi.i18n" } 0055 cutehmi.i18n.update: true 0056 cutehmi.i18n.additionalTranslations: [ 0057 "i18n/cutehmi-examples-i18n-0_en_US.ts", 0058 "i18n/cutehmi-examples-i18n-0_eo.ts", 0059 ] 0060 ``` 0061 0062 Properties can be also passed to Qbs from the command line, resulting in a bit lenghty command. Here's a command, which introduces 0063 Russian translation: 0064 0065 ``` 0066 qbs build project.buildBinaries:false modules.cutehmi.qmltypes.additionalProductTypes:[] 0067 products.CuteHMI.Examples.I18N.0.cutehmi.i18n.additionalTranslations:[cutehmi-examples-i18n-0_ru.ts] 0068 products.CuteHMI.Examples.I18N.0.cutehmi.i18n.update:true -f CuteHMI.qbs 0069 ``` 0070 0071 Argument `project.buildBinaries:false` turns off generation of binaries and `modules.cutehmi.qmltypes.additionalProductTypes:[]` 0072 prevents qmlplugindump being called on unbuilt products. 0073 0074 After `.ts` files are added to the project files, `cutehmi.i18n` Qbs module becomes unnecessary. It may significantly slow down 0075 the compilation by repeatedly making costly `lupdate` calls. It is reasonable to comment out its entries and only occasionally run 0076 Qbs with `cutehmi.i18n.update` set to `true` to update the translations. 0077 0078 Of course, although this may change in the future, actual translations still have to be provided by a human. One can use Qt Linguist program to edit `.ts` files. 0079 0080 ### File names 0081 0082 Names of translation files consist of stem and suffix. Stem is constructed by lowercasing product name and replacing dots with 0083 hyphens. In case of `CuteHMI.Examples.I18N.0` extension, stem is `cutehmi-examples-i18n-0`. The suffix part denotes a language in 0084 a format described in Qt documentation on QLocale::QLocale(const QString &). Here we have `en_US.ts` for US English and `eo.ts` 0085 for Esperanto. One can see that suffix has been separated from the stem by underscore character (`_`), but in fact language can 0086 be specified in many different ways. Supported translation suffixes and translation directory locations are listed in the 0087 documentation of CuteHMI.Internationalizer.loadTranslation() function. 0088 0089 ## The actual example 0090 0091 After all the prerequisites has been satisfied, using CuteHMI.Internationalizer from QML is pretty straightforward. 0092 0093 At first, we provide some translatable string. 0094 0095 \snippet View.qml Translation string 0096 0097 The combo box controls the language choice, so we set `currentIndex` to match the initial language. 0098 0099 \snippet View.qml Setting currentIndex 0100 0101 When user chooses another language, we simply reflect his choice by updating CuteHMI.Internationalizer.uiLanguage property. 0102 0103 \snippet View.qml Language activated 0104 0105 \example View.qml 0106 Here is a complete listing of the example source file.