问题
I have a Qt Linguist *.ts file like:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="de_DE" sourcelanguage="en_GB">
<context>
<name></name>
<message>
<location filename="classWithStaticMethod.cpp" line="60"/>
<source>File</source>
<translation>Datei</translation>
</message>
</context>
</TS>
How do I enter a translation object with a static method.
ClassWithStaticMethod.cpp has a static method where a QT_TR_NOOP("File")
occurs at line 60 for instance. Leaving the name tags empty does nott work.
回答1:
Static variables are instantiated (and thus, constructor code run) before your int main
function is run. The translation code is set up in the QApplication
constructor (I believe), which isn't run until your int main function has been entered. Thus, you are trying to get the translation of a string before the code to support it has been initialized.
To avoid this, you could either accept that the given string isn't translated and explicitly translate it every time it is used, or use the Construct on First Use idiom instead of a static member variable.
来源:https://stackoverflow.com/questions/33482054/how-to-enter-a-static-methods-for-qt-linguist-qtranslator