In our apps, language change is very common functionality. Before jumping into that topic, let us see the basics QLocale and QTransaltor.
QLocale contains the language and country details. QLocale() will construct a locale object with the system language and locale. If you want to construct a locale of you interest, then initialize like this
The output for the above code is en_US.
To add a new locale support to your application go to bardescreptor.xml --> Localization tab. You can find the default language already defined. To add your support, click on Add and select the language from the list. This will be like
I'm selecting Hindi from the list. Then we can specify the title, app icon, splash screen in the right pane when the locale is changed to Hindi. The specified values will be automatically reflected once the system language is changed to Hindi. Reference picture below.
Once you build the application, under the translations folder of the project, projectname_hi.ts will be automatically generated. See the red circle in the above picture. We can define, the hindi translation for a specific string in the generated files.
The question is how will we load these .ts files into our application. This leads us to QTranslator.
QTranslator is capable of loading the language string file(.ts format) for the specified locale with simple steps.
In the above code, we are instantiating hindiLocale, initializing translator object, getting the name of hindi locale, load it into translator, install the translator to the application.
If the specified locale is not found, application will install the default locale.
QLocale contains the language and country details. QLocale() will construct a locale object with the system language and locale. If you want to construct a locale of you interest, then initialize like this
QLocale englishLocale = QLocale(QLocale::English, QLocale::UnitedStates); std::cout << "Locale is : " << englishLocale.name().toStdString();
The output for the above code is en_US.
To add a new locale support to your application go to bardescreptor.xml --> Localization tab. You can find the default language already defined. To add your support, click on Add and select the language from the list. This will be like
Select language |
I'm selecting Hindi from the list. Then we can specify the title, app icon, splash screen in the right pane when the locale is changed to Hindi. The specified values will be automatically reflected once the system language is changed to Hindi. Reference picture below.
Once you build the application, under the translations folder of the project, projectname_hi.ts will be automatically generated. See the red circle in the above picture. We can define, the hindi translation for a specific string in the generated files.
The question is how will we load these .ts files into our application. This leads us to QTranslator.
QTranslator is capable of loading the language string file(.ts format) for the specified locale with simple steps.
- Create a locale object
- Load the translator with that locale
- Install the translator into the application.
QLocale hindiLocale = QLocale(QLocale::Hindi, QLocale::India); QTranslator translator; QString locale_string = hindiLocale.name(); QString filename = QString("FileHandling_%1").arg(locale_string); if (translator.load(filename, "app/native/qm")) { Application::instance()->installTranslator(&translator); }
In the above code, we are instantiating hindiLocale, initializing translator object, getting the name of hindi locale, load it into translator, install the translator to the application.
If the specified locale is not found, application will install the default locale.
No comments:
Post a Comment