Forums

OverviewFelgo 2 Support (Qt 5) › qml dynamic language update android

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #21728

    Bas

    hi,

    I found a bug when implementing and using multi language files and dynamically updating qml text and calling:

    m_engine->retranslate();

    When calling this function all bindings will be called and re evaluated.

    but also the indings for the window size, and on Android the window size is really small.

    this happens on a Samsung S9 with android version 9.

     

    i have a solution by using the following class, and

    do this in the qml file -> text: qsTr(“BLAH BLAH”) + cppObject.emptyString

    #ifndef TRANSLATIONAPP_H
    #define TRANSLATIONAPP_H
    
    #include <QObject>
    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <FelgoApplication>
    #include <QQmlContext>
    #include <QTranslator>
    #include <QDebug>
    
    class TranslationApp : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)
    
    public:
        explicit TranslationApp(QObject *parent = nullptr);
    
        QString getEmptyString() {
            return "";
        }
    
        void initialize( QQmlApplicationEngine *engine ) {
            m_engine = engine;
        }
    
        //-- check language?
        Q_INVOKABLE void autoLocaleLang() {
    
            QString locn = getLocaleLang();
    
            if( locn.indexOf("NL") > -1) {
                selectLanguage("nl");
            }
            else if( locn.indexOf("DE") > -1) {
                selectLanguage("de");
            }
            else if( locn.indexOf("EN") > -1) {
                selectLanguage("en");
            }
        }
    
        Q_INVOKABLE QString getLocaleLang() {
            QString locname = QLocale().name();
            qDebug() << "getLocaleLang() : " << locname;
            return locname;
        }
    
        Q_INVOKABLE void selectLanguage(QString language) {
    
            if(language == QString("nl")) {
                qDebug() << translator1->load("langfile_nl", ":assets/lang/");
                qApp->installTranslator(translator1);
            }
    
            if(language == QString("de")) {
                qDebug() << translator2->load("langfile_de", ":assets/lang/");
                qApp->installTranslator(translator2);
            }
    
            if(language == QString("en")) {
                qApp->removeTranslator(translator1);
                qApp->removeTranslator(translator2);
            }
    
    
            emit languageChanged();
    
    
    #if defined(Q_OS_ANDROID)
            qDebug() << "not working as expected";
    #else
            if( m_engine ) {
                m_engine->retranslate();
    
            }
    
    #endif
        }
    
    signals:
    
        void languageChanged();
    
    public slots:
    
    private:
        QTranslator *translator1;
        QTranslator *translator2;
    
        QQmlApplicationEngine *m_engine;
    };
    
    #endif // TRANSLATIONAPP_H
    
    
    
    
    
    #include "translationapp.h"
    
    TranslationApp::TranslationApp(QObject *parent) : QObject(parent)
    {
        m_engine = nullptr;
    
        translator1 = new QTranslator(this);
        translator2 = new QTranslator(this);
    }
    
    
    
    

     

     

Viewing 1 post (of 1 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support