Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Qt5 on Android app localization
Forum Updated to NodeBB v4.3 + New Features

Qt5 on Android app localization

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
9 Posts 4 Posters 2.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gourmet
    wrote on last edited by
    #1

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

    raven-worxR 1 Reply Last reply
    0
    • G Gourmet

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

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      G 1 Reply Last reply
      0
      • raven-worxR raven-worx

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

        G Offline
        G Offline
        Gourmet
        wrote on last edited by Gourmet
        #3

        @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.

        raven-worxR K 2 Replies Last reply
        0
        • G Gourmet

          @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.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @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.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          G 1 Reply Last reply
          0
          • G Gourmet

            @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.

            K Offline
            K Offline
            KoneTaH
            wrote on last edited by KoneTaH
            #5

            @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).

            1 Reply Last reply
            1
            • raven-worxR raven-worx

              @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.

              G Offline
              G Offline
              Gourmet
              wrote on last edited by
              #6

              @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).

              K 1 Reply Last reply
              0
              • G Gourmet

                @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).

                K Offline
                K Offline
                KoneTaH
                wrote on last edited by KoneTaH
                #7

                @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.

                G 1 Reply Last reply
                1
                • K KoneTaH

                  @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.

                  G Offline
                  G Offline
                  Gourmet
                  wrote on last edited by
                  #8

                  @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.

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    Robert1
                    wrote on last edited by Robert1
                    #9

                    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

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved