Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Localization of a library

Localization of a library

Scheduled Pinned Locked Moved General and Desktop
30 Posts 4 Posters 18.2k Views 1 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.
  • R Offline
    R Offline
    realdarkman
    wrote on last edited by
    #1

    Hi all,

    I put common functions in a dynamical linked library for my main app. The strings and forms of main app are translated and will be loaded with QTranslator::load(...) and installed with QApplication::installTranslator(...), it works. But the translated strings and forms in the lib are not used, always english only! App and lib translation loading and installing are the same:

    @
    QTranslator translator;
    translator.load("german.qm");

    QApplication::installTranslator(&translator);
    @

    Why are the strings and forms from lib in english only? Any hints?

    Thanks!
    Chris

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      Does german.qm really contain the translations for the library too?

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • R Offline
        R Offline
        realdarkman
        wrote on last edited by
        #3

        Yes, I know, that is strange! I have test it with full path to german.qm ... all strings and forms are the english ones! I cannot explain this!

        1 Reply Last reply
        0
        • S Offline
          S Offline
          soroush
          wrote on last edited by
          #4

          Some suggestions:

          • Make sure that your translation file is loaded successfully. Try:
            @qDebug()<<translator.load("german.qm");@
          • Make sure you're loading language file before instantiating GUI elements.
          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            As you make the translator a local stack object, where do you call this?
            When the method exits, the translator will be destroyed and uninstalled.

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • R Offline
              R Offline
              realdarkman
              wrote on last edited by
              #6

              [quote author="soroush" date="1338288047"]* Make sure that your translation file is loaded successfully. Try:
              @qDebug()<<translator.load("german.qm");@[/quote]

              I made this already, it's always true!

              [quote author="soroush" date="1338288047"]* Make sure you're loading language file before instantiating GUI elements.[/quote]

              I load and install all translations in the main function before the MainWindow will initialized, but the lib has no main function!? Where can I load and install translations for the lib?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                soroush
                wrote on last edited by
                #7

                A library is a passive object. It has no entry point. So you may need to call a function of your library that installs translator, from program that uses the library.

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  realdarkman
                  wrote on last edited by
                  #8

                  Yes, I made this. In my MainApp:

                  @
                  int main(int argc, char *argv[]) {
                  QApplication application(argc, argv);

                  QTranslator qtTranslator, appTranslator;
                  QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
                  
                  qtTranslator.load("/home/chris/myApp/locale/qt_de.qm");
                  appTranslator.load("/home/chris/myApp/locale/german.qm");
                  
                  application.installTranslator(&qtTranslator);
                  application.installTranslator(&appTranslator);
                  
                  MyLib::loadTranslation();
                  
                  MyApp myApp;
                  myApp.show();
                  
                  return application.exec&#40;&#41;;
                  

                  }
                  @

                  MyLib:

                  @
                  void MyLib::loadTranslation() {
                  QTranslator translator;
                  QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));

                  translator.load("/home/chris/myLib/locale/german.qm");
                  
                  QApplication::installTranslator(&translator);
                  

                  }
                  @

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

                    All

                    @QTranslator::load(...)@

                    functions returns true and all strings/forms of MyApp are translated, but all strings/forms of MyLib are english only!

                    I don't know why!

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      soroush
                      wrote on last edited by
                      #10

                      Sorry I didn't noticed that... calling
                      @
                      QApplication::installTranslator(&translator);
                      @
                      inside a library makes no sense. There should be one instance of QApplication that installs all translation.

                      Consider Qt itself. When you load & install a translation of Qt libraries, there is no need to call any member inside libraries.

                      Make sure that your classes are inherited from QObject. They should also implement signal/slot mechanism (use Q_OBJECT macro).

                      I wrote a simple test. Works fine for me.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        soroush
                        wrote on last edited by
                        #11

                        I should also mention that, If you're re-implementing changeEvent(QEvent *e) and you tend to use texts from library in your GUI, you will have two sources for translation. This will prevent your application from extracting text correctly.

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          realdarkman
                          wrote on last edited by
                          #12

                          The german.qm is compiled in the lib as resource. The hard coded path above was a test. How can I load german.qm from library resource to load & install the translation? Is there a way?

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            soroush
                            wrote on last edited by
                            #13

                            Hmmm
                            You're doing it from hard way!
                            Ask your library to give you binary data of her translation file (contents of .qm embedded in your library).
                            Create a QTranslator in your application. "Load your binary data":http://doc-snapshot.qt-project.org/4.8/qtranslator.html#load-3:
                            @translator.load ( const uchar * data, int len)@
                            Install your translator to QApplication (outside library)

                            1 Reply Last reply
                            0
                            • R Offline
                              R Offline
                              realdarkman
                              wrote on last edited by
                              #14

                              Still a little problem: The data of german.qm from lib resource is compressed, so I need to use

                              @QByteArray qUncompress(const uchar *data, int nbytes)@

                              But how do I get a const uchar* from the QByteArray for QTranslator::load?

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                goetz
                                wrote on last edited by
                                #15

                                It should be possible to load the translation directly from the ressource:

                                @
                                QTranslator trans;
                                trans.load(":/path/to/resource/german.qm");
                                @

                                http://www.catb.org/~esr/faqs/smart-questions.html

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  soroush
                                  wrote on last edited by
                                  #16

                                  @
                                  QByteArray ba;
                                  ba = ...
                                  unsigned char data = (unsigned char) ba.data();
                                  @

                                  Volker's solution is better.

                                  1 Reply Last reply
                                  0
                                  • G Offline
                                    G Offline
                                    giesbert
                                    wrote on last edited by
                                    #17

                                    [quote author="realdarkman" date="1338289960"]MyLib:

                                    @
                                    void MyLib::loadTranslation() {
                                    QTranslator translator;
                                    QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));

                                    translator.load("/home/chris/myLib/locale/german.qm");
                                    
                                    QApplication::installTranslator(&translator);
                                    

                                    }
                                    @
                                    [/quote]

                                    As written earlier, you destroy the translator object after installing it directly. This will lead to no loaded translation again. You have to create it on the heap or create it in main and give it to the function.

                                    Nokia Certified Qt Specialist.
                                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                                    1 Reply Last reply
                                    0
                                    • R Offline
                                      R Offline
                                      realdarkman
                                      wrote on last edited by
                                      #18

                                      The german.qm is a ressource of MyLib, Volker's solution will not work!? Or can I access the MyLib resources from MyApp?

                                      Is there a better (C++) method to cast the ba.data()? I want to avoid C casts!

                                      1 Reply Last reply
                                      0
                                      • G Offline
                                        G Offline
                                        giesbert
                                        wrote on last edited by
                                        #19

                                        Volkers solution should work if you don't destroy the translator object.

                                        Nokia Certified Qt Specialist.
                                        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                                        1 Reply Last reply
                                        0
                                        • R Offline
                                          R Offline
                                          realdarkman
                                          wrote on last edited by
                                          #20

                                          [quote author="Gerolf" date="1338366133"]You have to create it on the heap or create it in main and give it to the function.[/quote]

                                          Create on heap? I give the translator to loadTranslation, but it doesn't work:

                                          [quote]/usr/include/QtCore/qtranslator.h:94: error: 'QTranslator::QTranslator(const QTranslator&)' is private[/quote]

                                          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