Je cherche à traduire mon application sans devoir la redémarrer à chaque changement de langue, donc avec des traductions dynamiques. A l'aide d'un menu "Language" qui permet à l'utilisateur de sélectionner sa langue. J'ai donc créé des fichiers .ts et .qm. Mais ça ne marche pas, voici mon code :
#include "FenPrincipale.h"
#include <QApplication>
FenPrincipale::FenPrincipale()
{
// Génération des widgets de la fenêtre principale
creerActions();
creerMenus();
creerBarresOutils();
creerBarreEtat();
// Génération des onglets et chargement de la page d'accueil
onglets = new QTabWidget;
onglets->addTab(creerOngletPageWeb(tr("http://www.duckduckgo.com")), tr("(New page)"));
connect(onglets, SIGNAL(currentChanged(int)), this, SLOT(changementOnglet(int)));
setCentralWidget(onglets);
// Définition de quelques propriétés de la fenêtre
setMinimumSize(500, 350);
setWindowIcon(QIcon(":images/navigo.png"));
setWindowTitle(tr("Navigo"));
translator = new QTranslator(0);
translator->load("navigo_en.qm");
qApp->installTranslator(translator);
}
void FenPrincipale::creerActions()
{
actionNouvelOnglet = new QAction(tr("&New tab"), this);
actionNouvelOnglet->setShortcut(tr("Ctrl+T"));
connect(actionNouvelOnglet, SIGNAL(triggered()), this, SLOT(nouvelOnglet()));
actionFermerOnglet = new QAction(tr("&Close the tab"), this);
actionFermerOnglet->setShortcut(tr("Ctrl+W"));
connect(actionFermerOnglet, SIGNAL(triggered()), this, SLOT(fermerOnglet()));
actionQuitter = new QAction(tr("&Exit"), this);
actionQuitter->setShortcut(tr("Ctrl+Q"));
connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));
actionPrecedente = new QAction(QIcon(":images/precedente.png"), tr("&Back"), this);
actionPrecedente->setShortcut(QKeySequence::Back);
connect(actionPrecedente, SIGNAL(triggered()), this, SLOT(precedente()));
actionSuivante = new QAction(QIcon(":images/suivante.png"), tr("&Forward"), this);
actionSuivante->setShortcut(QKeySequence::Forward);
connect(actionSuivante, SIGNAL(triggered()), this, SLOT(suivante()));
actionStop = new QAction(QIcon(":images/stop.png"), tr("S&top"), this);
connect(actionStop, SIGNAL(triggered()), this, SLOT(stop()));
actionActualiser = new QAction(QIcon(":images/actualiser.png"), tr("&Refresh"), this);
actionActualiser->setShortcut(QKeySequence::Refresh);
connect(actionActualiser, SIGNAL(triggered()), this, SLOT(actualiser()));
actionAccueil = new QAction(QIcon(":images/accueil.png"), tr("H&ome"), this);
connect(actionAccueil, SIGNAL(triggered()), this, SLOT(accueil()));
actionGo = new QAction(QIcon(":images/go.png"), tr("G&o to ெ"), this);
connect(actionGo, SIGNAL(triggered()), this, SLOT(chargerPage()));
actionEN = new QAction(QIcon(":images/en.png"), tr("English"), this);
connect(actionEN, SIGNAL(triggered()), this, SLOT(changementLangue(int indice=0)));
actionES = new QAction(QIcon(":images/es.png"), tr("Español"), this);
connect(actionES, SIGNAL(triggered()), this, SLOT(changementLangue(int indice=1)));
actionFR = new QAction(QIcon(":images/fr.png"), tr("Français"), this);
connect(actionFR, SIGNAL(triggered()), this, SLOT(changementLangue(int indice=2)));
actionAPropos = new QAction(tr("&About us..."), this);
connect(actionAPropos, SIGNAL(triggered()), this, SLOT(aPropos()));
actionAPropos->setShortcut(QKeySequence::HelpContents);
actionMAJ = new QAction (tr("Check for updates..."), this);
connect(actionMAJ, SIGNAL(triggered()), this, SLOT(MAJ()));
}
void FenPrincipale::creerMenus()
{
QMenu *menuFichier = menuBar()->addMenu(tr("&File"));
menuFichier->addAction(actionNouvelOnglet);
menuFichier->addAction(actionFermerOnglet);
menuFichier->addSeparator();
menuFichier->addAction(actionQuitter);
QMenu *menuNavigation = menuBar()->addMenu(tr("&Navigation"));
menuNavigation->addAction(actionPrecedente);
menuNavigation->addAction(actionSuivante);
menuNavigation->addAction(actionStop);
menuNavigation->addAction(actionActualiser);
menuNavigation->addAction(actionAccueil);
QMenu *menuLanguage = menuBar()->addMenu(tr("&Language"));
menuLanguage->addAction(actionEN);
menuLanguage->addAction(actionFR);
menuLanguage->addAction(actionES);
QMenu *menuAide = menuBar()->addMenu(tr("&?"));
menuAide->addAction(actionAPropos);
menuAide->addAction(actionMAJ);
}
void FenPrincipale::creerBarresOutils()
{
champAdresse = new QLineEdit;
connect(champAdresse, SIGNAL(returnPressed()), this, SLOT(chargerPage()));
QToolBar *toolBarNavigation = addToolBar(tr("Navigation"));
toolBarNavigation->addAction(actionPrecedente);
toolBarNavigation->addAction(actionSuivante);
toolBarNavigation->addAction(actionStop);
toolBarNavigation->addAction(actionActualiser);
toolBarNavigation->addAction(actionAccueil);
toolBarNavigation->addWidget(champAdresse);
toolBarNavigation->addAction(actionGo);
}
void FenPrincipale::creerBarreEtat()
{
progression = new QProgressBar(this);
progression->setVisible(false);
progression->setMaximumHeight(14);
statusBar()->addWidget(progression, 1);
}
// Slots
void FenPrincipale::precedente()
{
pageActuelle()->back();
}
void FenPrincipale::suivante()
{
pageActuelle()->forward();
}
void FenPrincipale::accueil()
{
pageActuelle()->load(QUrl(tr("http://www.duckduckgo.com")));
}
void FenPrincipale::stop()
{
pageActuelle()->stop();
}
void FenPrincipale::actualiser()
{
pageActuelle()->reload();
}
void FenPrincipale::aPropos()
{
QMessageBox::information(this, tr("About us..."), tr("Navigo is a web browser made by ...."));
}
void FenPrincipale::MAJ()
{
QMessageBox::information(this, tr("Update done!"), tr("Navigo 1.1.4 is currently the most recent version available.<br>"
"<br><strong>Coming soon</strong> ????"
"<br>-Displaying the history in a menu<br>"
"-Search in the page<br>"
"-Options window<br>"
"-Bookmark management"));
}
void FenPrincipale::nouvelOnglet()
{
int indexNouvelOnglet = onglets->addTab(creerOngletPageWeb(), tr("(New page)"));
onglets->setCurrentIndex(indexNouvelOnglet);
champAdresse->setText("");
champAdresse->setFocus(Qt::OtherFocusReason);
}
void FenPrincipale::fermerOnglet()
{
// On ne doit pas fermer le dernier onglet, sinon le QTabWidget ne marche plus
if (onglets->count() > 1)
{
onglets->removeTab(onglets->currentIndex());
}
else
{
QMessageBox::critical(this, tr("Error"), tr("At least one tab is needed!"));
}
}
void FenPrincipale::chargerPage()
{
QString url = champAdresse->text();
// On rajoute le "http://" s'il n'est pas déjà dans l'adresse
if (url.left(7) != "http://")
{
url = "http://" + url;
champAdresse->setText(url);
}
pageActuelle()->load(QUrl(url));
}
void FenPrincipale::changementOnglet(int index)
{
changementTitre(pageActuelle()->title());
changementUrl(pageActuelle()->url());
}
void FenPrincipale::changementTitre(const QString & titreComplet)
{
QString titreCourt = titreComplet;
// On tronque le titre pour éviter des onglets trop larges
if (titreComplet.size() > 40)
{
titreCourt = titreComplet.left(40) + "...";
}
setWindowTitle(titreCourt + " - " + tr("Navigo"));
onglets->setTabText(onglets->currentIndex(), titreCourt);
}
void FenPrincipale::changementUrl(const QUrl & url)
{
if (url.toString() != tr(":autres/page_blanche.html"))
{
champAdresse->setText(url.toString());
}
}
void FenPrincipale::chargementDebut()
{
progression->setVisible(true);
}
void FenPrincipale::chargementEnCours(int pourcentage)
{
progression->setValue(pourcentage);
}
void FenPrincipale::chargementTermine(bool ok)
{
progression->setVisible(false);
statusBar()->showMessage(tr("Loan"), 2000);
}
void FenPrincipale::changementLangue(int indice)
{
QString s_langueChoisie;
if(indice == 0)
{
s_langueChoisie = "en";
}
else if (indice == 1)
{
s_langueChoisie = "es";
}
else if (indice == 2)
{
s_langueChoisie = "fr";
}
if (!s_langueChoisie.isEmpty())
{
retranslateInterface(&s_langueChoisie);
}
}
void FenPrincipale::retranslateInterface(QString *language)
{
QString lang = language->toLower();
lang = "navigo_" + lang + ".qm";
if ( translator )
qApp->removeTranslator( translator );
translator->load( lang);
qApp->installTranslator( translator );
// ici on remet tous les textes à changer ...
actionAPropos = new QAction(tr("&About us..."), this);
// etc.
}
// Autres méthodes
QWidget *FenPrincipale::creerOngletPageWeb(QString url)
{
QWidget *pageOnglet = new QWidget;
QWebEngineView *pageWeb = new QWebEngineView;
QVBoxLayout *layout = new QVBoxLayout;
layout->setContentsMargins(0,0,0,0);
layout->addWidget(pageWeb);
pageOnglet->setLayout(layout);
if (url.isEmpty())
{
pageWeb->load(QUrl(tr(":autres/page_blanche.html")));
}
else
{
if (url.left(7) != "http://")
{
url = "http://" + url;
}
pageWeb->load(QUrl(url));
}
// Gestion des signaux envoyés par la page web
connect(pageWeb, SIGNAL(titleChanged(QString)), this, SLOT(changementTitre(QString)));
connect(pageWeb, SIGNAL(urlChanged(QUrl)), this, SLOT(changementUrl(QUrl)));
connect(pageWeb, SIGNAL(loadStarted()), this, SLOT(chargementDebut()));
connect(pageWeb, SIGNAL(loadProgress(int)), this, SLOT(chargementEnCours(int)));
connect(pageWeb, SIGNAL(loadFinished(bool)), this, SLOT(chargementTermine(bool)));
return pageOnglet;
}
QWebEngineView *FenPrincipale::pageActuelle()
{
return onglets->currentWidget()->findChild<QWebEngineView *>();
}
Peux-tu faire un exemple minimal qui met en évidence le problème ? Comme il impliquera vraisemblablement plusieurs fichier, partage le sur github et donne les instructions pour qu'on puisse le compiler.
L'idéal serait de repartir de ce tutoriel pour comprendre ce qui cloche.