I have a multilingual Qt application, where I have the following problem
In what way can I achieve this? All the examples I found deal with translating the entire application, but in my case, I need two independent translations (although possibly to the same language).
Would I need to replicate the fall-back mechanisms of QApplication::translate
myself and use QTranslator::translate
directly, or is there a more convenient way?
Some languages are great for one task but not another, so working with multiple programming languages enables developers to use the right tool for the job. In this way, all development is polyglot; it's just the nature of the beast. The creation of a polyglot environment is often gradual and situational.
Java, Python, C++, Kotlin, and Rust are popular app development languages ranked among the world's top 10 most preferred languages in 2022.
You write the mobile apps in Java and program against the Android SDK. The critics of Java say that Java needs a lot of "boilerplate" code to do a simple task, and the concepts like exceptions are difficult to understand. Thus far, this is the most widely used language for Android app development.
Multilanguage support is easy done for android. Create a new values directory for the language with the suffix of the language code. For german: values-de or french: values-fr than copy your string. xml into that and translate each entry.
Let's assume you need one translation for whole app, and another for a dialog. What you need to do is to create two qm files: one, which contains translations of everything, except this dialog, and one, which contains translations only for this dialog. Now you can load/unload this qm files separately.
I guess you know how to change translation dynamically.
Update from comment:
To create separate qm files you could put all main sources in one directory, and all dialog sources into another and call lupdate -no-obsolete -recursive ../src/Form -ts Form_ru.ts
, where ../src/Form
is dialogs directory. Or even call lupdate -no-obsolete ../src/Form.ui ../src/Form.cpp ../src/Form.h -ts Form_ru.ts
on files.
The result script would be something like this:
lupdate -no-obsolete -recursive ../test.pro -ts test_ru.ts
lupdate -no-obsolete ../Form.ui ../Form.cpp ../Form.h -ts Form_ru.ts
As the QTranslator class has a virtual function for translate. You could inherit from QTranslator and overload the translate function to return the language string for each locale you want, depending on a flag you set in your inherited class.
You then set the flag to the locale you want to use just before creating the page for printing and then set it back after that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With