Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Can a page changed by the change of language?



  • Hello everyone! Me again!
    Basically, I am asking that, can I change a page by changing the language?
    example,

    if language is english -> open somepage.qml
    if language is chinese -> open somepage_CH.qml
    if language is dog language -> open somepage_DOG.qml
    

    my body structure,
    //
    Body.qml

    //...
    SomeCategory.Page1{
            id:scp1
        }
        SomeCategory.Page2{
            id:scp2
        }
        SomeOtherCategory.Page1{
            id:socp1
        }
         SomeOtherCategory.Page2{
            id:socp2
        }
    
        Component.onCompleted: {
            GSystem.logoItm = logo;
            GSystem.addPageItem("Page 1",scp1);
            GSystem.addPageItem("Page 2",scp2);
            GSystem.addPageItem("Page 3",scp3);
    //...
    

    //
    translator.h

    #ifndef TRANSLATOR_H
    #define TRANSLATOR_H
    
    #include <QObject>
    #include <QGuiApplication>
    #include <QQuickView>
    #include <QTranslator>
    #include "langs.h"
    
    class Translator : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)
    
    public:
        Translator(QGuiApplication* app) { mApp = app; }
    
        QString getEmptyString() { return ""; }
    
    signals:
        void languageChanged();
    
    public slots:
        void updateLanguage(int lang){
            switch(lang){
            case Langs::AR:
                mTranslator.load("general_AR", ":/translator");
                mApp->installTranslator(&mTranslator);
                break;
            case Langs::JAP:
                mTranslator.load("general_JA", ":/translator");
                mApp->installTranslator(&mTranslator);
                break;
            case Langs::TR:
                mTranslator.load("general_TR", ":/translator");
                mApp->installTranslator(&mTranslator);
                break;
            case Langs::CH:
                mTranslator.load("general_CH", ":/translator");
                mApp->installTranslator(&mTranslator);
                break;
            default:
                mApp->removeTranslator(&mTranslator);
                break;
            }
            emit languageChanged();
        }
    
    private:
        QGuiApplication* mApp;
        QTranslator mTranslator;
    };
    
    #endif // MYTRANSLATOR_H
    

    The page I want to change by language is being opened by a menu button.
    //
    TopMenu.qml

    Button {
                                id:btnLights
                                bgSource : "qrc:/Lights.svg"
                                textColor: "white"
                                anchors.verticalCenter: parent.verticalCenter
                                width:77
                                height:88
                                x: 428
                                text: qsTr("LIGHTS") + mytrans.emptyString
                                onClicked: function(){
                                    console.log("lights clicked");
                                    GSystem.state = "Lights";
                                    GSystem.changePage("Lights");
                                }
                        }
    

    I tried to add a property var that is changing by which flag did you clicked in settings page to change your language, and use it with an if statement on that button like,

      onClicked: function(){
                     console.log("lights clicked");
                     GSystem.state = "Lights";
                     if (langvar==1){
                    GSystem.changePage("Lights_CH");
                    }else if (langvar==2){
                    GSystem.changePage("Lights_TR");
                     }else{
                    GSystem.changePage("Lights");
                    }
      }
    

    but it keeps langvar=0 by default and just the page "Lights" opens.
    Any ideas?



  • @closx Hi, here some comment from my side.

    1. please change getEmptyString() declaration to:
    QString getEmptyString() const { return QString(); }
    

    using return ""; is a very bad idea, this will always create a new QString instance with an empty string.

    1. where is langVar defined?

    2. I would update Translator class to:

    class Translator : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)
        Q_PROPERTY(int currentLanguage READ currentLanguage  NOTIFY currentLanguageChanged)
    
    public:
        explicit Translator(QObject *parent = nullptr): QObject(parent) {  m_currentLanguage =0; }
    
        QString getEmptyString() const { return QString(); }
        int currentLanguage() const { return m_currentLanguage; }
    
    signals:
        void languageChanged();
        void currentLanguageChanged(int currentLanguage);
    
    public slots:
        void updateLanguage(int lang){
            switch(lang){
            case Langs::AR:
                mTranslator.load("general_AR", ":/translator");
                qApp->installTranslator(&mTranslator);
                break;
            case Langs::JAP:
                mTranslator.load("general_JA", ":/translator");
                qApp->installTranslator(&mTranslator);
                break;
            case Langs::TR:
                mTranslator.load("general_TR", ":/translator");
                qApp->installTranslator(&mTranslator);
                break;
            case Langs::CH:
                mTranslator.load("general_CH", ":/translator");
                qApp->installTranslator(&mTranslator);
                break;
            default:
                lang = 0;
                qApp->removeTranslator(&mTranslator);
                break;
            }
            emit languageChanged();
            if(m_currentLanguage != lang)
            {
                m_currentLanguage  = lang;
                emit currentLanguageChanged(lang);
            }
        }
    
    private:
        int m_currentLanguage;
        QTranslator mTranslator;
    };
    

    Then use mytrans.currentLanguage when I have to know which is the current language.



  • @closx have you tried the QML internationalization example?
    It looks like you're over-complicating things...
    For sure you don't want to have one QML file per language...



  • @KroMignon I solved it last night JUST AS you say! Created a public variable that changes by the change of language integer (And I just learned that the languages has integer values as identifications. As, English=31, Chinese=25, Turkish=125 for me), and used that variable to make the page decision. Thanks for your answer, again! You are very helpful....

    //
    TopMenu.qml -> Menu Button

    onClicked: function(){
                console.log("lights clicked");
                GSystem.state = "Lights";
                if(smanager.lang==125){ 
                            GSystem.changePage("Lights_TR");
                }else if (smanager.lang==25)
                {
                            GSystem.changePage("Lights_CH");
                }else{
                            GSystem.changePage("Lights");
                }
    }
    //imported the translator as smanager...
    

    @Pablo-J.-Rogina said in Can a page changed by the change of language?:

    @closx have you tried the QML internationalization example?
    It looks like you're over-complicating things...
    For sure you don't want to have one QML file per language...

    Hey! Thanks for your answer! Actually, what I want from this solution is a little more complicated then you think :D Think like there is a page called "My traditional values and beliefs", and it should be changed in some labels, and the page structure by the change of language! -Not exactly like that, but I guess it is the closest way I can describe :D- (And thanks for your advice, but yes, I tried the example, friend :D)


  • Moderators

    @closx
    Don't forget to change to topic to Solved via the topic tools, if it is solved ;-)



  • @J.Hilk Done :D Thank all you guys!


  • Qt Champions 2018

    The best way to do this would be to use a QQmlFileSelector

    By conditionnaly adding a file selector for the current language, your code could just load somepage.qml and the qml engine will resolve it as +CH/somepage.qml if you have a CH selector.


Log in to reply