comparison src/gui/locale.cc @ 369:47c9f8502269

*: clang-format all the things I've edited the formatting a bit. Now pointer asterisks (and reference ampersands) are on the variable instead of the type, as well as having newlines for function braces (but nothing else)
author Paper <paper@tflc.us>
date Fri, 25 Jul 2025 10:16:02 -0400
parents b1f625b0227c
children
comparison
equal deleted inserted replaced
368:6d37a998cf91 369:47c9f8502269
9 9
10 #include <QtGlobal> 10 #include <QtGlobal>
11 11
12 namespace Locale { 12 namespace Locale {
13 13
14 std::string GetLocaleFullName(const QLocale& locale) { 14 std::string GetLocaleFullName(const QLocale &locale)
15 {
15 QString res = QLocale::languageToString(locale.language()); 16 QString res = QLocale::languageToString(locale.language());
16 #if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) 17 #if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
17 /* silence deprecation warning for locale.country() */ 18 /* silence deprecation warning for locale.country() */
18 if (locale.territory() != QLocale::AnyTerritory) 19 if (locale.territory() != QLocale::AnyTerritory)
19 res += " (" + QLocale::territoryToString(locale.territory()) + ")"; 20 res += " (" + QLocale::territoryToString(locale.territory()) + ")";
22 res += " (" + QLocale::countryToString(locale.country()) + ")"; 23 res += " (" + QLocale::countryToString(locale.country()) + ")";
23 #endif 24 #endif
24 return Strings::ToUtf8String(res); 25 return Strings::ToUtf8String(res);
25 } 26 }
26 27
27 Locale::Locale() { 28 Locale::Locale()
29 {
28 RefreshAvailableLocales(); 30 RefreshAvailableLocales();
29 SetActiveLocale(QLocale("en_US")); 31 SetActiveLocale(QLocale("en_US"));
30 } 32 }
31 33
32 Locale::Locale(const std::string& name) { 34 Locale::Locale(const std::string &name)
35 {
33 RefreshAvailableLocales(); 36 RefreshAvailableLocales();
34 SetActiveLocale(QLocale(Strings::ToQString(name))); 37 SetActiveLocale(QLocale(Strings::ToQString(name)));
35 } 38 }
36 39
37 QLocale Locale::GetLocale() const { 40 QLocale Locale::GetLocale() const
41 {
38 return _locale; 42 return _locale;
39 } 43 }
40 44
41 std::vector<QLocale> Locale::GetAvailableLocales() const { 45 std::vector<QLocale> Locale::GetAvailableLocales() const
46 {
42 return _available_translations; 47 return _available_translations;
43 } 48 }
44 49
45 void Locale::RefreshAvailableLocales() { 50 void Locale::RefreshAvailableLocales()
51 {
46 _available_translations.clear(); 52 _available_translations.clear();
47 53
48 /* we will always have en_US */ 54 /* we will always have en_US */
49 _available_translations.push_back(QLocale("en_US")); 55 _available_translations.push_back(QLocale("en_US"));
50 56
53 return; 59 return;
54 60
55 QStringList translations = dir.entryList({"*.qm"}, QDir::Files); 61 QStringList translations = dir.entryList({"*.qm"}, QDir::Files);
56 62
57 _available_translations.reserve(translations.size()); 63 _available_translations.reserve(translations.size());
58 for (const QString& str : translations) 64 for (const QString &str : translations)
59 _available_translations.push_back(QLocale(str.mid(0, str.lastIndexOf(".")))); 65 _available_translations.push_back(QLocale(str.mid(0, str.lastIndexOf("."))));
60 } 66 }
61 67
62 bool Locale::IsLocaleAvailable(const QLocale& locale) const { 68 bool Locale::IsLocaleAvailable(const QLocale &locale) const
63 for (const QLocale& l : _available_translations) 69 {
70 for (const QLocale &l : _available_translations)
64 if (l == locale) 71 if (l == locale)
65 return true; 72 return true;
66 return false; 73 return false;
67 } 74 }
68 75
69 bool Locale::SetActiveLocale(const QLocale& locale) { 76 bool Locale::SetActiveLocale(const QLocale &locale)
77 {
70 if (!IsLocaleAvailable(locale) || !qApp) 78 if (!IsLocaleAvailable(locale) || !qApp)
71 return false; 79 return false;
72 80
73 if (_locale == locale) 81 if (_locale == locale)
74 return true; /* we're... already on this locale :) */ 82 return true; /* we're... already on this locale :) */
97 } 105 }
98 106
99 return return_value; 107 return return_value;
100 } 108 }
101 109
102 bool Locale::SwitchTranslator(QTranslator& translator, const QString& path) { 110 bool Locale::SwitchTranslator(QTranslator &translator, const QString &path)
111 {
103 qApp->removeTranslator(&translator); 112 qApp->removeTranslator(&translator);
104 113
105 if (!translator.load(path)) 114 if (!translator.load(path))
106 return false; 115 return false;
107 116