Bonjour,
Je créé une application et j'aimerais créer un menu pour permettre à l'utilisateur de sélectionner sa langue (francais, espagnol...), comment puis-je importer et utiliser mes fichier .qm ou .ts ?
Merci beaucoup,
Alexis
Bonjour,
Je créé une application et j'aimerais créer un menu pour permettre à l'utilisateur de sélectionner sa langue (francais, espagnol...), comment puis-je importer et utiliser mes fichier .qm ou .ts ?
Merci beaucoup,
Alexis
Bonjour,
C'est possible, mais généralement, on préfère demander à l'utilisateur (ou le faire sans demander) de redémarrer l'application, pour prendre en compte la nouvelle langue.
Voici la documentation à propos des traductions dynamiques : https://doc.qt.io/qt-5/international...ic-translation
Bonjour,
Désolé pour le temps de réponse et merci beaucoup @LittleWhite pour ta réponse ! Donc j'ai essayé les 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 :
Voilà mon fichier .cpp ! Pouvez m'aidez ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328 #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 *>(); }
Merci beaucoup par avance,
Alexis
Vous avez un bloqueur de publicités installé.
Le Club Developpez.com n'affiche que des publicités IT, discrètes et non intrusives.
Afin que nous puissions continuer à vous fournir gratuitement du contenu de qualité, merci de nous soutenir en désactivant votre bloqueur de publicités sur Developpez.com.
Partager