Qt5 on Android app localization



  • Qt5 and Android have different application localization techniques. But how "merge" them? Did anybody create Android apps for different languages using Qt5?


  • Moderators

    @Gourmet
    this question is very vague.
    What localization techniques of the Android system do you want to use in your Qt app and how?



  • @raven-worx said in Qt5 on Android app localization:

    What localization techniques of the Android system do you want to use in your Qt app and how?

    On Android localization must be implemented by files strings.xml located in /res/values-XX folder and accessed by Java code like

    String hello_world = getResources().getString( R.string.hello_world );
    

    and strings.xml file must look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="app_name">Locale Application</string>
        <string name="hello_world">Hello world!</string>
    </resources>
    

    Where XX - is language suffix like "en", "de", "fr" so on. Similar for pictures with text on different languages - they must be located in /res/drawable-XX folder. This allows switch application interface correspondingly to system language. But in Qt... you know it's localization technique is very different. Or may be... can it be used in Android? If I will use Qt5 localization and user will change system language - may be application with QLocale will properly define what language it must use. And then QTranslator will properly work? I just need choose proper localization way for my new application before create it.


  • Moderators

    @Gourmet
    QTranslator doesnt integrate with android resource strings.
    Heres an quick idea how you could implement it yourself:

    1. add a method to a custom activity (deriving QtActivity):
    public String getStringResourceByName(String id)
        {
            String packageName = getPackageName();
            int resId = getResources().getIdentifier(id, "string", packageName);
            return getString(resId);
        }
    
    1. implement a plain QObject with an invokable method (e.g. tr()) which calls the java method via JNI
    2. register an instance of this object as context property (e.g. setContextProperty("$", new MyAndroidTranslator)
    3. in QML then:
    Text {
       text: $.tr("string-resource-id")
    }
    

    a bit more could would be needed if you plan to switch translations during runtime.



  • @Gourmet said in Qt5 on Android app localization:

    Or may be... can it be used in Android? If I will use Qt5 localization and user will change system language - may be application with QLocale will properly define what language it must use. And then QTranslator will properly work? I just need choose proper localization way for my new application before create it.

    Yes, Qt localization via QTranslator/QLocale can be used in Android activity (but not in service - at least I could not get QLocale to work in service). So you can freely use QTranslator, tr() and qsTr() in Qt/QML code, but, as raven-worx mentioned before, it is a separate implementation and doesn't work with native Android resource strings. But it is cross-platform and can be used for example, on iOS without nearly any changes (if you decide to port your app to iOS later).



  • @KoneTaH said in Qt5 on Android app localization:

    Yes, Qt localization via QTranslator/QLocale can be used in Android activity (but not in service - at least I could not get QLocale to work in service).

    Ok, Thanks for advise. This greatly simplifies task. I do not need QLocale in service and I do not need run-time language switch. But if this will be mandatory needed - then I can just restart application (I already did this on Android on C++/Qt5).



  • @Gourmet said in Qt5 on Android app localization:

    I do not need run-time language switch

    You can implement this too with a piece of Java code by listening for ACTION_LOCALE_CHANGED broadcast notification and calling some native method which will call QTranslator::load() with new .qm file and then install it via installTranslator().

    P.S. But remember that Activity and Qt UI code usually work in different threads, so do not interact with Qt UI classes directly from native methods of your Activity. I prefer to use signal/slot mechanism in this case, it is guaranteed to cope with cross-threading.



  • @KoneTaH yes sure. I have already QWidget based apps (music players with audio visualizations) distributed on Google Play. But they all are "international" and do not require localization.



  • I am using QTranslate in my Android app and it works perfectly. Do you load and install translation before you load your view?
    In my QML app I needed to load translation before loading main.qml file. ttrockstars


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.