annotate src/gui/locale.cc @ 294:99cbc51433e4

*: cleanup uses of QPalette I didn't know about setBackgroundRole() which is very useful in 99% of the cases where I even needed to edit the palette
author Paper <paper@paper.us.eu.org>
date Sun, 12 May 2024 18:16:08 -0400
parents 862d0d8619f6
children b82841e76e79
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 195
diff changeset
4 #include <QApplication>
109
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 #include <QDir>
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 195
diff changeset
6 #include <QLocale>
109
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 #include <QString>
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 195
diff changeset
8 #include <QTranslator>
195
975a3f0965e2 locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
9
975a3f0965e2 locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
10 #include <QDebug>
975a3f0965e2 locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
11
109
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12 #include <iostream>
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 namespace Locale {
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16 std::string GetLocaleFullName(const QLocale& locale) {
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17 QString res = QLocale::languageToString(locale.language());
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18 if (locale.country() != QLocale::AnyCountry)
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19 res += " (" + QLocale::countryToString(locale.country()) + ")";
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20 return Strings::ToUtf8String(res);
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21 }
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23 Locale::Locale() {
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24 RefreshAvailableLocales();
195
975a3f0965e2 locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
25 SetActiveLocale(QLocale("en_US"));
109
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
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
28 Locale::Locale(const std::string& name) {
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29 RefreshAvailableLocales();
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30 SetActiveLocale(QLocale(Strings::ToQString(name)));
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
118
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 109
diff changeset
33 QLocale Locale::GetLocale() const {
109
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34 return _locale;
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 std::vector<QLocale> Locale::GetAvailableLocales() const {
109
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38 return _available_translations;
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
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41 void Locale::RefreshAvailableLocales() {
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 _available_translations.clear();
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 /* we will always have en_US */
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45 _available_translations.push_back(QLocale("en_US"));
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 QDir dir(":/locale");
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 if (!dir.exists())
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 return;
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 QStringList translations = dir.entryList({"*.qm"}, QDir::Files);
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53 _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
54 for (const QString& str : translations)
109
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 _available_translations.push_back(QLocale(str.mid(0, str.lastIndexOf("."))));
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
118
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 109
diff changeset
58 bool Locale::IsLocaleAvailable(const QLocale& locale) const {
109
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59 for (const QLocale& l : _available_translations)
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 if (l == locale)
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61 return true;
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62 return false;
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63 }
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65 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
66 if (!IsLocaleAvailable(locale) || !qApp)
109
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
67 return false;
195
975a3f0965e2 locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
68
109
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
69 if (_locale == locale)
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70 return true; /* we're... already on this locale :) */
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
71
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
72 _locale = locale;
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
73 QLocale::setDefault(_locale);
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
74
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
75 /* we can still do stuff even if one thing fails! */
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
76 bool return_value = true;
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
77
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
78 const QString name = _locale.name();
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
79 if (!SwitchTranslator(_translator, QString(":/locale/%1.qm").arg(name)))
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
80 return_value = false;
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 path = qApp->applicationDirPath();
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
83 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
84 /* Sometimes Qt will have proper translations for the language, but not the specific
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
85 country. In that case, we still want to use that language. */
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
86 const int underscore_index = name.lastIndexOf("_");
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
87 if (!underscore_index)
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
88 return false;
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
89
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90 const QString short_name = name.mid(0, underscore_index);
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
91 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
92 return_value = 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
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
95 return return_value;
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
96 }
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 bool Locale::SwitchTranslator(QTranslator& translator, const QString& path) {
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
99 qApp->removeTranslator(&translator);
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 if (!translator.load(path))
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
102 return false;
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 118
diff changeset
103
109
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
104 qApp->installTranslator(&translator);
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
105 return true;
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
106 }
79714c95a145 *: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
107
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 195
diff changeset
108 } // namespace Locale