Results 1 to 10 of 10

Thread: How to implement multiple Languages on an application from inside the application?

  1. #1
    Join Date
    Jul 2012
    Posts
    21
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Question How to implement multiple Languages on an application from inside the application?

    Hello every one.

    I'm very new to this Qt environment. I have made one mobile application for Symbian device which implements languages from out side of the application. That means when the application will start it will see the OS language and according to that it will select the appropriate ".qm" file and install it for the application. But my requirement is to select the language from inside of the application and load it throughout the application.

    My question is, Is it possible in Qt? And if How?

    Thanks in advance for your support.

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

    Default Re: How to implement multiple Languages on an application from inside the application

    Yes. You do it exactly the same way but rather than use QLocale to detect the language, you ask the user to choose from the list of available translations.
    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
    Jul 2012
    Posts
    21
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to implement multiple Languages on an application from inside the application

    I'm trying the same thing, means I tried to select a language from a combo box. It is returning me a file name. I have coded to install it on qApp object. I've written following code segments to work this. It is not working for me. So please check and rectify the logic.
    Qt Code:
    1. [QUOTE]main.cpp[/QUOTE]
    2.  
    3. QTranslator curTranslator;
    4.  
    5. QApplication app(argc, argv);
    6.  
    7. //installing the current language of the device before starting the application.
    8. curTranslator.load(QString("proj_") + QLocale::system().name() );
    9.  
    10. app.installTranslator(&curTranslator);
    11.  
    12. MainWindow mainWindow;//main application starts from here
    13.  
    14. mainWindow.showFullScreen();
    15.  
    16. return app.exec();
    17.  
    18. [QUOTE]Mainwindow.cpp[/QUOTE]
    19.  
    20. void MainWindow::on_Language_cb_currentIndexChanged(int index)
    21. {
    22. QString langAbbr;
    23.  
    24. switch(index)
    25. {
    26. case 0: langAbbr="en";break;
    27.  
    28. case 1: langAbbr="ar";break;
    29.  
    30. case 2: langAbbr="fr";break;
    31.  
    32. default: langAbbr="en";
    33. }
    34.  
    35. qApp->removeTranslator(&curTranslator);
    36.  
    37. QString tempStr=QString("proj_%1.qm").arg(langAbbr);
    38.  
    39. qDebug() << "current translation file"<<tempStr; //shows the name of the file.
    40.  
    41. curTranslator.load(tempStr);// not sure that whether it is loading the file or not...
    42.  
    43. qApp->installTranslator(&curTranslator); //it should install the selected language on the whole application..
    44. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: How to implement multiple Languages on an application from inside the application

    What is "not working"? What does QTranslator::load() return?
    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
    Jul 2012
    Posts
    21
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to implement multiple Languages on an application from inside the application

    Qt Code:
    1. curTranslator.load(tempStr);// not sure that whether it is loading the file or not...
    To copy to clipboard, switch view to plain text mode 
    It was giving 'false' as no path was there.
    Now I modified that segment to something like following code
    Qt Code:
    1. void MainWindow::on_Language_cb_currentIndexChanged(int index)
    2. {
    3. QString langAbbr;
    4. switch(index)
    5. {
    6. case 0: langAbbr="en";break;
    7. case 1: langAbbr="ar";break;
    8. case 2: langAbbr="fr";break;
    9. default: langAbbr="en";
    10. }
    11. QString tempStr=QString("proj_%1.qm").arg(langAbbr);
    12.  
    13. bool loader=curTranslator.load(tempStr,"D:/Test-to-device/17-Jul-2012/proj");
    14.  
    15. qDebug() << "current translation file"<<tempStr<<"file loaded"<<loader;
    16.  
    17. qApp->installTranslator(&curTranslator);//till this line it is working fine. and showing me the correct language file is loading
    18.  
    19. ui->retranslateUi(this);//if I include this line then out put is coming like the followings
    20. }
    To copy to clipboard, switch view to plain text mode 
    current translation file "proj_fr.qm" file loaded true

    `current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false `
    /*these lines between back quotes are repeated more than 30 times*/
    The program has unexpectedly finished.
    D:\Test-to-device\17-jul-2012\debug\proj.exe exited with code -1073741571
    Last edited by satya@sipl; 18th July 2012 at 09:31.

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

    Default Re: How to implement multiple Languages on an application from inside the application

    And is that good or bad?
    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.


  7. #7
    Join Date
    Jul 2012
    Posts
    21
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to implement multiple Languages on an application from inside the application

    wysota, My problem is that while selecting the language "french", the qDebug() should show me only one line
    current translation file "proj_fr.qm" file loaded true
    not anything else.
    And while I'm writing code segment
    Qt Code:
    1. qApp->installTranslator(&curTranslator);
    2.  
    3. ui->retranslateUi(this);
    To copy to clipboard, switch view to plain text mode 
    It should show me the effect of the language change on my UI which is not happening. Instead qDebug() is showing
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    current translation file "proj_en.qm" file loaded true
    current translation file "proj_.qm" file loaded false
    These lines are continuing for 30 times or 40 times.
    And then the program is crashing with following message
    The program has unexpectedly finished.
    D:\Test-to-device\17-jul-2012\debug\proj.exe exited with code -1073741571

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

    Default Re: How to implement multiple Languages on an application from inside the application

    Your combobox that keeps the list of languages is being retranslated, causing the signal to be emitted again, which results in trying to load a new translation, calling retranslateUi() and the whole situation repeats until you exhaust your stack space and the program crashes. Make your translation loading slot smarter to prevent such situations.
    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.


  9. The following user says thank you to wysota for this useful post:

    satya@sipl (18th July 2012)

  10. #9
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Cool Re: How to implement multiple Languages on an application from inside the application

    First install the translator i.e.;
    Qt Code:
    1. qApp->installTranslator(&trans);
    To copy to clipboard, switch view to plain text mode 

    use changeEvent() for changing the language of every source text. In change event check for the condition that is:-

    Qt Code:
    1. if(event->type()==QEvent::LanguageChange)
    2. {
    3. retranslateUi();
    4. }
    To copy to clipboard, switch view to plain text mode 
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  11. #10
    Join Date
    Jul 2012
    Posts
    21
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Thumbs up Re: How to implement multiple Languages on an application from inside the application

    Thank you very much wysota

    you made my day.

    I just added a user defined slot to signal
    activated(int)
    from combo Box and pasted my code there.

    Now I'm running good.

    Thank you.

Similar Threads

  1. Drag 'n drop Tab inside and outside the Main Application
    By mstegehu in forum Qt Programming
    Replies: 5
    Last Post: 22nd June 2014, 20:30
  2. Replies: 3
    Last Post: 20th October 2010, 22:36
  3. Drawing widgets inside a Qt application
    By aughey in forum Qt Programming
    Replies: 4
    Last Post: 13th May 2010, 08:45
  4. Multiple Qt Application
    By girishgowda in forum Qt Programming
    Replies: 4
    Last Post: 11th May 2010, 10:03
  5. Implement Skins in a QT4 application
    By zizou85 in forum Newbie
    Replies: 1
    Last Post: 27th May 2009, 10:03

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
  •  
Qt is a trademark of The Qt Company.