PDA

View Full Version : How to do internationalization of dynamic text?



montylee
15th April 2009, 18:29
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...

QbelcorT
16th April 2009, 00:00
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-programming-2/t-problem-with-accented-characters-20255.html may be related to your problem?

montylee
16th April 2009, 03:15
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.

caduel
16th April 2009, 07:26
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?

montylee
16th April 2009, 19:30
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:


QString temp = qsOriginalStr.toUtf8();

Now, if i change the above code to:

QString temp = trUtf8(qsOriginalStr);

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?

caduel
17th April 2009, 07:40
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.)

QString temp = qsOriginalStr;



QString temp = qsOriginalStr.toUtf8();
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.


QString temp = trUtf8(qsOriginalStr);
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

montylee
17th April 2009, 17:58
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.)

QString temp = qsOriginalStr;
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.




QString temp = qsOriginalStr.toUtf8();
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.



QString temp = trUtf8(qsOriginalStr);
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:


QString temp = trUtf8(qsOriginalStr.toLatin1().data());

caduel
17th April 2009, 18:45
QString temp = trUtf8(qsOriginalStr.toLatin1().data());
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?

montylee
17th April 2009, 22:00
QString temp = trUtf8(qsOriginalStr.toLatin1().data());
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:



MYSQL_ROW queryRow;
int queryFields = mysql_num_fields(pQueryResult);


while( (queryRow = mysql_fetch_row(pQueryResult)) != NULL )
{
printf("cSqlClient::cSqlQuery:next row\n");
for( i=0; i < queryFields; i++ )
{
qsOriginalStr = QString(queryRow[i]);


This is the existing written by someone else. I think i can just replace
qsOriginalStr = QString(queryRow[i]); by
qsOriginalStr = (char *)(queryRow[i]); and use trUtf8 directly on the (char *)