Results 1 to 9 of 9

Thread: How to do internationalization of dynamic text?

Hybrid View

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

    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?

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

    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?

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

    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

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

    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 

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

    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?

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

    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
  •  
Qt is a trademark of The Qt Company.