PDA

View Full Version : Plugin and internationalization



blackliteon
12th February 2006, 11:53
Hi!
I build my plugins, but can't load compiled language files.
Plugins automatically don't load language files that's lie near. (Becase it do QCore... class)
When I try to put phrases into main application langugage file, nothing happense too.
I Know that we can use Language loading by hand, but don't know how set this language file for plugin. (only for Application)

wysota
12th February 2006, 13:34
You have to load the files manualy into the translators while initialising plugins.

blackliteon
12th February 2006, 20:37
You mean I need load translations in main application?
And then tell to plugin thats we load it ?
Maybe a little sample of code ?:o

wysota
12th February 2006, 20:53
Hmm... Your plugin class should contain a method to create a translator ready to install, something like:


struct MyPluginInterface {
virtual ~MyPluginInterface(){};
virtual QTranslator *pluginTranslator(QString locale) = 0;
// ...

};

Your plugin should implement it like so:


QTranslator *MyPluginImplementation::pluginTranslator(QString locale){
QTranslator *translator = new QTranslator(this);
// you may need to change "this" to something else above
translator->load(":/translations/"+locale+".qm");
return translator;
}

And your main app:


QObject *plugin = loader.instance();
if(plugin){
MyPluginInterface *piface = qobject_cast<MyPluginInterface*>(plugin);
qApp->installTranslator(piface->translator("pl_PL");
}

You can try to load the translator directly from the plugin too instead of doing it in the main app.

blackliteon
13th February 2006, 09:51
You mean that's "tr" function used in plugin will be traslated by "qm" loaded from main application...
Yes, plugin can't define his translator by default, but we can inherit QTCore....
Today i try your way.

wysota
13th February 2006, 12:03
You mean that's "tr" function used in plugin will be traslated by "qm" loaded from main application...
Yes, sure.

Yes, plugin can't define his translator by default, but we can inherit QTCore....

I don't understand what you mean here. Definitely not "inherit". You can have only one QCoreApplication object in an application. But there is a global pointer to a QApplication object available, so you can use it anywhere.

blackliteon
13th February 2006, 15:52
If I understand you when we use tr everywhere we use QCoreApplication ?

wysota
13th February 2006, 16:06
tr() is just a call which uses an installed translator to get a translation for a string. Translators are installed in the application object, so yes, if you use tr() a call to the application object is made behind the scene.

blackliteon
13th February 2006, 21:47
So we use installTranslator function for each translator.
You mean that's translator add language phrases
I mean that if we load for every plugin his translational file all of them will be summed (phrases) ?

wysota
13th February 2006, 23:07
I mean that if we load for every plugin his translational file all of them will be summed (phrases) ?
Yes, they will.

blackliteon
14th February 2006, 08:29
Thanks! Theme closed!