Qt Translation returns the same string instead of translation
qt
Solution
It's not clear what is `instance()` in this line
this->currentTranslator = new QTranslator(instance());
But anyway, here's some things to check:
- Do you have something like this in `YourProjectName.pro` ?
RESOURCES += \
resources.qrc
(`resources.qrc` is the name I usually use. Replace with whatever name you choose.)
- Did you check the prefixes in your `resources.qrc` ? Are you sure there's a `translation/` prefix in there? Something like this should also work:
<RCC>
<qresource prefix="/">
<file>translation/YourProjectName_en.qm</file>
<!-- other resources here -->
</qresource>
</RCC>
provided that you have `YourProjectName_en.qm` in a `translation/` subdirectory.
This is what Works for Me™:
in `main.cpp`:
QApplication app(argc, argv);
QApplication::setApplicationName("MyApplication");
QApplication::setApplicationVersion("4.4");
QApplication::setOrganizationName("FUBAR");
QApplication::setOrganizationDomain("www.fu.bar");
QTranslator translator;
translator.load(app.applicationName() + "_" + QLocale::system().name(), ":/ts");
app.installTranslator(&translator);
in `resources.qrc`:
<RCC>
<qresource prefix="/">
<file>resources/pixmaps/exit.png</file>
<!-- ... -->
<file>ts/MyApplication_en.qm</file>
<file>ts/MyApplication_es.qm</file>
</qresource>
</RCC>
in `MyApplication.pro`:
TRANSLATIONS += \
ts/MyApplication_en.ts \
ts/MyApplication_es.ts
RESOURCES += \
resources.qrc
win32 {
RC_FILE = win32.rc
}
OTHER_FILES += \
win32.rc \
TODO.txt
And here's a brief description of the project tree:
MyApplication
├── resources
│ ├── pixmaps
│ └── ...
├── (sources are here)
├── MyApplication.pro
├── resources.qrc
├── TODO.txt
├── ts
│ ├── MyApplication_en.qm
│ ├── MyApplication_en.ts
│ ├── MyApplication_es.qm
│ └── MyApplication_es.ts
└── win32.rc
This is a very small project, sources are mixed with a few artifacts.
Edit:
I think you should try passing the filename and directory parts separated to the load method. In short, change this line
if (this->currentTranslator->load(":/translation/myApp_en.qm"))
to this
if (this->currentTranslator->load("myApp_en.qm", ":/translation/"))
Problem
I am experiencing a strange Qt Translation problem. Due to reasons I cannot change that involve legacy databases of translation tables, our 'natural language' is 'Enums'. ``` QString myString = tr("OUR_APP_MY_STRING"); ``` I have a script that builds the *.TS files out of our databases to be used by Qt. The entry in the *.TS files for english looks like this: ``` <message> <source>OUR_APP_MY_STRING</source> <translation>My String</translation> </message> ``` The *.TS file loads fine into Qt Linguist. Everything there looks fine. "OUR_APP_MY_STRING" is found and its "English Translation" looks ok. The QT project file has the *.TS files in the TRANSLATION sections I am using lRelease to generate the .QM files and putting them in the application's resources (.qrc) file. In my application's setup function (called by main() after construction) I have the following code: ``` // initialize translator this->currentTranslator = new QTranslator(instance()); if (this->currentTranslator->load(":/translation/myApp_en.qm")) { this->installTranslator(this->currentTranslator); QString test = tr("OUR_APP_MY_STRING"); // <<----- problem. output is always "OUR_APP_MY_STRING" } ``` Any ideas?