Results 1 to 9 of 9

Thread: How to do internationalization of dynamic text?

  1. #1
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question How to do internationalization of dynamic text?

    I have gone through the "internationalization" section of Qt documentation and i know that i can use QTranslator to translate user visible text.

    Now, the problem is that i am getting most of the visible data from a database and it's already coming in different languages (korean, chinese etc.) but it's showing as junk characters in the Qt application.

    I am using a QTableView with MVC (Model View Architecture). I get data from the database in the form of QStringList and when i print the data on the terminal, it properly shows in the language (korean, chinese etc.) but in the GUI, it shows junk characters.

    Anybody has an idea about how to proceed on this...

  2. #2
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to do internationalization of dynamic text?

    Hi,
    It would help if you could post a piece of code in the area in which you are trying to display the text.
    You said the text is already translated, so maybe this thread http://www.qtcentre.org/forum/f-qt-p...ers-20255.html may be related to your problem?

  3. The following user says thank you to QbelcorT for this useful post:

    montylee (16th April 2009)

  4. #3
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to do internationalization of dynamic text?

    My problem is a generic one so that's the reason i didn't post the code.
    I just have QStrings in a QStringList and i pass that QStringList to QAbstractTableModel to display it in QTableView.
    I also display the strings in a terminal. On the terminal it shows up properly but in the GUI QTableView, it shows as junk characters.

  5. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to do internationalization of dynamic text?

    Seems to be an encoding issue.
    Are you running linux?
    What settings are your relevant environment variables (LANG, LC_xxxx etc)?
    What locale is Qt set to?

  6. #5
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Re: How to do internationalization of dynamic text?

    Quote Originally Posted by caduel View Post
    Seems to be an encoding issue.
    Are you running linux?
    What settings are your relevant environment variables (LANG, LC_xxxx etc)?
    What locale is Qt set to?
    Yes, it seems to be an encoding issue. I am using Linux with Qt 4.4.
    I found the problem in my code. I was using toUtf8() function to get the copy the string from QString to another QString. So, this is the existing code:

    Qt Code:
    1. QString temp = qsOriginalStr.toUtf8();
    To copy to clipboard, switch view to plain text mode 

    Now, if i change the above code to:
    Qt Code:
    1. QString temp = trUtf8(qsOriginalStr);
    To copy to clipboard, switch view to plain text mode 

    It works properly.

    I have one simple query:
    trUtf8() is supposed to take "char *" argument and return a QString but here i am giving it a QString as argument. Is it ok to do so?

  7. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to do internationalization of dynamic text?

    Wasn't trUtf8() introduced with Qt4.5?

    I don't quite see why you don't just copy it by assigning (assuming qsOriginalStr is really a meaningful QString and not just some byte array that was wrongly de/encoded.)
    Qt Code:
    1. QString temp = qsOriginalStr;
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. QString temp = qsOriginalStr.toUtf8();
    To copy to clipboard, switch view to plain text mode 
    This code will produce a QByteArray with the UTF-8 encoding of your string. Those bytes will then (by the default encoding, whatever that might be in your case) converted back into a string. Quite plausible that this will not get you what you want.

    Qt Code:
    1. QString temp = trUtf8(qsOriginalStr);
    To copy to clipboard, switch view to plain text mode 
    To be honest, I don't see how that should compile either.
    Looking at the sources trUtf8 wants a char* (no overloads for QString; at least not in 4.5.0; and also no implicit QString -> char* casts.)

    HTH

  8. #7
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to do internationalization of dynamic text?

    Quote Originally Posted by caduel View Post
    Wasn't trUtf8() introduced with Qt4.5?

    I don't quite see why you don't just copy it by assigning (assuming qsOriginalStr is really a meaningful QString and not just some byte array that was wrongly de/encoded.)
    Qt Code:
    1. QString temp = qsOriginalStr;
    To copy to clipboard, switch view to plain text mode 
    trUtf8() is in Qt 4.4 too. If i assign qsOriginalStr directly, it gives me junk characters, so i have to use trUtf8() to get the proper encoded string.

    Quote Originally Posted by caduel View Post
    Qt Code:
    1. QString temp = qsOriginalStr.toUtf8();
    To copy to clipboard, switch view to plain text mode 
    This code will produce a QByteArray with the UTF-8 encoding of your string. Those bytes will then (by the default encoding, whatever that might be in your case) converted back into a string. Quite plausible that this will not get you what you want.
    This was the existing code which wasn't written by me.
    Quote Originally Posted by caduel View Post
    Qt Code:
    1. QString temp = trUtf8(qsOriginalStr);
    To copy to clipboard, switch view to plain text mode 
    To be honest, I don't see how that should compile either.
    Looking at the sources trUtf8 wants a char* (no overloads for QString; at least not in 4.5.0; and also no implicit QString -> char* casts.)
    I don't get any compilation errors so i think it implicitly converts the QString to char *
    Should i do like this:

    Qt Code:
    1. QString temp = trUtf8(qsOriginalStr.toLatin1().data());
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to do internationalization of dynamic text?

    Qt Code:
    1. QString temp = trUtf8(qsOriginalStr.toLatin1().data());
    To copy to clipboard, switch view to plain text mode 
    well, it does not really make sense to convert a string into the Latin1 encoding and then tell trUtf8 to pretend that this really is UTF8 encoded data...

    The original "string" should be either not a QString but only a QByteArray, or it should already be (as QStrings are) a unicode encoded string and no conversions should be needed.
    Where does qsOriginalStr come from?

  10. #9
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to do internationalization of dynamic text?

    Quote Originally Posted by caduel View Post
    Qt Code:
    1. QString temp = trUtf8(qsOriginalStr.toLatin1().data());
    To copy to clipboard, switch view to plain text mode 
    well, it does not really make sense to convert a string into the Latin1 encoding and then tell trUtf8 to pretend that this really is UTF8 encoded data...

    The original "string" should be either not a QString but only a QByteArray, or it should already be (as QStrings are) a unicode encoded string and no conversions should be needed.
    Where does qsOriginalStr come from?
    qsOriginalStr comes from mysql database. Here's the code:

    Qt Code:
    1. MYSQL_ROW queryRow;
    2. int queryFields = mysql_num_fields(pQueryResult);
    3.  
    4.  
    5. while( (queryRow = mysql_fetch_row(pQueryResult)) != NULL )
    6. {
    7. printf("cSqlClient::cSqlQuery:next row\n");
    8. for( i=0; i < queryFields; i++ )
    9. {
    10. qsOriginalStr = QString(queryRow[i]);
    To copy to clipboard, switch view to plain text mode 

    This is the existing written by someone else. I think i can just replace
    Qt Code:
    1. qsOriginalStr = QString(queryRow[i]);
    To copy to clipboard, switch view to plain text mode 
    by
    Qt Code:
    1. qsOriginalStr = (char *)(queryRow[i]);
    To copy to clipboard, switch view to plain text mode 
    and use trUtf8 directly on the (char *)

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Replies: 0
    Last Post: 10th April 2009, 15:28
  3. Designer plugin & dynamic value list for text input
    By janus in forum Qt Programming
    Replies: 0
    Last Post: 4th April 2009, 12:41
  4. Match the text beetween two string
    By dreamer in forum Qt Programming
    Replies: 4
    Last Post: 20th May 2008, 14:48
  5. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.