KTextAddons

translatorutil.cpp
1/*
2 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "translatorutil.h"
8
9#include "texttranslator_debug.h"
10#include <KConfigGroup>
11#include <KLocalizedString>
12#include <KSharedConfig>
13#include <QComboBox>
14
15using namespace Qt::Literals::StringLiterals;
16using namespace TextTranslator;
17TranslatorUtil::TranslatorUtil() = default;
18
19QMap<TranslatorUtil::Language, QString> TranslatorUtil::translatedLanguages()
20{
22 for (int i = TranslatorUtil::Language::automatic; i < TranslatorUtil::Language::lastLanguage; ++i) {
23 map.insert(static_cast<TranslatorUtil::Language>(i), translatedLanguage(static_cast<TranslatorUtil::Language>(i)));
24 }
25 return map;
26}
27
28QString TranslatorUtil::translatedLanguage(TranslatorUtil::Language lang)
29{
30 QString ret;
31 switch (lang) {
32 case automatic:
33 ret = i18n("Detect language");
34 break;
35 case hmong:
36 ret = i18n("Hmong");
37 break;
38 case bs:
39 ret = i18n("Bosnian");
40 break;
41 case en:
42 ret = i18n("English");
43 break;
44 case zh:
45 ret = i18n("Chinese (Simplified)");
46 break;
47 case zt:
48 ret = i18n("Chinese (Traditional)");
49 break;
50 case nl:
51 ret = i18n("Dutch");
52 break;
53 case fr:
54 ret = i18n("French");
55 break;
56 case de:
57 ret = i18n("German");
58 break;
59 case el:
60 ret = i18n("Greek");
61 break;
62 case it:
63 ret = i18n("Italian");
64 break;
65 case ja:
66 ret = i18n("Japanese");
67 break;
68 case ko:
69 ret = i18n("Korean");
70 break;
71 case pt:
72 ret = i18n("Portuguese");
73 break;
74 case ru:
75 ret = i18n("Russian");
76 break;
77 case es:
78 ret = i18n("Spanish");
79 break;
80 case af:
81 ret = i18n("Afrikaans");
82 break;
83 case sq:
84 ret = i18n("Albanian");
85 break;
86 case ar:
87 ret = i18n("Arabic");
88 break;
89 case hy:
90 ret = i18n("Armenian");
91 break;
92 case az:
93 ret = i18n("Azerbaijani");
94 break;
95 case eu:
96 ret = i18n("Basque");
97 break;
98 case be:
99 ret = i18n("Belarusian");
100 break;
101 case bg:
102 ret = i18n("Bulgarian");
103 break;
104 case ca:
105 ret = i18n("Catalan");
106 break;
107 case hr:
108 ret = i18n("Croatian");
109 break;
110 case cs:
111 ret = i18n("Czech");
112 break;
113 case da:
114 ret = i18n("Danish");
115 break;
116 case et:
117 ret = i18n("Estonian");
118 break;
119 case tl:
120 ret = i18n("Filipino");
121 break;
122 case fi:
123 ret = i18n("Finnish");
124 break;
125 case gl:
126 ret = i18n("Galician");
127 break;
128 case ka:
129 ret = i18n("Georgian");
130 break;
131 case ht:
132 ret = i18n("Haitian Creole");
133 break;
134 case iw:
135 ret = i18n("Hebrew");
136 break;
137 case hi:
138 ret = i18n("Hindi");
139 break;
140 case hu:
141 ret = i18n("Hungarian");
142 break;
143 case is:
144 ret = i18n("Icelandic");
145 break;
146 case id:
147 ret = i18n("Indonesian");
148 break;
149 case ga:
150 ret = i18n("Irish");
151 break;
152 case lv:
153 ret = i18n("Latvian");
154 break;
155 case lt:
156 ret = i18n("Lithuanian");
157 break;
158 case mk:
159 ret = i18n("Macedonian");
160 break;
161 case ms:
162 ret = i18n("Malay");
163 break;
164 case mt:
165 ret = i18n("Maltese");
166 break;
167 case no:
168 ret = i18n("Norwegian");
169 break;
170 case fa:
171 ret = i18n("Persian");
172 break;
173 case pl:
174 ret = i18n("Polish");
175 break;
176 case ro:
177 ret = i18n("Romanian");
178 break;
179 case sr:
180 ret = i18n("Serbian");
181 break;
182 case sk:
183 ret = i18n("Slovak");
184 break;
185 case sl:
186 ret = i18n("Slovenian");
187 break;
188 case sw:
189 ret = i18n("Swahili");
190 break;
191 case sv:
192 ret = i18n("Swedish");
193 break;
194 case th:
195 ret = i18n("Thai");
196 break;
197 case tr:
198 ret = i18n("Turkish");
199 break;
200 case uk:
201 ret = i18n("Ukrainian");
202 break;
203 case ur:
204 ret = i18n("Urdu");
205 break;
206 case vi:
207 ret = i18n("Vietnamese");
208 break;
209 case cy:
210 ret = i18n("Welsh");
211 break;
212 case yi:
213 ret = i18n("Yiddish");
214 break;
215 }
216 return ret;
217}
218
219QString TranslatorUtil::searchI18nFromLanguage(const QString &langCode)
220{
221 for (int i = TranslatorUtil::Language::automatic; i < TranslatorUtil::Language::lastLanguage; ++i) {
222 if (langCode == languageCode(static_cast<TranslatorUtil::Language>(i))) {
223 return translatedLanguage(static_cast<TranslatorUtil::Language>(i));
224 }
225 }
226 return {};
227}
228
229TranslatorUtil::Language TranslatorUtil::stringToLanguage(const QString &str)
230{
231 if (str == "en"_L1) {
232 return TranslatorUtil::en;
233 } else if (str == "fr"_L1) {
234 return TranslatorUtil::fr;
235 } else if (str == "es"_L1) {
236 return TranslatorUtil::es;
237 } else if (str == "pl"_L1) {
238 return TranslatorUtil::pl;
239 } else if (str == "de"_L1) {
240 return TranslatorUtil::de;
241 } else if (str == "cs"_L1) {
242 return TranslatorUtil::cs;
243 } else if (str == "bg"_L1) {
244 return TranslatorUtil::bg;
245 } else if (str == "et"_L1) {
246 return TranslatorUtil::et;
247 // } else if (str == "nn"_L1) {
248 // return TranslatorUtil::nn;
249 } else {
250 qCWarning(TEXTTRANSLATOR_LOG) << " Impossible to find TranslatorUtil::Language for " << str;
251 }
252 return TranslatorUtil::en;
253}
254
255QString TranslatorUtil::languageCode(TranslatorUtil::Language lang)
256{
257 QString ret;
258 switch (lang) {
259 case automatic:
260 ret = QStringLiteral("auto");
261 break;
262 case hmong:
263 ret = QStringLiteral("hmn");
264 break;
265 case en:
266 ret = QStringLiteral("en");
267 break;
268 case zh:
269 ret = QStringLiteral("zh-CN");
270 break;
271 case zt:
272 ret = QStringLiteral("zh-TW");
273 break;
274 case nl:
275 ret = QStringLiteral("nl");
276 break;
277 case fr:
278 ret = QStringLiteral("fr");
279 break;
280 case de:
281 ret = QStringLiteral("de");
282 break;
283 case el:
284 ret = QStringLiteral("el");
285 break;
286 case it:
287 ret = QStringLiteral("it");
288 break;
289 case ja:
290 ret = QStringLiteral("ja");
291 break;
292 case ko:
293 ret = QStringLiteral("ko");
294 break;
295 case pt:
296 ret = QStringLiteral("pt");
297 break;
298 case ru:
299 ret = QStringLiteral("ru");
300 break;
301 case es:
302 ret = QStringLiteral("es");
303 break;
304 case af:
305 ret = QStringLiteral("af");
306 break;
307 case sq:
308 ret = QStringLiteral("sq");
309 break;
310 case ar:
311 ret = QStringLiteral("ar");
312 break;
313 case hy:
314 ret = QStringLiteral("hy");
315 break;
316 case az:
317 ret = QStringLiteral("az");
318 break;
319 case eu:
320 ret = QStringLiteral("eu");
321 break;
322 case be:
323 ret = QStringLiteral("be");
324 break;
325 case bg:
326 ret = QStringLiteral("bg");
327 break;
328 case ca:
329 ret = QStringLiteral("ca");
330 break;
331 case hr:
332 ret = QStringLiteral("hr");
333 break;
334 case cs:
335 ret = QStringLiteral("cs");
336 break;
337 case da:
338 ret = QStringLiteral("da");
339 break;
340 case et:
341 ret = QStringLiteral("et");
342 break;
343 case tl:
344 ret = QStringLiteral("tl");
345 break;
346 case fi:
347 ret = QStringLiteral("fi");
348 break;
349 case gl:
350 ret = QStringLiteral("gl");
351 break;
352 case ka:
353 ret = QStringLiteral("ka");
354 break;
355 case ht:
356 ret = QStringLiteral("ht");
357 break;
358 case iw:
359 ret = QStringLiteral("he");
360 break;
361 case hi:
362 ret = QStringLiteral("hi");
363 break;
364 case hu:
365 ret = QStringLiteral("hu");
366 break;
367 case is:
368 ret = QStringLiteral("is");
369 break;
370 case id:
371 ret = QStringLiteral("id");
372 break;
373 case ga:
374 ret = QStringLiteral("ga");
375 break;
376 case lv:
377 ret = QStringLiteral("lv");
378 break;
379 case lt:
380 ret = QStringLiteral("lt");
381 break;
382 case mk:
383 ret = QStringLiteral("mk");
384 break;
385 case ms:
386 ret = QStringLiteral("ms");
387 break;
388 case mt:
389 ret = QStringLiteral("mt");
390 break;
391 case no:
392 ret = QStringLiteral("no");
393 break;
394 case fa:
395 ret = QStringLiteral("fa");
396 break;
397 case pl:
398 ret = QStringLiteral("pl");
399 break;
400 case ro:
401 ret = QStringLiteral("ro");
402 break;
403 case sr:
404 ret = QStringLiteral("sr");
405 break;
406 case sk:
407 ret = QStringLiteral("sk");
408 break;
409 case sl:
410 ret = QStringLiteral("sl");
411 break;
412 case sw:
413 ret = QStringLiteral("sw");
414 break;
415 case sv:
416 ret = QStringLiteral("sv");
417 break;
418 case th:
419 ret = QStringLiteral("th");
420 break;
421 case tr:
422 ret = QStringLiteral("tr");
423 break;
424 case uk:
425 ret = QStringLiteral("uk");
426 break;
427 case ur:
428 ret = QStringLiteral("ur");
429 break;
430 case vi:
431 ret = QStringLiteral("vi");
432 break;
433 case cy:
434 ret = QStringLiteral("cy");
435 break;
436 case yi:
437 ret = QStringLiteral("yi");
438 break;
439 case bs:
440 return QStringLiteral("bs");
441 }
442 return ret;
443}
444
445void TranslatorUtil::addItemToFromComboBox(QComboBox *combo, const QString &languageCode, const QString &translatedStr)
446{
447 combo->addItem(translatedStr, languageCode);
448}
449
450QString TranslatorUtil::groupTranslateName()
451{
452 return QStringLiteral("Translate");
453}
454
455QString TranslatorUtil::engineTranslateName()
456{
457 return QStringLiteral("engine");
458}
459
460QString TranslatorUtil::defaultEngineName()
461{
462 return QStringLiteral("google");
463}
464
465QString TranslatorUtil::loadEngine()
466{
467 KConfigGroup myGeneralGroup(KSharedConfig::openConfig(), groupTranslateName());
468 const QString engineTypeStr = myGeneralGroup.readEntry(engineTranslateName(), defaultEngineName()); // Default google
469 return engineTypeStr;
470}
471
472void TranslatorUtil::saveEngineSettings(const QString &engineName)
473{
474 KConfigGroup myGroup(KSharedConfig::openConfig(), groupTranslateName());
475 myGroup.writeEntry(engineTranslateName(), engineName);
476 myGroup.sync();
477}
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString i18n(const char *text, const TYPE &arg...)
void addItem(const QIcon &icon, const QString &text, const QVariant &userData)
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.