qtranslator with dynamic labels
-
I have a dictionary italian-english and english-italian, but with this dictionary I can translate only the labels fixed in ui, the labels that I write during the program aren't translated
I wrote:void MainWindow::on_Traduzione_Inglese_clicked() { qApp->installTranslator(&Lingua_Italiana); ui->Traduzione_Italiano->setDisabled(false); // if(ui->info->text=="CONF. IN CARICAMENTO") if(ui->Info->text()=="CONF. IN CARICAMENTO") ui->Info->setText("cONF..lOADING"); }
if is for dynamic label but it doesn't work, is there a way fast and not manual?
-
@vale88
hi, when you load a qtranslator a general ChangeEvent is send through the application.one usually listens to that and reapplies/sets the texts.
for your ui form class this is automated via aui->retranslateUi(this);
callIf you set any texts inside your source code, you'll have to mark those for translation your self via
tr("Text to Translate")
and simply recall the setText function.example:
void myClass::changeEvent(QEvent *e) { QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); setTexts(); break; default: break; } } void myClass::setTexts() { backButton->setText(tr("Back")); closeButton->setText(tr("Close")); saveButton->setText(tr( "Save")); .... }
-
@vale88 said in qtranslator with dynamic labels:
if is for dynamic label but it doesn't work, is there a way fast and not manual?
Nope. Any text must be marked with TR or it won't work.
you should read this to get an overview of how it works
https://doc.qt.io/qt-5/i18n-source-translation.html -
@J.Hilk yes, I have:
void MainWindow::changeEvent(QEvent* event) { if(event->type() == QEvent::LanguageChange) { ui->retranslateUi(this); } QWidget::changeEvent(event); }
and then:
void MainWindow::on_Traduzione_Inglese_clicked() { qApp->installTranslator(&Lingua_Italiana); ui->Traduzione_Italiano->setDisabled(false); // if(ui->info->text=="CONF. IN CARICAMENTO") if(ui->Info->text()=="CONF. IN CARICAMENTO") ui->Info->setText("cONF..lOADING"); }
if I have a function like this:
void MainWindow::Caricamento() { ui->Configurazione_Attuale->setText(selected_file_name); ui->Info->setText("CONF. CARICATA"); }
i can write ui->Info->setText(tr(....
but how I can call the function in change event, because I have other functions in Caricamento -
@vale88 must I add something in qt linguist? I don't understand
-
@vale88
all text must be flagged with TR
so
ui->Info->setText( tr("cONF..lOADING") );Then you extract the texts using the tool
use qt linguist to do the actual translation. (adding the translated text)
for the languages you support.
Then produced the compiled text qm and use that with
installTranslatorThe rules for what text it will extract is talked about here
https://doc.qt.io/qt-5/i18n-source-translation.htmlAlso for any dynamically created QLabel, you must also call its setText again.
-
@vale88
well how do you set the dynamic labels ?it should be something like
QLabel *lab= new QLabel(this)
lab->setText( tr("my text") ); -
This post is deleted!
-
@vale88
Hi
You have to find out why it does not see the texts and extracts them. -
@vale88 have you run
lupdate
after adding thetr
tags around your dynamic labels ? -
@J.Hilk MY PROBLEM is: if I have some words that I write during the code:
example:ui->info->setText("ciao");
to translate ciao I have write:
ui->infpo->setText(tr("ciao"));
but IT DOESN'T work, I think because "ciao" isn't in my dictionary, how cAN i DO TO ADD? -
@vale88
try run lupdate a few times.
It does work that way. -
and you need to have in your .pro file
TRANSLATIONS = myApp_it.ts
where the names are correct. -
@vale88
If it does not see the texts, im not sure later the translation will work.
Also, as far as i know, you can't add manual text in qt linguist. at least i didn't see any menu for that. -
@mrjj said in qtranslator with dynamic labels:
Also, as far as i know, you can't add manual text in qt linguist. at least i didn't see any menu for that.
you could edit the XML file manually, but that's not a good idea.
@vale88 please review the documentation suggested previously. As a summary you should:
- Enclose all the strings you want translated with tr() method in your code
- Set property translatable checked for all the texts you want translated in the UI (when using Qt Designer)
- Have a languagueChange() method in every class you want translated on the fly (if you want the user to switch languages with the application running). The method will be called automatically in response to a new translator being installed. Pseudo-code:
void YourClass::languageChange() { // all strings in UI form for this class will be retranslated here ui->retranslateUi(this); // Use following approach for setting a translated string other than the one you set in Designer ui->label->setText(tr("set label value to a different one from what you used in UI Designer")); }
- Add the desired language(s) to your project (.pro) file:
TRANSLATIONS = yourApp_it.ts / yourApp_es.ts / yourApp_frts
- run lupdate in your project's root folder. In the example above, you'll end up with 3 files to translate (Italian, Spanish, French)
- Run linguist and open the *.ts file and provide the proper translations for the desired language(s)
- Run lrelease to convert the translated .ts file(s) into .qm files (binary format)
- In your application, install the proper translator (.qm file) for the selected language
- Enjoy!
Just in case from your previous snippet:
void MainWindow::on_Traduzione_Inglese_clicked() { qApp->installTranslator(&Lingua_Italiana); ui->Traduzione_Italiano->setDisabled(false); // if(ui->info->text=="CONF. IN CARICAMENTO") if(ui->Info->text()=="CONF. IN CARICAMENTO") ui->Info->setText("cONF..lOADING"); }); }
it looks like the method is called when a button for "English Transalation" is clicked but you seem to load the Italian translator. In addition, it's a good approach to check that the translator did actually was installed properly, i.e. (pseudo-code again):
bool loaded = m_translator->load(pagePrefix +'_'+ locale); if (!loaded) qWarning() << QString("can't load %1 translation").arg(locale);
-
@Pablo-J.-Rogina I want to translate from italian to english, so how must i write in .pro?
and an other question, if I write manually,like this..void MainWindow::on_Traduzione_Inglese_clicked() { qApp->installTranslator(&Lingua_Italiana); ui->Traduzione_Italiano->setDisabled(false); // if(ui->info->text=="CONF. IN CARICAMENTO") if(ui->Info->text()=="CONF. IN CARICAMENTO") ui->Info->setText("cONF..lOADING"); }
in Info doesn't wtite conf loading..because installTranslaton block this I think
-
@vale88 it looks like you're not following the suggestions, like reading the documentation...