Mercurial > minori
annotate src/gui/locale.cc @ 295:b82841e76e79
*: better support on Windows
things now look much better in dark mode
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Sun, 12 May 2024 20:24:09 -0400 |
parents | 862d0d8619f6 |
children | b1f625b0227c |
rev | line source |
---|---|
109
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
1 #include "gui/locale.h" |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
2 #include "core/strings.h" |
195
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
3 |
258 | 4 #include <QApplication> |
109
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
5 #include <QDir> |
258 | 6 #include <QLocale> |
109
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
7 #include <QString> |
258 | 8 #include <QTranslator> |
195
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
9 |
295 | 10 #include <QtGlobal> |
109
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
11 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
12 namespace Locale { |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
13 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
14 std::string GetLocaleFullName(const QLocale& locale) { |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
15 QString res = QLocale::languageToString(locale.language()); |
295 | 16 #if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) |
17 /* silence deprecation warning for locale.country() */ | |
18 if (locale.territory() != QLocale::AnyTerritory) | |
19 res += " (" + QLocale::territoryToString(locale.territory()) + ")"; | |
20 #else | |
109
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
21 if (locale.country() != QLocale::AnyCountry) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
22 res += " (" + QLocale::countryToString(locale.country()) + ")"; |
295 | 23 #endif |
109
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
24 return Strings::ToUtf8String(res); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
25 } |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
26 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
27 Locale::Locale() { |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
28 RefreshAvailableLocales(); |
195
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
29 SetActiveLocale(QLocale("en_US")); |
109
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
30 } |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
31 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
32 Locale::Locale(const std::string& name) { |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
33 RefreshAvailableLocales(); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
34 SetActiveLocale(QLocale(Strings::ToQString(name))); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
35 } |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
36 |
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
109
diff
changeset
|
37 QLocale Locale::GetLocale() const { |
109
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
38 return _locale; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
39 } |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
40 |
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
109
diff
changeset
|
41 std::vector<QLocale> Locale::GetAvailableLocales() const { |
109
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
42 return _available_translations; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
43 } |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
44 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
45 void Locale::RefreshAvailableLocales() { |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
46 _available_translations.clear(); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
47 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
48 /* we will always have en_US */ |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
49 _available_translations.push_back(QLocale("en_US")); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
50 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
51 QDir dir(":/locale"); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
52 if (!dir.exists()) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
53 return; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
54 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
55 QStringList translations = dir.entryList({"*.qm"}, QDir::Files); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
56 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
57 _available_translations.reserve(translations.size()); |
195
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
58 for (const QString& str : translations) |
109
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
59 _available_translations.push_back(QLocale(str.mid(0, str.lastIndexOf(".")))); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
60 } |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
61 |
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
109
diff
changeset
|
62 bool Locale::IsLocaleAvailable(const QLocale& locale) const { |
109
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
63 for (const QLocale& l : _available_translations) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
64 if (l == locale) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
65 return true; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
66 return false; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
67 } |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
68 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
69 bool Locale::SetActiveLocale(const QLocale& locale) { |
195
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
70 if (!IsLocaleAvailable(locale) || !qApp) |
109
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
71 return false; |
195
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
72 |
109
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
73 if (_locale == locale) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
74 return true; /* we're... already on this locale :) */ |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
75 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
76 _locale = locale; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
77 QLocale::setDefault(_locale); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
78 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
79 /* we can still do stuff even if one thing fails! */ |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
80 bool return_value = true; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
81 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
82 const QString name = _locale.name(); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
83 if (!SwitchTranslator(_translator, QString(":/locale/%1.qm").arg(name))) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
84 return_value = false; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
85 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
86 const QString path = qApp->applicationDirPath(); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
87 if (!SwitchTranslator(_translator_qt, path + QString("/translations/qt_%1.qm").arg(name))) { |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
88 /* Sometimes Qt will have proper translations for the language, but not the specific |
295 | 89 * country. In that case, we still want to use that language. */ |
109
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
90 const int underscore_index = name.lastIndexOf("_"); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
91 if (!underscore_index) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
92 return false; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
93 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
94 const QString short_name = name.mid(0, underscore_index); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
95 if (!SwitchTranslator(_translator_qt, path + QString("/translations/qt_%1.qm").arg(short_name))) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
96 return_value = false; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
97 } |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
98 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
99 return return_value; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
100 } |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
101 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
102 bool Locale::SwitchTranslator(QTranslator& translator, const QString& path) { |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
103 qApp->removeTranslator(&translator); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
104 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
105 if (!translator.load(path)) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
106 return false; |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
107 |
109
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
108 qApp->installTranslator(&translator); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
109 return true; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
110 } |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
111 |
258 | 112 } // namespace Locale |