May 4, 2012

ronM71 ronM71
Lab Rat
225 posts

[ Resolved ] QTranslator and UTF-8 combo give me trouble

 

(Using Qt 4.7.4 on Mac)

My *.TS files are generated and are UTF-8 using a perl script I wrote. Long story with legacy dependency. We have 13 languages we have to support including some exotic ones. Generating *.QM files out of them seems to work well. You can load the *.QM file into Qt Linguist and everything looks healthy.

Due to legacy reasons, my system langauge is “Enums”. That is, I do something like this:

  1. QString label = tr("APP_STRINGS_MYLABEL_TEXT"); // this translates to 13 langauges...

so, an excerpt from the english .TS file for instance reads:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE TS>
  3. <TS version="2.0" language="en">
  4. <defaultcodec>UTF-8</defaultcodec>
  5. <context>
  6.   <name>MyApp</name>
  7.     <message>
  8.       <source>APP_STRINGS_MYLABEL_TEXT/source>
  9.       <translation>My Label Text In English</translation>
  10.     </message>
  11. ..
  12. ..
  13. ..
  14. </context>

My project file has the following:

  1. CODECFORTR = UTF-8

My main method has test code that looks like this:

  1. QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
  2. QTranslator translator;
  3. bool empty = translator.isEmpty(); // Returns TRUE
  4. bool loaded = translator.load("myApp_en","/Users/user/Dev/myApp/translation");
  5. empty = translator.isEmpty(); // returns FALSE
  6.  
  7. // install the translator.
  8. MyApp->installTranslator(&translator);
  9.  
  10. // Here comes my problem...
  11. QString test = MyApp->trUtf8("APP_STRINGS_MYLABEL_TEXT"); // returns "APP_STRINGS_MYLABEL_TEXT"
  12. QString test2 = MyApp->tr("APP_STRINGS_MYLABEL_TEXT"); // returns "APP_STRINGS_MYLABEL_TEXT"

As you can see, all my strings are being translated back to their original values. No real translation takes place. If you have any idea why thins might be happening, i’d appreciate the assistance. I am at a loss right now.

1 reply

May 7, 2012

ronM71 ronM71
Lab Rat
225 posts

The culprit was the context. Since I am using generated strings from a 3rd party source, the context was always wrong. As soon as I started using the “MyApp” context explicitly in the translate API it started working.

thanks to all those who responded.

 
  ‹‹ Combine floating QDockWidgets and into a single floating window with tabs?      painter.drawText() crashes under OSX Lion - not under Leopard ››

You must log in to post a reply. Not a member yet? Register here!