comparison src/gui/locale.cc @ 109:79714c95a145

*: add translation files and locale files I forgot to add these in the last commit :p also now you have to ask cmake to update the translations because before the ts files were not tracked (obviously)
author Paper <mrpapersonic@gmail.com>
date Mon, 06 Nov 2023 01:51:44 -0500
parents
children 39521c47c7a3
comparison
equal deleted inserted replaced
108:2004b41d4a59 109:79714c95a145
1 #include "gui/locale.h"
2 #include "core/strings.h"
3 #include <QTranslator>
4 #include <QLocale>
5 #include <QDir>
6 #include <QString>
7 #include <QApplication>
8 #include <iostream>
9
10 namespace Locale {
11
12 std::string GetLocaleFullName(const QLocale& locale) {
13 QString res = QLocale::languageToString(locale.language());
14 if (locale.country() != QLocale::AnyCountry)
15 res += " (" + QLocale::countryToString(locale.country()) + ")";
16 return Strings::ToUtf8String(res);
17 }
18
19 Locale::Locale() {
20 RefreshAvailableLocales();
21
22 /* default to en_US */
23 if (!IsLocaleAvailable(QLocale()))
24 SetActiveLocale(QLocale("en_US"));
25 else /* TODO: is this needed? */
26 SetActiveLocale(QLocale());
27 }
28
29 Locale::Locale(const std::string& name) {
30 RefreshAvailableLocales();
31 SetActiveLocale(QLocale(Strings::ToQString(name)));
32 }
33
34 QLocale Locale::GetLocale() {
35 return _locale;
36 }
37
38 std::vector<QLocale> Locale::GetAvailableLocales() {
39 return _available_translations;
40 }
41
42 void Locale::RefreshAvailableLocales() {
43 _available_translations.clear();
44
45 /* we will always have en_US */
46 _available_translations.push_back(QLocale("en_US"));
47
48 QDir dir(":/locale");
49 if (!dir.exists())
50 return;
51
52 QStringList translations = dir.entryList({"*.qm"}, QDir::Files);
53
54 _available_translations.reserve(translations.size());
55 for (const QString& str : translations) {
56 _available_translations.push_back(QLocale(str.mid(0, str.lastIndexOf("."))));
57 }
58 }
59
60 bool Locale::IsLocaleAvailable(const QLocale& locale) {
61 for (const QLocale& l : _available_translations)
62 if (l == locale)
63 return true;
64 return false;
65 }
66
67 bool Locale::SetActiveLocale(const QLocale& locale) {
68 if (!IsLocaleAvailable(locale))
69 return false;
70 if (_locale == locale)
71 return true; /* we're... already on this locale :) */
72
73 _locale = locale;
74 QLocale::setDefault(_locale);
75
76 /* we can still do stuff even if one thing fails! */
77 bool return_value = true;
78
79 const QString name = _locale.name();
80 if (!SwitchTranslator(_translator, QString(":/locale/%1.qm").arg(name)))
81 return_value = false;
82
83 const QString path = qApp->applicationDirPath();
84 if (!SwitchTranslator(_translator_qt, path + QString("/translations/qt_%1.qm").arg(name))) {
85 /* Sometimes Qt will have proper translations for the language, but not the specific
86 country. In that case, we still want to use that language. */
87 const int underscore_index = name.lastIndexOf("_");
88 if (!underscore_index)
89 return false;
90
91 const QString short_name = name.mid(0, underscore_index);
92 if (!SwitchTranslator(_translator_qt, path + QString("/translations/qt_%1.qm").arg(short_name)))
93 return_value = false;
94 }
95
96 return return_value;
97 }
98
99 bool Locale::SwitchTranslator(QTranslator& translator, const QString& path) {
100 qApp->removeTranslator(&translator);
101
102 if (!translator.load(path))
103 return false;
104 qApp->installTranslator(&translator);
105 return true;
106 }
107
108 }