4

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.

  • 1
    Probably because `tr` is equivalent (if not `#define`'d) to `QString::fromUtf8`. The translation tool is pretty stupid and just looks for `tr("...")`; it would be fooled by `#undef tr, #define tr(x) QString::fromLatin1(x)` – MSalters Aug 19 '14 at 13:07

2 Answers2

4

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)

Dmitry Sokolov
  • 3,118
  • 1
  • 30
  • 35
  • Thank you for that. The output is indeed what you describe as Latin1 encoding (fffd and e4) but every text editor I use to look at my file tells me it is encoded as UTF-8. I don't know what to do now ...? – Patrick Bangert Aug 19 '14 at 14:24
  • Try to open a source file in a text editor (Qt Creator, NPP, ...), select right encoding that will display strings correctly and convert encoding of this source file to UTF-8 with BOM. – Dmitry Sokolov Aug 19 '14 at 18:14
  • I used several text editors (among them Qt Creator and NPP) to convert the file into UTF-8 and into Latin1 and run your code. In both encodings, I get the result "fffd\ne4". Therefore, the test you suggest does not seem to work. – Patrick Bangert Aug 20 '14 at 04:44
  • Please add in your question what system, toolchain and etc. you use. – Dmitry Sokolov Aug 20 '14 at 14:21
  • I added to my question that I'm using VS2010 on Windows 8. Thank you for your help! – Patrick Bangert Aug 21 '14 at 08:14
-1

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.

Marek R
  • 32,568
  • 6
  • 55
  • 140