Reputation: 83
In a Qt 5.3 application, I have a string literal that contains non-ASCII characters (specifically German Umlauts) that will need to be translated into foreign languages. So I have two issues: (1) I have to mark that literal with tr() and (2) I have to display the string correctly on the screen for which I would seem to have to use QString::fromLatin1() or some such function.
If I do
QString s = tr("ä");
the string is marked for translation but will not display right.
If I do
QString r = QString::fromLatin1("ä");
the string will display right but will not be marked for translation.
How can I combine the two into one? And yes, my source file is saved in UTF8 encoding.
I've been searching up and down the forums and none of the hints work; mainly because most of the solutions apply to Qt 4.8 and have been removed or depreciated for Qt 5.3. Thank you for your help!!
PS: I'm developing using Visual Studio 2010 on Windows 8. According to VS2010 and Notepad++ my sources are saved in UTF8 with BOM encoding.
Upvotes: 4
Views: 2330
Reputation: 3180
If using QString::fromLatin1("ä")
you get a correct output then your source files haven't UTF-8 encoding.
When source file
printf("%x\n", QString("ä").at(0).unicode());
printf("%x\n", QString::fromLatin1("ä").at(0).unicode());
has UTF-8 encoding, then output is
e4
c3
but when Latin1 (ISO-8859-1), then
fffd
e4
e4
is the Unicode code of the letter ä
(U+00E4)
Upvotes: 4
Reputation: 37512
Read documentation of trUtf8 (deprecated/obsolete in Qt5).
So you don't have to use this function, just set proper default codec. Add i main this line:
QTextCodec::setCodecForTr("UTF-8");
If you prefer avoid changing default codec just use trUtf8
instead of tr
.
Upvotes: -1