How to load translation for static library widget
-
My question is quite simple. How can I load *.qm from static library resource? as you can see below I can load *.qm easily from current project resource by using ":/"
You're asking the wrong question. Actually @SGaist and @AlterX have provided valuable feedback and you'd be wise to take it into account. I peeked into your repository and I'm pretty sure you're creating your translator wrongly. Did you notice this warning: "Note that the translator must be created before the application's widgets." in the documentation?
Kind regards.
-
To add to @kshegunov , the note concerns the use of QTranslator shown in the example i.e. setup of the language at application startup and no support of dynamic translations.
-
@SGaist said:
To add to @kshegunov , the note concerns the use of QTranslator shown in the example i.e. setup of the language at application startup and no support of dynamic translations.
How the application doesn't support dynamic translations? Take a look into this line it calls
changeEvent
and if you run the app. you'll find thatTester
loads the translations dynamically correctly while it doesn't call translations for static library widget because I don't know how to do it! -
I tried to use
Q_INIT_RESOURCE
as mentioned in this line but unfortunately it makes things worst. -
How the application doesn't support dynamic translations?
He was referring to the note in the documentation I was citing, and was explaining that it is only relevant when there is no support for dynamic translation.
it doesn't call translations for static library widget because I don't know how to do it!
You do it, like you're doing it now. Here's the thing, a static library is nothing more then a bunch of .obj files put together. Linking a static library just includes them into your application's binary. This means, that there is nothing different between widgets that come from a static library and these declared in your application (see @AlterX's comment, he mentioned that). That's why I said you're asking the wrong question. Your translation loading looks perfectly fine to me, and it has nothing to do with static or dynamic libraries - you need to look elsewhere to solve the problem. Do you have any output in the creator's windows that might suggest your translation is not loaded? Do you get the "Failed to load a translation for QT in your local language" message printed?
I tried to use Q_INIT_RESOURCE as mentioned in this line but unfortunately it makes things worst.
Yes you should do that for all the resources in your static library. But according to this it should be placed in the beginning of
main()
.Kind regards.
-
Do you have any output in the creator's windows that might suggest your translation is not loaded?
No
Do you get the "Failed to load a translation for QT in your local language" message printed?
No. I get
Failed to load translation from static library
error message! -
Hello,
Something is amiss here, let's take it from the top. You said that you get the "Failed to load a translation for QT in your local language" message, but this message is to be shown when this fails:myTranslator->load(*local, QLatin1String("tester"), QLatin1String("_"), QLatin1String(":/i18n"))
This particular line is in your application and tries to load an application translation, where does a static library resource come in play here? Could you tell what is the current language you're using and its iso code (you could use
QLocale::language()
andQLocale::languageToString
), and make sure there's a matching translation file?Kind regards.
-
@kshegunov said:
Hello,
Something is amiss here, let's take it from the top. You said that you get the "Failed to load a translation for QT in your local language" message, but this message is to be shown when this fails:myTranslator->load(*local, QLatin1String("tester"), QLatin1String("_"), QLatin1String(":/i18n"))
I didn't say that!
I said I getFailed to load translation from static library
which points tomyTranslator->load(":/languages/arabic")
failure!where does a static library resource come in play here?
As I mentioned above this line suppose to load translation from static library's resource
Could you tell what is the current language you're using and its iso code
English; en_US
-
I didn't say that!
I said I get Failed to load translation from static libraryIndeed you did, it's my mistake, sorry.
which points to myTranslator->load(":/languages/arabic") failure!
Could you verify that this file is available? For example what does
QFile::exists(":/languages/arabic")
return, true or false? If the file exists then the problem is with the actual loading, if the file is not found, then I guess there's a problem with the resource initialization.Kind regards.
-
@kshegunov said:
Could you verify that this file is available? For example what does
QFile::exists(":/languages/arabic")
return, true or false?I get
true
If the file exists then the problem is with the actual loading, if the file is not found, then I guess there's a problem with the resource initialization.
After reviewing the project I discovered there are two "resources.qrc" the first one in the static library and the other in tester app. so I supposed this maybe makes Qt confused so I renamed one of them to a different name but unfortunately I still get same result.
-
@mrjj said:
QDirIterator it(":", QDirIterator::Subdirectories); while (it.hasNext()) { qDebug() << it.next(); }
to have a look how the files are named in the running exe to to verify
that they are indeed under ":/i18n" and paths are as you expect.I got this output:
Failed to load translation from static library ":/qt-project.org" ":/qt-project.org/qmime" ":/qt-project.org/qmime/freedesktop.org.xml" ":/qt-project.org/styles" ":/qt-project.org/styles/commonstyle" ":/qt-project.org/styles/commonstyle/images" ":/qt-project.org/styles/commonstyle/images/networkdrive-16.png" ":/qt-project.org/styles/commonstyle/images/networkdrive-32.png" ":/qt-project.org/styles/commonstyle/images/dvd-16.png" ":/qt-project.org/styles/commonstyle/images/cdr-16.png" ":/qt-project.org/styles/commonstyle/images/up-16.png" ":/qt-project.org/styles/commonstyle/images/up-32.png" ":/qt-project.org/styles/commonstyle/images/dvd-32.png" ":/qt-project.org/styles/commonstyle/images/cdr-32.png" ":/qt-project.org/styles/commonstyle/images/left-32.png" ":/qt-project.org/styles/commonstyle/images/stop-32.png" ":/qt-project.org/styles/commonstyle/images/file-16.png" ":/qt-project.org/styles/commonstyle/images/down-16.png" ":/qt-project.org/styles/commonstyle/images/stop-24.png" ":/qt-project.org/styles/commonstyle/images/left-16.png" ":/qt-project.org/styles/commonstyle/images/down-32.png" ":/qt-project.org/styles/commonstyle/images/file-32.png" ":/qt-project.org/styles/commonstyle/images/dirlink-32.png" ":/qt-project.org/styles/commonstyle/images/diropen-32.png" ":/qt-project.org/styles/commonstyle/images/diropen-16.png" ":/qt-project.org/styles/commonstyle/images/cleartext-16.png" ":/qt-project.org/styles/commonstyle/images/dirlink-16.png" ":/qt-project.org/styles/commonstyle/images/networkdrive-128.png" ":/qt-project.org/styles/commonstyle/images/media-volume-muted-16.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-cancel-128.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-open-128.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-apply-32.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-apply-16.png" ":/qt-project.org/styles/commonstyle/images/fonttruetype-16.png" ":/qt-project.org/styles/commonstyle/images/newdirectory-128.png" ":/qt-project.org/styles/commonstyle/images/filecontents-128.png" ":/qt-project.org/styles/commonstyle/images/floppy-128.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-delete-16.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-delete-32.png" ":/qt-project.org/styles/commonstyle/images/harddrive-128.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-clear-32.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-close-32.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-clear-16.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-close-16.png" ":/qt-project.org/styles/commonstyle/images/dirclosed-128.png" ":/qt-project.org/styles/commonstyle/images/trash-128.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-apply-128.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-yes-16.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-yes-32.png" ":/qt-project.org/styles/commonstyle/images/fontbitmap-16.png" ":/qt-project.org/styles/commonstyle/images/newdirectory-32.png" ":/qt-project.org/styles/commonstyle/images/dirclosed-32.png" ":/qt-project.org/styles/commonstyle/images/newdirectory-16.png" ":/qt-project.org/styles/commonstyle/images/dirclosed-16.png" ":/qt-project.org/styles/commonstyle/images/up-128.png" ":/qt-project.org/styles/commonstyle/images/media-pause-32.png" ":/qt-project.org/styles/commonstyle/images/media-pause-16.png" ":/qt-project.org/styles/commonstyle/images/cdr-128.png" ":/qt-project.org/styles/commonstyle/images/dvd-128.png" ":/qt-project.org/styles/commonstyle/images/media-volume-16.png" ":/qt-project.org/styles/commonstyle/images/harddrive-32.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-cancel-16.png" ":/qt-project.org/styles/commonstyle/images/harddrive-16.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-closetab-hover-16.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-cancel-32.png" ":/qt-project.org/styles/commonstyle/images/viewdetailed-128.png" ":/qt-project.org/styles/commonstyle/images/fusion_groupbox.png" ":/qt-project.org/styles/commonstyle/images/right-128.png" ":/qt-project.org/styles/commonstyle/images/fileinfo-16.png" ":/qt-project.org/styles/commonstyle/images/filelink-16.png" ":/qt-project.org/styles/commonstyle/images/filelink-32.png" ":/qt-project.org/styles/commonstyle/images/fileinfo-32.png" ":/qt-project.org/styles/commonstyle/images/dirlink-128.png" ":/qt-project.org/styles/commonstyle/images/media-seek-forward-16.png" ":/qt-project.org/styles/commonstyle/images/media-seek-forward-32.png" ":/qt-project.org/styles/commonstyle/images/diropen-128.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-help-128.png" ":/qt-project.org/styles/commonstyle/images/viewdetailed-16.png" ":/qt-project.org/styles/commonstyle/images/viewdetailed-32.png" ":/qt-project.org/styles/commonstyle/images/media-seek-backward-16.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-ok-128.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-no-128.png" ":/qt-project.org/styles/commonstyle/images/media-seek-backward-32.png" ":/qt-project.org/styles/commonstyle/images/refresh-32.png" ":/qt-project.org/styles/commonstyle/images/refresh-24.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-save-128.png" ":/qt-project.org/styles/commonstyle/images/viewlist-128.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-yes-128.png" ":/qt-project.org/styles/commonstyle/images/computer-32.png" ":/qt-project.org/styles/commonstyle/images/computer-16.png" ":/qt-project.org/styles/commonstyle/images/parentdir-32.png" ":/qt-project.org/styles/commonstyle/images/parentdir-16.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-closetab-16.png" ":/qt-project.org/styles/commonstyle/images/media-play-16.png" ":/qt-project.org/styles/commonstyle/images/desktop-16.png" ":/qt-project.org/styles/commonstyle/images/media-stop-16.png" ":/qt-project.org/styles/commonstyle/images/desktop-32.png" ":/qt-project.org/styles/commonstyle/images/media-play-32.png" ":/qt-project.org/styles/commonstyle/images/down-128.png" ":/qt-project.org/styles/commonstyle/images/media-stop-32.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-delete-128.png" ":/qt-project.org/styles/commonstyle/images/media-skip-backward-32.png" ":/qt-project.org/styles/commonstyle/images/media-skip-backward-16.png" ":/qt-project.org/styles/commonstyle/images/fusion_arrow.png" ":/qt-project.org/styles/commonstyle/images/viewlist-16.png" ":/qt-project.org/styles/commonstyle/images/viewlist-32.png" ":/qt-project.org/styles/commonstyle/images/file-128.png" ":/qt-project.org/styles/commonstyle/images/floppy-32.png" ":/qt-project.org/styles/commonstyle/images/left-128.png" ":/qt-project.org/styles/commonstyle/images/floppy-16.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-no-32.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-ok-32.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-no-16.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-ok-16.png" ":/qt-project.org/styles/commonstyle/images/media-skip-forward-32.png" ":/qt-project.org/styles/commonstyle/images/media-skip-forward-16.png" ":/qt-project.org/styles/commonstyle/images/filelink-128.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-open-16.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-open-32.png" ":/qt-project.org/styles/commonstyle/images/fileinfo-128.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-help-32.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-help-16.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-save-16.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-save-32.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-closetab-down-16.png" ":/qt-project.org/styles/commonstyle/images/parentdir-128.png" ":/qt-project.org/styles/commonstyle/images/filecontents-32.png" ":/qt-project.org/styles/commonstyle/images/trash-32.png" ":/qt-project.org/styles/commonstyle/images/filecontents-16.png" ":/qt-project.org/styles/commonstyle/images/trash-16.png" ":/qt-project.org/styles/commonstyle/images/right-16.png" ":/qt-project.org/styles/commonstyle/images/right-32.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-clear-128.png" ":/qt-project.org/styles/commonstyle/images/standardbutton-close-128.png" ":/qt-project.org/styles/macstyle" ":/qt-project.org/styles/macstyle/images" ":/qt-project.org/styles/macstyle/images/toolbar-ext.png" ":/qt-project.org/styles/macstyle/images/dockdock-down-16.png" ":/qt-project.org/styles/macstyle/images/closedock-16.png" ":/qt-project.org/styles/macstyle/images/dockdock-16.png" ":/qt-project.org/styles/macstyle/images/closedock-down-16.png" ":/qt-project.org/styles/macstyle/images/toolbar-ext@2x.png" ":/qt-project.org/qmessagebox" ":/qt-project.org/qmessagebox/images" ":/qt-project.org/qmessagebox/images/qtlogo-64.png" ":/i18n" ":/i18n/tester_ar.qm" ":/i18n/tester_de.qm" ":/i18n/tester_fr.qm" ":/i18n/tester_en.qm" ":/i18n/tester_ru.qm" ":/i18n/tester_it.qm" ":/languages" ":/languages/i18n" ":/languages/i18n/WidgetKeyboard_fr.qm" ":/languages/i18n/WidgetKeyboard_ar.qm" ":/languages/i18n/WidgetKeyboard_de.qm" ":/languages/i18n/WidgetKeyboard_en.qm" ":/languages/i18n/WidgetKeyboard_it.qm" ":/languages/i18n/WidgetKeyboard_ru.qm" ":/images" ":/images/cut" ":/images/copy" ":/images/logo" ":/images/paste" ":/images/normalEcho" ":/images/passwdEcho" ":/sounds" ":/sounds/click.wav" QSoundEffect(pulseaudio): Error decoding source
-
@kshegunov said:
And as a second suggestion: I think you should create another
QTranslator
object for the second translation file.Thanks. This fixed
Tester
app. problem but it didn't fix the main problem (loading translation from static library) I still get this error message:Failed to load translation from static library
-
Can I ask some silly maybe ?
The list shows
:/i18n/tester_ar.qmThere is no "arabic" under languages
there is
":/languages/i18n/WidgetKeyboard_ar.qm" -
@kshegunov said:
And have you tried the loading like this
translator->load("arabic", ":/languages");
, since you didn't comment on that?I tried to use
translator->load(":/languages/i18n/WidgetKeyboard_ar")
instead. The error message disappeared ("Failed to load translation from static library") but the widget comes from the static library still appears in English! -
Silly question number 2.
The test calls ui->retranslateUi(this);
void Tester::changeEvent(QEvent *event)
{
if (event->type() == QEvent::LanguageChange) {
ui->retranslateUi(this);
} else
QWidget::changeEvent(event);
}
But tester ui->retranslate() knows about the widgets from the library ?
retranslateUi() just call all ->setText for elements inserted on the UI.
How can it know about widgets in the Lib?
Who calls widget_in_lib->setText?