Multiple language support from file



  • Hi,

    I would like to provide a support to my desktop QT application with multiple language. My application has some button and text fields which needs to be changed based on the language selected from top of the application screen.

    I have used linguest to obtain the same but it needs to recompile the entire application whenever i need to change the button name for different language. Is it possible to give all the language text in a separate file and the application should read the file when we select the language in drop down box and display the proper name in button and text fields.

    Thanks in advance.



  • You can change the language on the fly while the application is running.
    Add the language files compiled by Linguist as resources and reimplement a changeEvent for the Language change. Something like this:

    void QTGUI_MainWindow::changeEvent(QEvent *e)
    {
    if(e->type()==QEvent::LanguageChange)
    ui->retranslateUi(this);
    }

    and use something like this to set/change the language:

    void setLanguage(QString language)
    {
    QLocale::setDefault(language);

    if(m_translator)
    {
        qApp->removeTranslator(m_translator);
        delete m_translator;
    }
    
    m_translator = new QTranslator;
    m_translator->load(QString(":/Languages/%1").arg(language));
    qApp->installTranslator(m_translator);
    

    }

    ot: can somebody tell me how to mark code segments in the new forum ?!?
    Sorry if this post looks strange.



  • Dear McLion,

    Thanks for your reply.

    If we add the linguist in resources, i would need to again create the linguist file once again if i changed any text (eg. in english, i created a linguist file for a button name "add" and later i would like to change this name to "sum". In this case, i would need to create a fresh linguist file and need to add it in resource. ).

    What i need is that i should be able to change the text in button in run time for same new language (not for other language).

    Is there any method to add all the new language text in button and the application can read this file and show it in button based on the user selection. so that, if i need to change the button name later for a same language, i can change it in the text file and i don't need to build the application.

    I hope i have explained my doubt clearly. If you did not understand, please inform me. Sorry for the confusion.

    Thanks in advance.



  • Hi dsrking,

    The basic is, if you have translations for the strings in .ts file and .qm file is generated from .ts file and you are using tr() in the code, then change in language should work perfectly.

    In your example,
    First you have a button with the text "add". You must be having the translation for add in .ts file.
    And as per your example, in the runtime, the text of button will change to "sum". So, even for sum you should have translation in the .ts file.

    So for now, you will be having two strings in .ts file "add", "sum" and their translations.

    Then generate .qm files.

    Dynamic language change should work.

    Let me know if any of the step is unclear.

    Best Regards,
    Kumar



  • @dsrking said:

    Hi,

    I would like to provide a support to my desktop QT application with multiple language. My application has some button and text fields which needs to be changed based on the language selected from top of the application screen.

    I have used linguist to obtain the same but it needs to recompile the entire application whenever i need to change the button name for different language. Is it possible to give all the language text in a separate file and the application should read the file when we select the language in drop down box and display the proper name in button and text fields.

    Thanks in advance.

    And as a minor correction, its not QT. QT stands for QuickTime. Please correct as Qt :)



  • If you would like to change the text on the buttons or any other element, maybe not in a language context, you can simply do this in code if the changes are fix. If you need to do this dynamically, i.e. let the user name elements you may need to create something that is changing and reading the names for the elements from a file.
    To my knowledge, there is no such thing present in Qt and you need to do it on your own.


Log in to reply
 

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