Translations aren't working
-
I'm sure I'm doing something wrong, but for the life of me I can't figure out what it is.
I have a CMake project where I'm creating and adding TS files with
qt5_create_translation
andqt5_add_translation
. Here's how myqt5_create_translation
call looks:qt5_create_translation(QM_FILES ${SOURCES} ${TS_FILES})
The TS files seem to generate properly and I can translate as normal in Qt Linguist. I then include them in the build with a resource file that looks like this:
<RCC> <qresource prefix="/ts"> <file>en_GB.qm</file> </qresource> </RCC>
The TS files seem to release and compile correctly. I get output that looks like this, which seems correct:
Updating '/Users/thsprppr/app/ts/en_GB.qm'... Generated 5 translation(s) (3 finished and 2 unfinished) Ignored 842 untranslated source text(s)
The application can also see the file exists if I call
QFileInfo::exists(":/ts/en_GB.qm")
(returnstrue
) andQTranslate::language()
even correctly determines the locale isen_GB
(unless it's just getting that from the filename.But no matter which way I try to load it, none of the lines seem to translate. I've read dozens of threads and tried everything I could find but nothing seems to work.
Here's the code I've got in
main()
QApplication* a = new QApplication(argc, argv); QTranslator t; qDebug() << t.load(":/ts/en_GB.qm"); qDebug() << QCoreApplication::installTranslator(&t);
This is before any GUI objects are created and immediately after the QApplication process is created. All UI strings are wrapped in
tr()
. Both of thoseqDebug()
lines returntrue
, and this is inmain()
so the QTranslator shouldn't be going out of scope. CallingQTranslator::language()
even correctly returns the language I set for this file (unless it's just getting that from the filename). I also made sure to add the Q_OBJECT macro to any objects that I was warned didn't already have it. Yet none of my text is being translated whatsoever.Since neither of those calls are providing errors, my current guess is the lines just aren't being matched somehow? I'm not sure if my
qt5_create_translation
line is correct, but it is as far as I can tell.I really have no idea what's going on. Any help would be very much appreciated.
-
I'm sure I'm doing something wrong, but for the life of me I can't figure out what it is.
I have a CMake project where I'm creating and adding TS files with
qt5_create_translation
andqt5_add_translation
. Here's how myqt5_create_translation
call looks:qt5_create_translation(QM_FILES ${SOURCES} ${TS_FILES})
The TS files seem to generate properly and I can translate as normal in Qt Linguist. I then include them in the build with a resource file that looks like this:
<RCC> <qresource prefix="/ts"> <file>en_GB.qm</file> </qresource> </RCC>
The TS files seem to release and compile correctly. I get output that looks like this, which seems correct:
Updating '/Users/thsprppr/app/ts/en_GB.qm'... Generated 5 translation(s) (3 finished and 2 unfinished) Ignored 842 untranslated source text(s)
The application can also see the file exists if I call
QFileInfo::exists(":/ts/en_GB.qm")
(returnstrue
) andQTranslate::language()
even correctly determines the locale isen_GB
(unless it's just getting that from the filename.But no matter which way I try to load it, none of the lines seem to translate. I've read dozens of threads and tried everything I could find but nothing seems to work.
Here's the code I've got in
main()
QApplication* a = new QApplication(argc, argv); QTranslator t; qDebug() << t.load(":/ts/en_GB.qm"); qDebug() << QCoreApplication::installTranslator(&t);
This is before any GUI objects are created and immediately after the QApplication process is created. All UI strings are wrapped in
tr()
. Both of thoseqDebug()
lines returntrue
, and this is inmain()
so the QTranslator shouldn't be going out of scope. CallingQTranslator::language()
even correctly returns the language I set for this file (unless it's just getting that from the filename). I also made sure to add the Q_OBJECT macro to any objects that I was warned didn't already have it. Yet none of my text is being translated whatsoever.Since neither of those calls are providing errors, my current guess is the lines just aren't being matched somehow? I'm not sure if my
qt5_create_translation
line is correct, but it is as far as I can tell.I really have no idea what's going on. Any help would be very much appreciated.
hi @thsprppr
have you triedQTranslator t; qDebug() << t.load("en_GB.qm", ":/ts/");
does this return true?
also, are you sure your QTranslator instance does not go out of scope? I think, not, this seems to be inside main, but one never knows ;) -
@thsprppr said in Translations aren't working:
Here's the code I've got in main()
QApplication* a = new QApplication(argc, argv);
QTranslator t;
qDebug() << t.load(":/ts/en_GB.qm");
qDebug() << QCoreApplication::installTranslator(&t);Why are you doing
QApplication* a = new QApplication(argc, argv);
?
Don't make sense to me.
Is this youmain()
code? -
hi @thsprppr
have you triedQTranslator t; qDebug() << t.load("en_GB.qm", ":/ts/");
does this return true?
also, are you sure your QTranslator instance does not go out of scope? I think, not, this seems to be inside main, but one never knows ;)@J-Hilk said in Translations aren't working:
hi @thsprppr
have you triedQTranslator t; qDebug() << t.load("en_GB.qm", ":/ts/");
does this return true?
also, are you sure your QTranslator instance does not go out of scope? I think, not, this seems to be inside main, but one never knows ;)Yes, I mention in the OP that both
qDebug()
s return true and that this is indeed insidemain()
. I also triedQTranslator* t = new QTranslator();
just to test it and it made no difference.@KroMignon said in Translations aren't working:
@thsprppr said in Translations aren't working:
Here's the code I've got in main()
QApplication* a = new QApplication(argc, argv);
QTranslator t;
qDebug() << t.load(":/ts/en_GB.qm");
qDebug() << QCoreApplication::installTranslator(&t);Why are you doing
QApplication* a = new QApplication(argc, argv);
?
Don't make sense to me.
Is this youmain()
code?Yes, I mention in the OP that this is in
main()
. I'm usingnew
because my application has a headless mode that creates anew QCoreApplication
, but I removed it for clarity. If it matters to you, this is the full snippet:QCoreApplication* a; if (!headless) { a = new QApplication(argc, argv); } else { a = new QCoreApplication(argc, argv); } QTranslator t; qDebug() << t.load(":/ts/en_US"); qDebug() << QCoreApplication::installTranslator(&t);
-
@J-Hilk said in Translations aren't working:
hi @thsprppr
have you triedQTranslator t; qDebug() << t.load("en_GB.qm", ":/ts/");
does this return true?
also, are you sure your QTranslator instance does not go out of scope? I think, not, this seems to be inside main, but one never knows ;)Yes, I mention in the OP that both
qDebug()
s return true and that this is indeed insidemain()
. I also triedQTranslator* t = new QTranslator();
just to test it and it made no difference.@KroMignon said in Translations aren't working:
@thsprppr said in Translations aren't working:
Here's the code I've got in main()
QApplication* a = new QApplication(argc, argv);
QTranslator t;
qDebug() << t.load(":/ts/en_GB.qm");
qDebug() << QCoreApplication::installTranslator(&t);Why are you doing
QApplication* a = new QApplication(argc, argv);
?
Don't make sense to me.
Is this youmain()
code?Yes, I mention in the OP that this is in
main()
. I'm usingnew
because my application has a headless mode that creates anew QCoreApplication
, but I removed it for clarity. If it matters to you, this is the full snippet:QCoreApplication* a; if (!headless) { a = new QApplication(argc, argv); } else { a = new QCoreApplication(argc, argv); } QTranslator t; qDebug() << t.load(":/ts/en_US"); qDebug() << QCoreApplication::installTranslator(&t);
-
@J-Hilk said in Translations aren't working:
hi @thsprppr
have you triedQTranslator t; qDebug() << t.load("en_GB.qm", ":/ts/");
does this return true?
also, are you sure your QTranslator instance does not go out of scope? I think, not, this seems to be inside main, but one never knows ;)Yes, I mention in the OP that both
qDebug()
s return true and that this is indeed insidemain()
. I also triedQTranslator* t = new QTranslator();
just to test it and it made no difference.@KroMignon said in Translations aren't working:
@thsprppr said in Translations aren't working:
Here's the code I've got in main()
QApplication* a = new QApplication(argc, argv);
QTranslator t;
qDebug() << t.load(":/ts/en_GB.qm");
qDebug() << QCoreApplication::installTranslator(&t);Why are you doing
QApplication* a = new QApplication(argc, argv);
?
Don't make sense to me.
Is this youmain()
code?Yes, I mention in the OP that this is in
main()
. I'm usingnew
because my application has a headless mode that creates anew QCoreApplication
, but I removed it for clarity. If it matters to you, this is the full snippet:QCoreApplication* a; if (!headless) { a = new QApplication(argc, argv); } else { a = new QCoreApplication(argc, argv); } QTranslator t; qDebug() << t.load(":/ts/en_US"); qDebug() << QCoreApplication::installTranslator(&t);
@thsprppr said in Translations aren't working:
QTranslator t;
qDebug() << t.load(":/ts/en_US");
qDebug() << QCoreApplication::installTranslator(&t);According to documenation:
Usually, it is better to use the QTranslator::load(const QLocale &, const QString &, const QString &, const QString &, const QString &) function instead, because it uses QLocale::uiLanguages() and not simply the locale name, which refers to the formatting of dates and numbers and not necessarily the UI language.
I think you should change the load to:
QTranslator t; qDebug() << t.load(QLocale(), ":/ts/en_US"); qDebug() << QCoreApplication::installTranslator(&t);
-
@thsprppr ok, what platform are you targeting?
IIRC some platforms do not support loading translation files from resources, for what ever reasons.@J-Hilk So far I've tried this on 5.15 on macOS and 5.12 on Linux, both seem to have the same issue.
It there something else I'm supposed to do in Linguist? I've been setting them to "complete" (ctrl+enter) and
lrelease
counts them as "finished", but is there something else I could have missed?@KroMignon said in Translations aren't working:
@thsprppr said in Translations aren't working:
QTranslator t;
qDebug() << t.load(":/ts/en_US");
qDebug() << QCoreApplication::installTranslator(&t);According to documenation:
Usually, it is better to use the QTranslator::load(const QLocale &, const QString &, const QString &, const QString &, const QString &) function instead, because it uses QLocale::uiLanguages() and not simply the locale name, which refers to the formatting of dates and numbers and not necessarily the UI language.
I think you should change the load to:
QTranslator t; qDebug() << t.load(QLocale(), ":/ts/en_US"); qDebug() << QCoreApplication::installTranslator(&t);
I switched it to
t.load(QLocale(), ":/ts/en_US");
and bothqDebug()
s came outfalse
and nothing was translated.I also tried
t->load(QLocale::system(), QString(), QString(), ":/ts");
as an experiment (my system locale isen_US
), and they were bothtrue
again, but still didn't work. -
@J-Hilk So far I've tried this on 5.15 on macOS and 5.12 on Linux, both seem to have the same issue.
It there something else I'm supposed to do in Linguist? I've been setting them to "complete" (ctrl+enter) and
lrelease
counts them as "finished", but is there something else I could have missed?@KroMignon said in Translations aren't working:
@thsprppr said in Translations aren't working:
QTranslator t;
qDebug() << t.load(":/ts/en_US");
qDebug() << QCoreApplication::installTranslator(&t);According to documenation:
Usually, it is better to use the QTranslator::load(const QLocale &, const QString &, const QString &, const QString &, const QString &) function instead, because it uses QLocale::uiLanguages() and not simply the locale name, which refers to the formatting of dates and numbers and not necessarily the UI language.
I think you should change the load to:
QTranslator t; qDebug() << t.load(QLocale(), ":/ts/en_US"); qDebug() << QCoreApplication::installTranslator(&t);
I switched it to
t.load(QLocale(), ":/ts/en_US");
and bothqDebug()
s came outfalse
and nothing was translated.I also tried
t->load(QLocale::system(), QString(), QString(), ":/ts");
as an experiment (my system locale isen_US
), and they were bothtrue
again, but still didn't work.@thsprppr said in Translations aren't working:
So far I've tried this on 5.15 on macOS and 5.12 on Linux, both seem to have the same issue.
I don't know about Linux, but MacOS and iOS are 2 systems, where I explicitly pack those translation files into the app bundle and load them from there
APP_Language.files = $$PWD/translations/myApp_de.qm \ $$PWD/translations/myApp_en.qm APP_Language.path = Contents/MacOS/translations QMAKE_BUNDLE_DATA += APP_Language
-
@thsprppr said in Translations aren't working:
So far I've tried this on 5.15 on macOS and 5.12 on Linux, both seem to have the same issue.
I don't know about Linux, but MacOS and iOS are 2 systems, where I explicitly pack those translation files into the app bundle and load them from there
APP_Language.files = $$PWD/translations/myApp_de.qm \ $$PWD/translations/myApp_en.qm APP_Language.path = Contents/MacOS/translations QMAKE_BUNDLE_DATA += APP_Language
I've tried loading QM files from the file system too with no luck unfortunately.
I did discover something interesting though. In a brand new Qt Widgets project on CMake, the following code works (in the MainWindow constructor before constructing any widgets):
QTranslator* t = new QTranslator(this); qDebug() << t->load("/Users/thsprppr/translation.qm"); qDebug() << QCoreApplication::installTranslator(t);
Yet that same exact code (with a different QM file) in the same place (MainWindow constructor) in my other project does not. There must be something in my code that's interfering with the translation.
-
I've tried loading QM files from the file system too with no luck unfortunately.
I did discover something interesting though. In a brand new Qt Widgets project on CMake, the following code works (in the MainWindow constructor before constructing any widgets):
QTranslator* t = new QTranslator(this); qDebug() << t->load("/Users/thsprppr/translation.qm"); qDebug() << QCoreApplication::installTranslator(t);
Yet that same exact code (with a different QM file) in the same place (MainWindow constructor) in my other project does not. There must be something in my code that's interfering with the translation.
-
You too should learn about object life time in c++
will make your life much easier in the future :D