Open Source Snapmatic picture and Savegame viewer/editor for GTA V https://gta5view.syping.de/

TranslationClass.cpp 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /*****************************************************************************
  2. * gta5view Grand Theft Auto V Profile Viewer
  3. * Copyright (C) 2017 Syping
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *****************************************************************************/
  18. #include "TranslationClass.h"
  19. #include "AppEnv.h"
  20. #include "config.h"
  21. #include <QStringBuilder>
  22. #include <QApplication>
  23. #include <QStringList>
  24. #include <QTranslator>
  25. #include <QSettings>
  26. #include <QLocale>
  27. #include <QDebug>
  28. #include <QFile>
  29. #include <QDir>
  30. #if QT_VERSION >= 0x050000
  31. #define QtBaseTranslationFormat "qtbase_"
  32. #else
  33. #define QtBaseTranslationFormat "qt_"
  34. #endif
  35. TranslationClass TranslationClass::translationClassInstance;
  36. void TranslationClass::initUserLanguage()
  37. {
  38. QSettings settings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  39. settings.beginGroup("Interface");
  40. userLanguage = settings.value("Language", "System").toString();
  41. userAreaLanguage = settings.value("AreaLanguage", "Auto").toString();
  42. settings.endGroup();
  43. }
  44. void TranslationClass::loadTranslation(QApplication *app)
  45. {
  46. if (isLangLoaded) { unloadTranslation(app); }
  47. else { currentLangIndex = 0; }
  48. QString exLangPath = AppEnv::getExLangFolder();
  49. QString inLangPath = AppEnv::getInLangFolder();
  50. if (userLanguage == "en" || userLanguage == "en_GB")
  51. {
  52. currentLanguage = "en_GB";
  53. if (loadQtTranslation_p(exLangPath, &exQtTranslator))
  54. {
  55. app->installTranslator(&exQtTranslator);
  56. }
  57. else if (loadQtTranslation_p(inLangPath, &inQtTranslator))
  58. {
  59. app->installTranslator(&inQtTranslator);
  60. }
  61. QLocale::setDefault(currentLanguage);
  62. isLangLoaded = true;
  63. return;
  64. }
  65. #ifndef GTA5SYNC_QCONF // Classic modable loading method
  66. QString externalLanguageStr;
  67. bool externalLanguageReady = false;
  68. bool externalEnglishMode = false;
  69. bool loadInternalLang = false;
  70. bool trLoadSuccess = false;
  71. if (isUserLanguageSystem_p())
  72. {
  73. #ifdef GTA5SYNC_DEBUG
  74. qDebug() << "loadExSystemLanguage";
  75. #endif
  76. trLoadSuccess = loadSystemTranslation_p(exLangPath, &exAppTranslator);
  77. }
  78. else
  79. {
  80. #ifdef GTA5SYNC_DEBUG
  81. qDebug() << "loadExUserLanguage";
  82. #endif
  83. trLoadSuccess = loadUserTranslation_p(exLangPath, &exAppTranslator);
  84. if (!trLoadSuccess)
  85. {
  86. #ifdef GTA5SYNC_DEBUG
  87. qDebug() << "loadInUserLanguage";
  88. #endif
  89. trLoadSuccess = loadUserTranslation_p(inLangPath, &inAppTranslator);
  90. if (!trLoadSuccess)
  91. {
  92. #ifdef GTA5SYNC_DEBUG
  93. qDebug() << "loadUserLanguageFailed";
  94. #endif
  95. }
  96. else
  97. {
  98. #ifdef GTA5SYNC_DEBUG
  99. qDebug() << "loadUserLanguageSuccess";
  100. #endif
  101. loadInternalLang = true;
  102. isLangLoaded = true;
  103. }
  104. }
  105. else
  106. {
  107. #ifdef GTA5SYNC_DEBUG
  108. qDebug() << "loadUserLanguageSuccess";
  109. #endif
  110. isLangLoaded = true;
  111. }
  112. }
  113. if (trLoadSuccess)
  114. {
  115. if (currentLangIndex != 0 || isEnglishMode) // Don't install the language until we know we not have a better language for the user
  116. {
  117. #ifdef GTA5SYNC_DEBUG
  118. qDebug() << "externalLanguageReady" << currentLanguage;
  119. #endif
  120. externalEnglishMode = isEnglishMode;
  121. externalLanguageStr = currentLanguage;
  122. externalLanguageReady = true;
  123. }
  124. else
  125. {
  126. #ifdef GTA5SYNC_DEBUG
  127. qDebug() << "installTranslation";
  128. #endif
  129. if (loadInternalLang)
  130. {
  131. app->installTranslator(&inAppTranslator);
  132. }
  133. else
  134. {
  135. app->installTranslator(&exAppTranslator);
  136. }
  137. if (loadQtTranslation_p(exLangPath, &exQtTranslator))
  138. {
  139. app->installTranslator(&exQtTranslator);
  140. }
  141. else if (loadQtTranslation_p(inLangPath, &inQtTranslator))
  142. {
  143. app->installTranslator(&inQtTranslator);
  144. }
  145. QLocale::setDefault(currentLanguage);
  146. isLangLoaded = true;
  147. }
  148. }
  149. if (externalLanguageReady)
  150. {
  151. #ifdef GTA5SYNC_DEBUG
  152. qDebug() << "loadInSystemLanguage";
  153. #endif
  154. int externalLangIndex = currentLangIndex;
  155. trLoadSuccess = loadSystemTranslation_p(inLangPath, &inAppTranslator);
  156. #ifdef GTA5SYNC_DEBUG
  157. qDebug() << "externalLangIndex" << externalLangIndex << "internalLangIndex" << currentLangIndex;
  158. qDebug() << "externalEnglishMode" << externalEnglishMode << "internalEnglishMode" << isEnglishMode;
  159. #endif
  160. if ((trLoadSuccess && externalLangIndex > currentLangIndex) || (trLoadSuccess && externalEnglishMode && !isEnglishMode))
  161. {
  162. #ifdef GTA5SYNC_DEBUG
  163. qDebug() << "installInternalTranslation";
  164. #endif
  165. app->installTranslator(&inAppTranslator);
  166. if (loadQtTranslation_p(exLangPath, &exQtTranslator))
  167. {
  168. app->installTranslator(&exQtTranslator);
  169. }
  170. else if (loadQtTranslation_p(inLangPath, &inQtTranslator))
  171. {
  172. app->installTranslator(&inQtTranslator);
  173. }
  174. QLocale::setDefault(currentLanguage);
  175. isLangLoaded = true;
  176. }
  177. else
  178. {
  179. #ifdef GTA5SYNC_DEBUG
  180. qDebug() << "installExternalTranslation";
  181. #endif
  182. isEnglishMode = externalEnglishMode;
  183. currentLanguage = externalLanguageStr;
  184. app->installTranslator(&exAppTranslator);
  185. if (loadQtTranslation_p(exLangPath, &exQtTranslator))
  186. {
  187. app->installTranslator(&exQtTranslator);
  188. }
  189. else if (loadQtTranslation_p(inLangPath, &inQtTranslator))
  190. {
  191. app->installTranslator(&inQtTranslator);
  192. }
  193. QLocale::setDefault(currentLanguage);
  194. isLangLoaded = true;
  195. }
  196. }
  197. else if (!isLangLoaded)
  198. {
  199. #ifdef GTA5SYNC_DEBUG
  200. qDebug() << "loadInSystemLanguage";
  201. #endif
  202. trLoadSuccess = loadSystemTranslation_p(inLangPath, &inAppTranslator);
  203. if (trLoadSuccess)
  204. {
  205. #ifdef GTA5SYNC_DEBUG
  206. qDebug() << "installInternalTranslation";
  207. #endif
  208. app->installTranslator(&inAppTranslator);
  209. if (loadQtTranslation_p(exLangPath, &exQtTranslator))
  210. {
  211. app->installTranslator(&exQtTranslator);
  212. }
  213. else if (loadQtTranslation_p(inLangPath, &inQtTranslator))
  214. {
  215. app->installTranslator(&inQtTranslator);
  216. }
  217. QLocale::setDefault(currentLanguage);
  218. isLangLoaded = true;
  219. }
  220. else if (!trLoadSuccess)
  221. {
  222. #ifdef GTA5SYNC_DEBUG
  223. qDebug() << "fallbackToDefaultApplicationLanguage";
  224. #endif
  225. currentLanguage = "en_GB";
  226. if (loadQtTranslation_p(exLangPath, &exQtTranslator))
  227. {
  228. app->installTranslator(&exQtTranslator);
  229. }
  230. else if (loadQtTranslation_p(inLangPath, &inQtTranslator))
  231. {
  232. app->installTranslator(&inQtTranslator);
  233. }
  234. QLocale::setDefault(currentLanguage);
  235. isLangLoaded = true;
  236. }
  237. }
  238. #else // New qconf loading method
  239. bool trLoadSuccess;
  240. if (isUserLanguageSystem_p())
  241. {
  242. trLoadSuccess = loadSystemTranslation_p(inLangPath, &inAppTranslator);
  243. }
  244. else
  245. {
  246. trLoadSuccess = loadUserTranslation_p(inLangPath, &inAppTranslator);
  247. }
  248. if (!trLoadSuccess && !isUserLanguageSystem_p())
  249. {
  250. trLoadSuccess = loadSystemTranslation_p(inLangPath, &inAppTranslator);
  251. }
  252. if (trLoadSuccess)
  253. {
  254. #ifdef GTA5SYNC_DEBUG
  255. qDebug() << "installTranslation" << currentLanguage;
  256. #endif
  257. app->installTranslator(&inAppTranslator);
  258. if (loadQtTranslation_p(exLangPath, &exQtTranslator))
  259. {
  260. app->installTranslator(&exQtTranslator);
  261. }
  262. else if (loadQtTranslation_p(inLangPath, &inQtTranslator))
  263. {
  264. app->installTranslator(&inQtTranslator);
  265. }
  266. QLocale::setDefault(currentLanguage);
  267. isLangLoaded = true;
  268. }
  269. #endif
  270. }
  271. QStringList TranslationClass::listTranslations(const QString &langPath)
  272. {
  273. QDir langDir;
  274. langDir.setNameFilters(QStringList("gta5sync_*.qm"));
  275. langDir.setPath(langPath);
  276. QStringList availableLanguages;
  277. for (QString lang : langDir.entryList(QDir::Files | QDir::NoDotAndDotDot, QDir::NoSort))
  278. {
  279. availableLanguages << QString(lang).remove("gta5sync_").remove(".qm");
  280. }
  281. return availableLanguages;
  282. }
  283. QStringList TranslationClass::listAreaTranslations()
  284. {
  285. QDir langDir;
  286. langDir.setNameFilters(QStringList("global.*.ini"));
  287. langDir.setPath(":/global");
  288. QStringList availableLanguages;
  289. for (QString lang : langDir.entryList(QDir::Files | QDir::NoDotAndDotDot, QDir::NoSort))
  290. {
  291. availableLanguages << QString(lang).remove("global.").remove(".ini");
  292. }
  293. return availableLanguages;
  294. }
  295. bool TranslationClass::loadSystemTranslation_p(const QString &langPath, QTranslator *appTranslator)
  296. {
  297. #ifdef GTA5SYNC_DEBUG
  298. qDebug() << "loadSystemTranslation_p";
  299. #endif
  300. int currentLangCounter = 0;
  301. for (QString languageName : QLocale::system().uiLanguages())
  302. {
  303. #ifdef GTA5SYNC_DEBUG
  304. qDebug() << "loadLanguage" << languageName;
  305. #endif
  306. QStringList langList = QString(languageName).replace("-","_").split("_");
  307. if (langList.length() == 2)
  308. {
  309. #ifdef GTA5SYNC_DEBUG
  310. qDebug() << "loadLanguageFile" << QString(langPath % "/gta5sync_" % langList.at(0) % "_" % langList.at(1) % ".qm");
  311. #endif
  312. if (QFile::exists(langPath % "/gta5sync_" % langList.at(0) % "_" % langList.at(1) % ".qm"))
  313. {
  314. if (appTranslator->load(langPath % "/gta5sync_" % langList.at(0) % "_" % langList.at(1) % ".qm"))
  315. {
  316. #ifdef GTA5SYNC_DEBUG
  317. qDebug() << "loadLanguageFileSuccess" << QString(langPath % "/gta5sync_" % langList.at(0) % "_" % langList.at(1) % ".qm");
  318. #endif
  319. isEnglishMode = false;
  320. currentLanguage = languageName;
  321. currentLangIndex = currentLangCounter;
  322. return true;
  323. }
  324. }
  325. #ifdef GTA5SYNC_DEBUG
  326. qDebug() << "loadLanguageFile" << QString(langPath % "/gta5sync_" % langList.at(0) % ".qm");
  327. #endif
  328. if (QFile::exists(langPath % "/gta5sync_" % langList.at(0) % ".qm"))
  329. {
  330. if (appTranslator->load(langPath % "/gta5sync_" % langList.at(0) % ".qm"))
  331. {
  332. #ifdef GTA5SYNC_DEBUG
  333. qDebug() << "loadLanguageFileSuccess" << QString(langPath % "/gta5sync_" % langList.at(0) % ".qm");
  334. #endif
  335. isEnglishMode = false;
  336. currentLanguage = languageName;
  337. currentLangIndex = currentLangCounter;
  338. return true;
  339. }
  340. else if (langList.at(0) == "en")
  341. {
  342. #ifdef GTA5SYNC_DEBUG
  343. qDebug() << "languageEnglishMode index" << currentLangCounter;
  344. #endif
  345. isEnglishMode = true;
  346. currentLanguage = languageName;
  347. currentLangIndex = currentLangCounter;
  348. return true;
  349. }
  350. }
  351. else if (langList.at(0) == "en")
  352. {
  353. #ifdef GTA5SYNC_DEBUG
  354. qDebug() << "languageEnglishMode index" << currentLangCounter;
  355. #endif
  356. isEnglishMode = true;
  357. currentLanguage = languageName;
  358. currentLangIndex = currentLangCounter;
  359. return true;
  360. }
  361. }
  362. else if (langList.length() == 1)
  363. {
  364. #ifdef GTA5SYNC_DEBUG
  365. qDebug() << "loadLanguageFile" << QString(langPath % "/gta5sync_" % langList.at(0) % ".qm");
  366. #endif
  367. if (QFile::exists(langPath % "/gta5sync_" % langList.at(0) % ".qm"))
  368. {
  369. if (appTranslator->load(langPath % "/gta5sync_" % langList.at(0) % ".qm"))
  370. {
  371. #ifdef GTA5SYNC_DEBUG
  372. qDebug() << "loadLanguageFileSuccess" << QString(langPath % "/gta5sync_" % langList.at(0) % ".qm");
  373. #endif
  374. isEnglishMode = false;
  375. currentLanguage = languageName;
  376. currentLangIndex = currentLangCounter;
  377. return true;
  378. }
  379. }
  380. }
  381. #ifdef GTA5SYNC_DEBUG
  382. qDebug() << "currentLangCounter bump";
  383. #endif
  384. currentLangCounter++;
  385. }
  386. return false;
  387. }
  388. bool TranslationClass::loadUserTranslation_p(const QString &langPath, QTranslator *appTranslator)
  389. {
  390. #ifdef GTA5SYNC_DEBUG
  391. qDebug() << "loadUserTranslation_p";
  392. #endif
  393. QString languageName = userLanguage;
  394. QStringList langList = QString(languageName).replace("-","_").split("_");
  395. if (langList.length() == 2)
  396. {
  397. #ifdef GTA5SYNC_DEBUG
  398. qDebug() << "loadLanguageFile" << QString(langPath % "/gta5sync_" % langList.at(0) % "_" % langList.at(1) % ".qm");
  399. #endif
  400. if (QFile::exists(langPath % "/gta5sync_" % langList.at(0) % "_" % langList.at(1) % ".qm"))
  401. {
  402. if (appTranslator->load(langPath % "/gta5sync_" % langList.at(0) % "_" % langList.at(1) % ".qm"))
  403. {
  404. #ifdef GTA5SYNC_DEBUG
  405. qDebug() << "loadLanguageFileSuccess" << QString(langPath % "/gta5sync_" % langList.at(0) % "_" % langList.at(1) % ".qm");
  406. #endif
  407. currentLanguage = languageName;
  408. return true;
  409. }
  410. }
  411. #ifdef GTA5SYNC_DEBUG
  412. qDebug() << "loadLanguageFile" << QString(langPath % "/gta5sync_" % langList.at(0) % ".qm");
  413. #endif
  414. if (QFile::exists(langPath % "/gta5sync_" % langList.at(0) % ".qm"))
  415. {
  416. if (appTranslator->load(langPath % "/gta5sync_" % langList.at(0) % ".qm"))
  417. {
  418. #ifdef GTA5SYNC_DEBUG
  419. qDebug() << "loadLanguageFileSuccess" << QString(langPath % "/gta5sync_" % langList.at(0) % ".qm");
  420. #endif
  421. currentLanguage = languageName;
  422. return true;
  423. }
  424. }
  425. }
  426. else if (langList.length() == 1)
  427. {
  428. #ifdef GTA5SYNC_DEBUG
  429. qDebug() << "loadLanguageFile" << QString(langPath % "/gta5sync_" % langList.at(0) % ".qm");
  430. #endif
  431. if (QFile::exists(langPath % "/gta5sync_" % langList.at(0) % ".qm"))
  432. {
  433. if (appTranslator->load(langPath % "/gta5sync_" % langList.at(0) % ".qm"))
  434. {
  435. #ifdef GTA5SYNC_DEBUG
  436. qDebug() << "loadLanguageFileSuccess" << QString(langPath % "/gta5sync_" % langList.at(0) % ".qm");
  437. #endif
  438. currentLanguage = languageName;
  439. return true;
  440. }
  441. }
  442. }
  443. return false;
  444. }
  445. bool TranslationClass::loadQtTranslation_p(const QString &langPath, QTranslator *qtTranslator)
  446. {
  447. #ifdef GTA5SYNC_DEBUG
  448. qDebug() << "loadQtTranslation_p" << currentLanguage;
  449. #endif
  450. QString languageName = currentLanguage;
  451. QStringList langList = QString(languageName).replace("-","_").split("_");
  452. if (langList.length() == 2)
  453. {
  454. #ifdef GTA5SYNC_DEBUG
  455. qDebug() << "loadLanguageFile" << QString(langPath % QDir::separator() % QtBaseTranslationFormat % langList.at(0) % "_" % langList.at(1) % ".qm");
  456. #endif
  457. if (QFile::exists(langPath % QDir::separator() % QtBaseTranslationFormat % langList.at(0) % "_" % langList.at(1) % ".qm"))
  458. {
  459. if (qtTranslator->load(langPath % QDir::separator() % QtBaseTranslationFormat % langList.at(0) % "_" % langList.at(1) % ".qm"))
  460. {
  461. #ifdef GTA5SYNC_DEBUG
  462. qDebug() << "loadLanguageFileSuccess" << QString(langPath % QDir::separator() % QtBaseTranslationFormat % langList.at(0) % "_" % langList.at(1) % ".qm");
  463. #endif
  464. return true;
  465. }
  466. }
  467. #ifdef GTA5SYNC_DEBUG
  468. qDebug() << "loadLanguageFile" << QString(langPath % QDir::separator() % QtBaseTranslationFormat % langList.at(0) % ".qm");
  469. #endif
  470. if (QFile::exists(langPath % QDir::separator() % QtBaseTranslationFormat % langList.at(0) % ".qm"))
  471. {
  472. if (qtTranslator->load(langPath % QDir::separator() % QtBaseTranslationFormat % langList.at(0) % ".qm"))
  473. {
  474. #ifdef GTA5SYNC_DEBUG
  475. qDebug() << "loadLanguageFileSuccess" << QString(langPath % QDir::separator() % QtBaseTranslationFormat % langList.at(0) % ".qm");
  476. #endif
  477. return true;
  478. }
  479. }
  480. }
  481. else if (langList.length() == 1)
  482. {
  483. #ifdef GTA5SYNC_DEBUG
  484. qDebug() << "loadLanguageFile" << QString(langPath % QDir::separator() % QtBaseTranslationFormat % langList.at(0) % ".qm");
  485. #endif
  486. if (QFile::exists(langPath % QDir::separator() % QtBaseTranslationFormat % langList.at(0) % ".qm"))
  487. {
  488. if (qtTranslator->load(langPath % QDir::separator() % QtBaseTranslationFormat % langList.at(0) % ".qm"))
  489. {
  490. #ifdef GTA5SYNC_DEBUG
  491. qDebug() << "loadLanguageFileSuccess" << QString(langPath % QDir::separator() % QtBaseTranslationFormat % langList.at(0) % ".qm");
  492. #endif
  493. return true;
  494. }
  495. }
  496. }
  497. return false;
  498. }
  499. bool TranslationClass::isUserLanguageSystem_p()
  500. {
  501. return (userLanguage == "System" || userLanguage.trimmed().isEmpty());
  502. }
  503. QString TranslationClass::getCurrentAreaLanguage()
  504. {
  505. const QStringList areaTranslations = listAreaTranslations();
  506. if (userAreaLanguage == "Auto" || userAreaLanguage.trimmed().isEmpty())
  507. {
  508. GameLanguage gameLanguage = AppEnv::getGameLanguage(AppEnv::getGameVersion());
  509. if (gameLanguage == GameLanguage::Undefined)
  510. {
  511. #ifdef GTA5SYNC_DEBUG
  512. qDebug() << "autoAreaLanguageModeInterface";
  513. #endif
  514. QString langCode = QString(currentLanguage).replace("-", "_");
  515. if (areaTranslations.contains(langCode))
  516. {
  517. #ifdef GTA5SYNC_DEBUG
  518. qDebug() << "autoAreaLanguageSelected" << langCode;
  519. #endif
  520. return langCode;
  521. }
  522. else if (langCode.contains("_"))
  523. {
  524. langCode = langCode.split("_").at(0);
  525. if (!areaTranslations.contains(langCode)) goto outputDefaultLanguage;
  526. #ifdef GTA5SYNC_DEBUG
  527. qDebug() << "autoAreaLanguageSelected" << langCode;
  528. #endif
  529. return langCode;
  530. }
  531. }
  532. else
  533. {
  534. #ifdef GTA5SYNC_DEBUG
  535. qDebug() << "autoAreaLanguageModeGame";
  536. #endif
  537. QString langCode = AppEnv::gameLanguageToString(gameLanguage).replace("-", "_");
  538. if (areaTranslations.contains(langCode))
  539. {
  540. #ifdef GTA5SYNC_DEBUG
  541. qDebug() << "autoAreaLanguageSelected" << langCode;
  542. #endif
  543. return langCode;
  544. }
  545. else if (langCode.contains("_"))
  546. {
  547. langCode = langCode.split("_").at(0);
  548. if (!areaTranslations.contains(langCode)) goto outputDefaultLanguage;
  549. #ifdef GTA5SYNC_DEBUG
  550. qDebug() << "autoAreaLanguageSelected" << langCode;
  551. #endif
  552. return langCode;
  553. }
  554. }
  555. }
  556. else if (areaTranslations.contains(userAreaLanguage))
  557. {
  558. #ifdef GTA5SYNC_DEBUG
  559. qDebug() << "userAreaLanguageSelected" << userAreaLanguage;
  560. #endif
  561. return userAreaLanguage;
  562. }
  563. else if (userAreaLanguage.contains("_"))
  564. {
  565. QString langCode = QString(userAreaLanguage).replace("-", "_").split("_").at(0);
  566. if (!areaTranslations.contains(langCode)) goto outputDefaultLanguage;
  567. #ifdef GTA5SYNC_DEBUG
  568. qDebug() << "userAreaLanguageSelected" << langCode;
  569. #endif
  570. return langCode;
  571. }
  572. outputDefaultLanguage:
  573. #ifdef GTA5SYNC_DEBUG
  574. qDebug() << "defaultAreaLanguageSelected";
  575. #endif
  576. return "en";
  577. }
  578. QString TranslationClass::getCurrentLanguage()
  579. {
  580. return currentLanguage;
  581. }
  582. bool TranslationClass::isLanguageLoaded()
  583. {
  584. return isLangLoaded;
  585. }
  586. void TranslationClass::unloadTranslation(QApplication *app)
  587. {
  588. if (isLangLoaded)
  589. {
  590. #ifndef GTA5SYNC_QCONF
  591. app->removeTranslator(&exAppTranslator);
  592. app->removeTranslator(&exQtTranslator);
  593. app->removeTranslator(&inAppTranslator);
  594. app->removeTranslator(&inQtTranslator);
  595. #else
  596. app->removeTranslator(&inAppTranslator);
  597. app->removeTranslator(&exQtTranslator);
  598. #endif
  599. currentLangIndex = 0;
  600. currentLanguage = QString();
  601. QLocale::setDefault(QLocale::c());
  602. isEnglishMode = false;
  603. isLangLoaded = false;
  604. }
  605. #ifdef _MSC_VER // Fix dumb Microsoft compiler warning
  606. Q_UNUSED(app)
  607. #endif
  608. }
  609. QString TranslationClass::getCountryCode(QLocale::Country country)
  610. {
  611. QList<QLocale> locales = QLocale::matchingLocales(QLocale::AnyLanguage,
  612. QLocale::AnyScript,
  613. country);
  614. if (locales.isEmpty()) return QString();
  615. QStringList localeStrList = locales.at(0).name().split("_");
  616. if (localeStrList.length() >= 2)
  617. {
  618. return localeStrList.at(1).toLower();
  619. }
  620. else
  621. {
  622. return QString();
  623. }
  624. }
  625. QString TranslationClass::getCountryCode(QLocale locale)
  626. {
  627. QStringList localeStrList = locale.name().split("_");
  628. if (localeStrList.length() >= 2)
  629. {
  630. return localeStrList.at(1).toLower();
  631. }
  632. else
  633. {
  634. return QString();
  635. }
  636. }