Hi all,
I have a problem for dynamic translation. I am making a log system for an application and it must be international.
I have 2 kinds of log messages, with and without parameter.
Without parameter, the use of tr() fits perfectly.
But with parameters i keep having the original untranslated string, whereas the lupdate and lrelease worked perfectly (neither warning nor error).
Here is a sample of the code :
// The messages are stored in the constructor of the object (a singleton class)
Log::Log (){
// _message is a map where the strings are stored, declared in the .hpp file
map<unsigned int, QString>::iterator it = _message.begin();
_message.
insert(it,pair<unsigned
int,QString>
(1,
QObject::tr(" Missing parameter ")));
...
_message.insert(it,pair<unsigned int,QString>(51,QT_TR_NOOP(" Example message with %1")));
_message.insert(it,pair<unsigned int,QString>(53,QT_TR_NOOP(" Message2 with %1 and %2")));
...
}
// The messages are stored in the constructor of the object (a singleton class)
Log::Log (){
// _message is a map where the strings are stored, declared in the .hpp file
map<unsigned int, QString>::iterator it = _message.begin();
_message.insert(it,pair<unsigned int,QString>(1,QObject::tr(" Missing parameter ")));
...
_message.insert(it,pair<unsigned int,QString>(51,QT_TR_NOOP(" Example message with %1")));
_message.insert(it,pair<unsigned int,QString>(53,QT_TR_NOOP(" Message2 with %1 and %2")));
...
}
To copy to clipboard, switch view to plain text mode
In the same class I have a convert(int,list<QString>) function that takes a number and possible parameters and it returns the corresponding string :
QString Log::convert(int numMsg, list<QString> param
) { map<unsigned int,QString>::iterator it = _message.begin ();
// If there are parameters
switch (param.size ()){
case 0 :
return _message.find (numMsg); // Returning the corresponding string
break;
case 1 :
// Function that finds the corresponding message with 1 parameter
return fill1(param,it);
break;
case 2 :
....
}
}
QString Log::convert(int numMsg, list<QString> param) {
map<unsigned int,QString>::iterator it = _message.begin ();
// If there are parameters
switch (param.size ()){
case 0 :
return _message.find (numMsg); // Returning the corresponding string
break;
case 1 :
// Function that finds the corresponding message with 1 parameter
return fill1(param,it);
break;
case 2 :
....
}
}
To copy to clipboard, switch view to plain text mode
And the fill1() function that is in the Log class too and that must perform the translation :
QString Log::fill1 (list<QString> param, map<unsigned
int,QString>
::iterator it
) { list<QString>::iterator iter = param.begin ();
return QObject::tr ((*it
).
second.
toLatin1 ()).
arg(*iter
);
}
QString Log::fill1 (list<QString> param, map<unsigned int,QString>::iterator it) {
list<QString>::iterator iter = param.begin ();
return QObject::tr ((*it).second.toLatin1 ()).arg(*iter);
}
To copy to clipboard, switch view to plain text mode
It works fine with the english version, but when i load a translator, the strings translated using the QT_TR_NOOP macro are still in english.
Assuming i have neither warning nor error in QLinguist, and the number of translated sentence with lrelease corresponds exactly to the number of sentence i have, does anyone have an idea why the right sentences are not found ?
I used a local map in the fill1 () function to try but it remained the same, i tried QT_TRANSLATE_NOOP () with context but nothing changed.
.
Bookmarks