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
Trusted Contributor
Posts: 134
Registered: ‎05-31-2010
My Device: 8300
My Carrier: vodqafone
Accepted Solution

Unable to change locale.

Hi,

 

   I'm changing  the app language from English to Arabic when the locale is changed in Device settings. But the text of my qml still remains in English. My FileHandling_ar.ts looks like

 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="ar_SA">
<context>
    <name>Locale</name>
    <message>
        <location filename="../assets/Locale.qml" line="10"/>
        <source>Hello</source>
        <translation>مرحبا</translation>
    </message>
</context>
<context>
    <name>main</name>
    <message>
        <location filename="../assets/main.qml" line="10"/>
        <source>Hello World</source>
        <translation></translation>
    </message>
</context>
</TS>

My cpp file connects with the systemLanguageChanged() signal of LocaleHandler.  Find this below.

 

LocaleTest::LocaleTest(bb::cascades::Application *app) :
		QObject(app) {

	root = 0;
	m_pTranslator = new QTranslator(this);
	m_pLocaleHandler = new LocaleHandler(this);
	QObject::connect(m_pLocaleHandler, SIGNAL(systemLanguageChanged()), this,
			SLOT(onSystemLanguageChanged()));

	onSystemLanguageChanged();

	// create scene document from main.qml asset
	// set parent to created document to ensure it exists for the whole application lifetime
	QmlDocument *qml = QmlDocument::create("asset:///Locale.qml").parent(this);
	qml->setContextProperty("cppObj", this);
	// create root object for the UI
	root = qml->createRootObject<AbstractPane>();
	// set created root object as a scene
	app->setScene(root);
}

void LocaleTest::onSystemLanguageChanged() {
    QCoreApplication::instance()->removeTranslator(m_pTranslator);

    // Initiate, load and install the application translation files.
    QString locale_string = QLocale().name();
    QString file_name = QString("FileHandling_%1").arg(locale_string);
    if (m_pTranslator->load(file_name, "app/native/qm")) {
        QCoreApplication::instance()->installTranslator(m_pTranslator);
    }
}

  Upon language change, onSystemLanguageChanged method is successfully called and locale is changed. But, my text which is in Locale.qml file is not changed to arabic.

 

Locale.qml file looks like.

 

import bb.cascades 1.0

Page {
    Container {
        layout: StackLayout {
            orientation: LayoutOrientation.BottomToTop
        }
        Label {
            objectName: "label"
            text: qsTr("Hello")
        }
    }
}

Please help me out.

 

Thanks,

Sha.

Developer
Posts: 163
Registered: ‎04-28-2012
My Device: BlackBerry 9800
My Carrier: BlackBerry

Re: Unable to change locale.

Hi harsha,

              

              

                If u have changed the language of your device when the app is running , then you need to append Retranslate.onLanguageChanged to the text that is going to effect. Then your application will reflect the language changes. I think it will solve your problem.

 

import bb.cascades 1.0

Page {
    Container {
        layout: StackLayout {
            orientation: LayoutOrientation.BottomToTop
        }
        Label {
            objectName: "label"
            text: qsTr("Hello") + Retranslate.onLanguageChanged
        }
    }
}

 Regards,

 Naresh Kodumuri.

Trusted Contributor
Posts: 134
Registered: ‎05-31-2010
My Device: 8300
My Carrier: vodqafone

Re: Unable to change locale.

That worked like a charm.. Thanks very much @nareshkodumuri

Trusted Contributor
Posts: 134
Registered: ‎05-31-2010
My Device: 8300
My Carrier: vodqafone

Re: Unable to change locale.

Naresh,

 

  When I try to use the translation strings in cpp files with the help of tr("string"), I'm unable to identify the corresponding strings in .tr files. Are these traslations specific to QML files?

 

Regards,

Sha.

Developer
Posts: 163
Registered: ‎04-28-2012
My Device: BlackBerry 9800
My Carrier: BlackBerry

Re: Unable to change locale.

[ Edited ]

 

             No Harsha, whatever the file you are using qstr() (In qml) and tr() (in cpp) you need to add the path of that folder in your .pro file. Just Check your .cpp file, those cpp files are directly under src folder or in any sub folder, if it is in any sub folder  u need to add these lines to .pro file, check it. I think it will solve your problem.

 

lupdate_inclusion {
 SOURCES += \
    $$quote($$BASEDIR/../assets/pages(name of the sub folder under assets folder)/*.qml) \
    $$quote($$BASEDIR/../src/main(name of the sub folder under src folder)/*.cpp) 
}

 Regards,

Naresh Kodumuri.

Trusted Contributor
Posts: 134
Registered: ‎05-31-2010
My Device: 8300
My Carrier: vodqafone

Re: Unable to change locale.

Wow... that worked out.. Thanks again Naresh ....

Trusted Contributor
Posts: 134
Registered: ‎05-31-2010
My Device: 8300
My Carrier: vodqafone

Re: Unable to change locale.

Naresh,

 

  Thanks very much for helping me in this locale's .

 

 Is there any way to change the text automatically for Cpp files like retranslate in QML files ?

Highlighted
Developer
Posts: 163
Registered: ‎04-28-2012
My Device: BlackBerry 9800
My Carrier: BlackBerry

Re: Unable to change locale.

Hi harsha,

 

                It is possible to change the text automatically from cpp also like in qml,in your LocalTest constructor connect languageChanged() signal to a slot and there you can write your function

QObject::connect(m_pLocaleHandler , SIGNAL(languageChanged()),
this, SLOT(onLanguageChanged()));

void LocalTest::onLanguageChanged() {
  label->setText(tr("SomeText"));
}

 Check it, I haven't tried but it will work.

 

 

Regards,

Naresh Kodumuri.