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...
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?
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.
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?
Re: How to do internationalization of dynamic text?
Quote:
Originally Posted by
caduel
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:
Code:
QString temp
= qsOriginalStr.
toUtf8();
Now, if i change the above code to:
Code:
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?
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.)
Code:
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.
Code:
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
Re: How to do internationalization of dynamic text?
Quote:
Originally Posted by
caduel
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.)
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
Code:
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.
Quote:
Originally Posted by
caduel
Code:
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:
Code:
QString temp
= trUtf8
(qsOriginalStr.
toLatin1().
data());
Re: How to do internationalization of dynamic text?
Code:
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?
Re: How to do internationalization of dynamic text?
Quote:
Originally Posted by
caduel
Code:
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:
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
Code:
qsOriginalStr
= QString(queryRow
[i
]);
by
Code:
qsOriginalStr = (char *)(queryRow[i]);
and use trUtf8 directly on the (char *)