PDA

View Full Version : Switch QTranslator at runtime?



whitefurrows
28th October 2006, 23:41
How can i change QTranslator dynamic? I try this:


int main(int argc, char** argv)
{
QApplication app( argc, argv );

//...

if (language=="de"){
QTranslator* translator = new QTranslator();
translator->load(":/translation/language + ".qm");
app.installTranslator(translator);
}

//...
}

__________________________________________________ _________________________


void ApplicationSetup::switchLanguage()
{
static QTranslator* translator=0;

if (translator)
{
qApp->removeTranslator(translator);
delete translator;
}

translator = new QTranslator(0);

if (language =="en")
{
translator->load(":/translation/language + ".qm");
qApp->installTranslator( translator );
}
}

The language don’t change at runtime. I use ui-files and classes code by hand. All strings look like this tr("Please translate me").

Can somebody tell me how can i translate all strings in all open widgets at runtime?

Thanks in advance,

Whitefurrows

jpn
28th October 2006, 23:59
QtCentre Wiki: Dynamic translation (http://wiki.qtcentre.org/index.php?title=Dynamic_translation)

whitefurrows
29th October 2006, 13:20
Hi,

thank you for the example, that’s good, but i have one more question.

My QMainWindow is build without Designer and i can’t use ui.retranslateUi(this);. How can i translate the strings in my QMainWindow?

Is it right that i must use the following code in all Widgets, wich build with the designer to translate the strings?


void MyWindow::changeEvent(QEvent* event)
{
if (event ->type() == QEvent::LanguageChange)
{
// retranslate designer form (single inheritance approach)
ui.retranslateUi(this);

// retranslate other widgets which weren't added in designer
retranslate();
}

// remember to call base class implementation
QMainWindow::changeEvent(event);
}


How can i tell the designer, don’t translate all strings? I want translate only definite strings.

Another problem is, the designer translate html in the strings. How can i tell the disigner translate the strings without html?

Thanks in advance,

Whitefurrows

wysota
29th October 2006, 13:35
If you don't use Designer, you have to implement your own method for retranslating strings (in the above example it is called retranslate()) and you have to call it on every language change.

whitefurrows
30th October 2006, 18:31
Hi,

thank you, but i have a problem.

I do this e.g.:

void MyMainWindow::createActions()
{
actionOpen = new QAction(QIcon(":/myPic.png"), tr("&Open File"), this);
}

and i must do this:

void MyMainWindow::retranslate()
{
actionOpen->setText(tr("&Datei öffnen"));
}
I think that’s dirty and a lot to do, but it’s OK.

What can i do in a function like that:

void AnyWidget::anyFunction(){
QString html;

html.append("<table border=\"0\" width=\"100%\">");
html.append("<tr><td>" + tr("Text to Translate") + "</td>");
html.append("<td>" + tr("Text to Translate") + "</td></tr>");
html.append("</table>");

myTextEdit->document()->setHtml(html);
}
I can’t translate in retranslate()

Thanks in advance,

Whitefurrows

jpn
30th October 2006, 18:47
I do this e.g.:

void MyMainWindow::createActions()
{
actionOpen = new QAction(QIcon(":/myPic.png"), tr("&Open File"), this);
}

and i must do this:

void MyMainWindow::retranslate()
{
actionOpen->setText(tr("&Datei öffnen"));
}
I think that’s dirty and a lot to do, but it’s OK.

Hmm, you are not supposed to hardcode all the languages in the code. Use only one string in code and then use translation files to translate it to any language you want.

void MyMainWindow::createActions()
{
actionOpen = new QAction(QIcon(":/myPic.png"), tr("&Open File"), this);
}
void MyMainWindow::retranslate()
{
actionOpen->setText(tr("&Open File")); // same string here, put "&Datei öffnen" into the translation file
}

whitefurrows
30th October 2006, 23:46
Hi,

ok i think i have understood, but what can i do with a function like that:

void AnyWidget::anyFunction(){
QString html;

html.append("<table border=\"0\" width=\"100%\">");
html.append("<tr><td>" + tr("Text to Translate") + "</td>");
html.append("<td>" + tr("Text to Translate") + "</td></tr>");
html.append("</table>");

myTextEdit->document()->setHtml(html);
}

Thanks in advance,

Whitefurrows

jpn
31st October 2006, 08:05
There is no other way around than somehow identifying the translated parts (using regexp maybe?) and then setting the translated text again after passing it to tr().

whitefurrows
31st October 2006, 13:37
Hi,

sorry i don't know what are you mean. Can you give me a example please.

Thanks in advance,

Whitefurrows

wysota
1st November 2006, 22:58
I think that in that situation you should tr() the whole html code and provide translations for it in the translation file.


html->append(tr("<HTML><BODY>Text to be translated goes here<BR>\\n"
"and you can even have arguments for it like this: %1</BODY></HTML>")
.arg(tr("Some argument")));

whitefurrows
2nd November 2006, 14:24
Hi,

thank you, but this is not my problem. I have attach a example and hope you can help me.

Thanks in advance,

Whitefurrows

wysota
2nd November 2006, 18:24
And what is the difference between the two?

whitefurrows
3rd November 2006, 12:01
It’s not unconditional necessary to translate a argunent like arg(tr("Some argument")). I don’t know how can i translate a text that is set in a QTextEdit, can you help me?

Tanks in advance,

Whitefurrows

wysota
3rd November 2006, 18:20
Nobody says you have to have any arguments. I just said there is a possibility to do that, I didn't say you have to use it. If you have an append() call in your code, just recreate the contents using the same calls. If all those appends have a meaning, you can surely create a list of "commands" and refill the text edit with those commands (think of them as templates) translated to the appropriate language.

whitefurrows
4th November 2006, 19:16
Hi,

sorry i don't know how can i create a list of "commands" and refill the text edit with those commands. I have made a example that show you exact what i need, but is not so complex as the original. Please take a look at the example i have need a lot of time to make the example so easy for you. I hope you can help me.

Thanks in advance,

Whitefurrows

wysota
4th November 2006, 19:27
I'm sorry, I just don't see the problem, you mean this?

void MainWindow::showResult(int result)
{
QString html;
html.append("<HTML><BODY>");

html.append("<table border=\"0\" width=\"100%\">");
html.append("<tr><td align=\"center\">" + tr("The result is:") + "</td>");
html.append("<td align=\"center\">" + QString().setNum(result) + "</td></tr>");
html.append("</table>");

html.append("</HTML></BODY>");

ui.info->document()->setHtml(html);
};

Just store the result somewhere and call this method again with the stored value when you need to retranslate the text edit.

BTW. You can substitute
QString().setNum(result) with
QString::number(result).

whitefurrows
5th November 2006, 02:23
Hi,

thank you for your great help. i think i can do that, i store the result and call showResult again. What can i do in showInfo i set the text again but that wasn't translate. Can you help me?

Thanks in advance,

Whitefurrows

wysota
5th November 2006, 10:11
You mean this?

InfoView info(infos[i], this);

In this situation you have to call tr() here to do the translation and don't translate in setInfo(). You should only translate strings when they are displayed on screen (or shown to user in some other way). In setInfo() you should only mark strings for translation with QT_TR_NOOP().

whitefurrows
5th November 2006, 15:57
Hi,

thank you, that's fine. A last little questin. Now is all working, the only thing what's not OK is that:


QT_TR_NOOP("Any information") + QString("<br><br>") + QT_TR_NOOP("for more text look next page");

What can i do to translate? I don't want translate the html code.

Thanks in advance,

Whitefurrows

wysota
5th November 2006, 16:14
How about:
QString s = QString("%1<br><br>%2")
.arg(tr("Any information"))
.arg(tr("for more text look next page"));
setText(s); // or something simmilar

If you want to avoid translating html code, you have to introduce html tags during actual translation (when tr() is called) or even later - not when using QT_TR_NOOP().

whitefurrows
6th November 2006, 11:29
the problem is not tr() or QT_TR_NOOP() the problem is i use


tr("Any information") + "<br><br>" + tr("for more text look next page");

and translate it


tr(infoData->Text.toLatin1())

That’s the reason why your last example and my code not work. My trial with QT_TR_NOOP() has work because i have use only one line text.

That’s a look of a little orignal code with dummy text.

QString text = tr("TextTextText") + "<br><br>"+ tr("TextTextText") + "<br><br>"
"<table width=\"100%\">"
"<tr>"
"<td width=\"25%\" align=\"center\">" + tr("TextTextText") + "</td>"
"<td width=\"25%\" align=\"center\">" + tr("TextTextText") + "</td>"
"<td width=\"25%\" align=\"center\">" + tr("TextTextText") + "</td>"
"<td width=\"25%\" align=\"center\">" + tr("TextTextText") + "</td>"
"</tr>"
"<tr>"
"<td align=\"center\">" + tr("TextTextText") + "</td>"
"<td align=\"center\">" + tr("TextTextText") + "</td>"
"<td align=\"center\">" + tr("TextTextText") + "</td>"
"<td align=\"center\">" + tr("TextTextText") + "</td>"
"</tr>"
"<tr>"
"<td align=\"center\">" + tr("TextTextText") + "</td>"
"<td align=\"center\">" + tr("TextTextText") + "</td>"
"<td align=\"center\">" + tr("TextTextText") + "</td>"
"<td align=\"center\">" + tr("TextTextText") + "</td>"
"</tr>"
"<tr>"
"<td align=\"center\">" + tr("TextTextText") + "</td>"
"<td align=\"center\">" + tr("TextTextText") + "</td>"
"<td align=\"center\">" + tr("TextTextText") + "</td>"
"<td align=\"center\">" + tr("TextTextText") + "</td>"
"</tr>"
"</table>";

I can translate the text if i set all in tr() but that’s looks like dirty in the linguist and it’s hard to translate. Can you tell me how can i translate it without html?

Thanks in advance,

Whitefurrows

wysota
6th November 2006, 11:32
You could for example place markers in the plain text, then perform the translations and then substitute markers with HTML code. Or even don't use custom markers but the ones provided by Qt (%1, %2, etc.) so you can use QString::arg to substitute them.

whitefurrows
6th November 2006, 12:32
Hi,

a big sorry to you, it’s my mistake i have forget to clear QList, tr() and QString::arg() worke fine. A lot of thanks to you for your splendid help and patience.

Best regards,

Whitefurrows