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
Developer
ekke
Posts: 1,571
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone
Accepted Solution

PROBLEM: getting translations from Cascades Library

I'm having problems to get translations from my library project into application project

 

Thanks to Isaac Gordetzky the Library template was a great starting point and I got some more tips from him

 

So in the meantime I resolved some taks:

 

1. Library Project creates translation files

I had to add a skeleton bar-descriptor.xml to the library project and add my locales there

 

2.Get tr() work in C++

I had to replace

tr("...")

with
QT_TRANSLATE_NOOP("my::namespace::MyClass","...")

3. Use of QRC

In my .pro I inserted

RESOURCES += baasParseLibrary.qrc

where baasParseLibrary is my library name

 

this is the content of baasParseLibrary.qrc:

<!DOCTYPE RCC>
<RCC version="1.0">
<qresource prefix="/baasParseLibrary">
<file>translations/baasParseLibrary_de.ts</file>
<file>translations/baasParseLibrary_en.ts</file>
</qresource>
</RCC>

 

4. Initialize QRC in my Appplication

In main.cpp I inserted this before Application was created

Q_INIT_RESOURCE(baasParseLibrary);

 

5. Provide Translator from my Library code

added a method to the API of my library:

void MyLibApp::updateTranslations() {
static QTranslator * m_pTranslator = new QTranslator(
		QCoreApplication::instance());
QString locale_string = QLocale().name();
QString file_name = QString(":/baasParseLibrary/translations/baasParseLibrary_%1").arg(locale_string);
if (m_pTranslator->load(file_name, "app/native/qm")) {
	QCoreApplication::instance()->installTranslator(m_pTranslator);
} else {
	qDebug() << "cannot load " << file_name;
}
}

 Then from my app I'm calling updateTranslations()

 

PROBLEM:

from updateTranslations() the file cannot be loaded into the translator.

 

so there must be something wrong.

Question: are prefix and file in the qrc file correct ?

prefix /baasParseLibrary is the name of the library project in the workspace - not the physical location on disc

in updateTranslations() I'm refering the same path

 

Or am I missing something else ?

I never hav done something with QRC or translations from libraries before.

 

all the other things are working - also getting assets like images from the library

 

thanks for tips

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner http://vimeo.com/ekkescorner/videos
bb10-development: http://appbus.org Twitter: @ekkescorner
BlackBerry Development Advisor
IsaacGordezky
Posts: 57
Registered: ‎10-04-2012
My Device: developer

Re: PROBLEM: getting translations from Cascades Library

[ Edited ]

The translation .ts files are source files for the qt translation system, which generates binary .qm files that are used at run-time. You just need to include the .qm files instead of the .ts files.

<!DOCTYPE RCC>
<RCC version="1.0">
<qresource prefix="/baasParseLibrary">
<file>translations/baasParseLibrary_de.qm</file>
<file>translations/baasParseLibrary_en.qm</file>
</qresource>
</RCC>

 

To address your other questions:

prefix /baasParseLibrary is arbitrary, and the same as your project name by convention. I put it in to avoid collisions, but I am not sure if it is necessary for shared libraries.

 

Also, note the default cascades project ApplicationUI onSystemLanguageChanged() function:

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

Note the removeTranslator(m_pTranslator) call is missing in your code

 

Developer
ekke
Posts: 1,571
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: PROBLEM: getting translations from Cascades Library

[ Edited ]

Hi Isaac,

 

thanks for your hint. You're right I have to use the *.qm files inside the QRC file. because they were compiled into .qm from library build process before.

 

Then I got the libraray files loaded. (Both: from APP and LIB)

 

But translations still failed.

 

tried many variations and then found the reason. We changed all

tr("..") 

 

into

QT_TRANSLATE_NOOP("ekkescorner::parse::ParseREST","...")

 

to let the libraray compile without error

 

this doesn't work as I found out now.

But I remembered that you mentioned a second notation which could help.

And YES: now translations will be found if using

 

::ekkescorner::parse::ParseREST::tr("...")

 

 

curious: both create exactly the same content in *.ts files, but only the last one works.

 

... will blog about all this later when publishing my Parse.com Libraray for Cascades at Github.

--------

finally want to verify that my code is correct to add both translators.

 

here's the usual stuff generated by Cascades Project template inside the APP:

 

void ApplicationUI::onSystemLanguageChanged() {
	QCoreApplication::instance()->removeTranslator(m_pTranslator);
	// Initiate, load and install the application translation files.
	QString locale_string = QLocale().name();
	QString file_name = QString("baasParseTest_%1").arg(locale_string);
	if (m_pTranslator->load(file_name, "app/native/qm")) {
		QCoreApplication::instance()->installTranslator(m_pTranslator);
		qDebug() << "load translator file from APP: " << file_name;
	} else {
		qDebug() << "cannot load " << file_name;
	}
	mServer->updateTranslations();
}

 as you see after doing first initialization of translator from the app, I'm calling the library:

 

void ParseREST::updateTranslations() {
	static QTranslator * m_pTranslator = new QTranslator(
			QCoreApplication::instance());
	QCoreApplication::instance()->removeTranslator(m_pTranslator);
	QString locale_string = QLocale().name();
	QString file_name = QString(":/baasParseLibrary/translations/baasParseLibrary_%1").arg(locale_string);
	if (m_pTranslator->load(file_name)) {
		QCoreApplication::instance()->installTranslator(m_pTranslator);
		qDebug() << "load translator file from LIB : " << file_name;
	} else {
		qDebug() << "cannot load " << file_name;
	}
}

 and from debug I see:

INFO        load translator file from APP:  "baasParseTest_de_DE"

INFO        load translator file from LIB :  ":/baasParseLibrary/translations/baasParseLibrary_de_DE"

 

and translations from C++ and QML are working now.

 

I'm wondering about the removeTranslator() - calling from both methods and thought the second one would remove the first. but it seems that you're right and I have to remove always.

 

... thanx again ... next step done using Cascades Libraries not only providing C++ code but also translations and assets like images

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner http://vimeo.com/ekkescorner/videos
bb10-development: http://appbus.org Twitter: @ekkescorner