Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
tnavabothu
Posts: 48
Registered: ‎08-07-2013
My Device: z10

change language of app based on selected language

hi 

 

    in my app,i want to change the language.i used localization.it works very well,but my requirement is that.my app consists of a dropdown to select the language when certain language is selected the app has to transulate in that language.

 

    when i used localization i'm able to transulate by changing the settings of the device,but in my app i have to change the language with in the app itself.  please help me..

 

thank you

BlackBerry Development Advisor
EunKyung
Posts: 43
Registered: ‎09-27-2012
My Device: Passport

Re: change language of app based on selected language

[ Edited ]

From what you described, I think you need to listen to the language change and dynamically load the translator based on the change. There are two settings in Device>Settings>Language and Input.  Language and Region.  I am assuming that you are changing the "Language" setting to load translated UI strings for your app.

 

Check out the code snippet below to handle translations dynamically based on the language settings on the device.

Also, feel free to msg me if you have any questions.

 

In your application source code, the private section of the class should have the following:


private:
    QTranslator translator;
    bb::cascades::LocaleHandler localeHandler;
Add the following slots as well.

// Called when the system language is changed
void ApplicationUI::onSystemLanguageChanged() {
    QCoreApplication::instance()->removeTranslator(m_pTranslator);    
QString locale_string = QLocale().name();     QString filename = QString( "HelloTrans_%1" ).arg( locale_string );     if (translator.load(filename, "app/native/qm")) {         QCoreApplication::instance()->installTranslator(&translator);     } } // Called when new translators are installed (for testing) // Update all your labels, text areas, etc., void ApplicationUI::onLanguageChanged() {   qDebug() << "onLanguageChanged() called";   someLabel->setText(tr("translate me")); }

 

And finally, put the following code in the constructor of the class to hook it all up and initialize the translator:


    QObject::connect(&localeHandler, SIGNAL(systemLanguageChanged()),
                     this, SLOT(onSystemLanguageChanged()));
    QObject::connect(&localeHandler, SIGNAL(languageChanged()),
                     this, SLOT(onLanguageChanged()));

    onSystemLanguageChanged();
----------------------------------------------------
Hope this was helpful. Happy Coding!
EK @echotown
Developer
bmorr
Posts: 331
Registered: ‎04-13-2013
My Device: Z10

Re: change language of app based on selected language

I responded to a previous quesiton on here with a step-by-step tutorial.  It seemed to help so I created a PDF of it here: https://s3-us-west-2.amazonaws.com/appstuffbmo/Localization+Tutorial.pdf

 

Hope it helps.