Results 1 to 15 of 15

Thread: Dynamic Translate

  1. #1
    Join Date
    Sep 2010
    Location
    Tel Aviv, Israel
    Posts
    26
    Thanks
    2
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Dynamic Translate

    Hi!!
    I have problems in understanding how should i implement the dynamic language in my case:
    I have a class "GlobalClass" contains an array of strings in this manner:
    Qt Code:
    1. const char * const ids[] = {
    2. QT_TR_NOOP("METER"),
    3. QT_TR_NOOP("P.Q."),
    4. QT_TR_NOOP("Monitoring"),
    5. ...
    To copy to clipboard, switch view to plain text mode 

    I have some different widgets (icons) that contain a QString, which I initialize in this manner:
    Qt Code:
    1. icons[index].Text = tr(GlobalClassObject->ids[index2]);
    To copy to clipboard, switch view to plain text mode 

    Till now everything is good, and i can see the text on the screen.
    But! if i want to dynamic change it, how do i do it??
    Please respond! thanks in advance..
    Liran

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dynamic Translate

    What do you mean by that you want to dynamically change it? What do you mean by "dynamically" and what is "it"?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Location
    Tel Aviv, Israel
    Posts
    26
    Thanks
    2
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic Translate

    Dynamically translate is changing the language of your application "on-the-fly" (without reset your application).
    You can read about it in the qt programming guide.
    So anyone know how to implement what i meant?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dynamic Translate

    I know what is dynamic translation but seeing just one line of your code it is not possible to deduce what you want. If you want to change the language you need to retranslate all user-visible strings (like what retranslateUi() method of forms generated by uic does). Without knowing where your single line of code fits in it is not possible to suggest a solution.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Dynamic Translate

    Maybe you need this: dynamic translation

  6. #6
    Join Date
    Sep 2010
    Location
    Tel Aviv, Israel
    Posts
    26
    Thanks
    2
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic Translate

    Oh!
    So thats exactly what i'm asking! how should i write the retranslate function to fit my needs.
    I know how to do it when i have the string in the tr() function (like tr("string")), but i don't know how am i suppose to retranslate when i have the string in that manner of QT_TR_NOOP array.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dynamic Translate

    Exactly the same way. Eventually you'll have to call tr() to do the translation.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Sep 2010
    Location
    Tel Aviv, Israel
    Posts
    26
    Thanks
    2
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic Translate

    Hi again!
    Well, i did exactly what you said, but it still doesn't work and i'm starting to get crazy.
    This is what i do:
    1. I have a global class, which have this strings array initialize:
    Qt Code:
    1. GeneralStruct::GeneralStruct(QWidget *parent):QWidget(parent){
    2. greeting_strings[0] = QT_TR_NOOP("France");
    3. greeting_strings[1] = QT_TR_NOOP("Energy");
    4. greeting_strings[2] = QT_TR_NOOP("Caps");
    5. greeting_strings[3] = QT_TR_NOOP("Switch");
    6. };
    To copy to clipboard, switch view to plain text mode 

    2.Now, in order to install the translator when necessary, i send from the main the QApplication pointer, and later install in it the translator:
    Qt Code:
    1. app->installTranslator(NewTranlator);
    To copy to clipboard, switch view to plain text mode 

    3. Then, I change the texts of the visible icons to the new language like this:
    Qt Code:
    1. for (i=0; i<Number_Of_Boxes; i++)
    2. boxes[i].Text = tr(generalStructObject->greeting_strings[i]);
    To copy to clipboard, switch view to plain text mode 

    The new word is exactly the same! it doesn't change at all!
    What is the problem here? do i miss something?

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dynamic Translate

    What do you apart setting these Text properties? Do you map them to some widget or anything? What's the type of "boxes"?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Sep 2010
    Location
    Tel Aviv, Israel
    Posts
    26
    Thanks
    2
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic Translate

    the type of boxes are:
    Qt Code:
    1. typedef struct _menu_box
    2. {
    3. QPixmap pixmap;
    4. QPixmap pixmapAlpha;
    5. int clicked;
    6. int action;
    7. QString Text;
    8. QString ActionID;
    9. } menu_box;
    To copy to clipboard, switch view to plain text mode 

    and are placed in a widget called menu_screen:
    Qt Code:
    1. class menu_screen : public QGraphicsView
    2. ...
    3. menu_box *boxes;
    4. ...
    To copy to clipboard, switch view to plain text mode 
    Last edited by liran ritkop; 30th March 2011 at 10:25.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dynamic Translate

    How are they "placed" there? Are you assuming that:
    Qt Code:
    1. QString str = "x";
    2. QLabel lab;
    3. lab.setText(str);
    4. str = "y";
    To copy to clipboard, switch view to plain text mode 
    will make the label's text change from "x" to "y"? It won't.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Sep 2010
    Location
    Tel Aviv, Israel
    Posts
    26
    Thanks
    2
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic Translate

    They are placed there as i wrote before (look at paragraph 3).
    the tr() function return the same string in the default language.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dynamic Translate

    Did installTranslator() return true? Did you load a proper message catalogue?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Sep 2010
    Location
    Tel Aviv, Israel
    Posts
    26
    Thanks
    2
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic Translate

    Ok!
    After a long digging in this issue, I succeeded to change the language "on the fly"!
    The problem was here:
    When you declare about QT_TR_NOOP or tr() functions, the "lupdate" tool build a ts file, where each such declaration is placed under it's object label. In my case:
    Qt Code:
    1. <context>
    2. <name>GeneralStruct</name>
    3. <message>
    4. <source>METER</source>
    5. <translation type="unfinished">translatedMeter</translation>
    6. </message>
    7. ...
    To copy to clipboard, switch view to plain text mode 

    I dont know why Qt developed it that way, i hope you can tell me the reason.
    Anyway, I invoke the tr() function in another object (called menu_screen) as you can see from my previous comments.
    It didn't translate the strings, till i changed the ts file manually, and turned it that way:
    Qt Code:
    1. <name>menu_screen</name>
    2. <message>
    3. <source>METER</source>
    4. <translation type="unfinished">translatedMeter</translation>
    5. </message>
    To copy to clipboard, switch view to plain text mode 
    This time, it worked!
    Thanks for your responses, and i hope it will help others who facing this problem.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dynamic Translate

    Quote Originally Posted by liran ritkop View Post
    I dont know why Qt developed it that way, i hope you can tell me the reason.
    Because the same message might be translated differently in different contexts. You can set the context by passing an argument to the macro and to tr().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Need people to translate my app
    By xtal256 in forum General Discussion
    Replies: 2
    Last Post: 29th April 2013, 23:28
  2. Translate help framework's gui
    By yagabey in forum Qt Programming
    Replies: 0
    Last Post: 14th October 2010, 07:38
  3. translate and qgraphicsitem
    By pb in forum Qt Programming
    Replies: 1
    Last Post: 9th June 2009, 05:25
  4. translate on fly
    By Noxxik in forum Qt Programming
    Replies: 5
    Last Post: 26th February 2009, 14:21
  5. Translate QFileDialog
    By ^NyAw^ in forum Qt Programming
    Replies: 4
    Last Post: 7th April 2008, 15:20

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.