Mercurial > minori
annotate src/gui/locale.cc @ 118:39521c47c7a3
*: another huge megacommit, SORRY
The torrents page works a lot better now
Added the edit option to the anime list right click menu
Vectorized currently playing files
Available player and extensions are now loaded at runtime
from files in (dotpath)/players.json and (dotpath)/extensions.json
These paths are not permanent and will likely be moved to
(dotpath)/recognition
...
...
...
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Tue, 07 Nov 2023 23:40:54 -0500 |
parents | 79714c95a145 |
children | 9613d72b097e |
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" |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
3 #include <QTranslator> |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
4 #include <QLocale> |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
5 #include <QDir> |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
6 #include <QString> |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
7 #include <QApplication> |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
8 #include <iostream> |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
9 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
10 namespace Locale { |
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 std::string GetLocaleFullName(const QLocale& locale) { |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
13 QString res = QLocale::languageToString(locale.language()); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
14 if (locale.country() != QLocale::AnyCountry) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
15 res += " (" + QLocale::countryToString(locale.country()) + ")"; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
16 return Strings::ToUtf8String(res); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
17 } |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
18 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
19 Locale::Locale() { |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
20 RefreshAvailableLocales(); |
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 /* default to en_US */ |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
23 if (!IsLocaleAvailable(QLocale())) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
24 SetActiveLocale(QLocale("en_US")); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
25 else /* TODO: is this needed? */ |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
26 SetActiveLocale(QLocale()); |
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 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
29 Locale::Locale(const std::string& name) { |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
30 RefreshAvailableLocales(); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
31 SetActiveLocale(QLocale(Strings::ToQString(name))); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
32 } |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
33 |
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
109
diff
changeset
|
34 QLocale Locale::GetLocale() const { |
109
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
35 return _locale; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
36 } |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
37 |
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
109
diff
changeset
|
38 std::vector<QLocale> Locale::GetAvailableLocales() const { |
109
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
39 return _available_translations; |
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 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
42 void Locale::RefreshAvailableLocales() { |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
43 _available_translations.clear(); |
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 /* we will always have en_US */ |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
46 _available_translations.push_back(QLocale("en_US")); |
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 QDir dir(":/locale"); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
49 if (!dir.exists()) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
50 return; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
51 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
52 QStringList translations = dir.entryList({"*.qm"}, QDir::Files); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
53 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
54 _available_translations.reserve(translations.size()); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
55 for (const QString& str : translations) { |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
56 _available_translations.push_back(QLocale(str.mid(0, str.lastIndexOf(".")))); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
57 } |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
58 } |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
59 |
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
109
diff
changeset
|
60 bool Locale::IsLocaleAvailable(const QLocale& locale) const { |
109
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
61 for (const QLocale& l : _available_translations) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
62 if (l == locale) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
63 return true; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
64 return false; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
65 } |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
66 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
67 bool Locale::SetActiveLocale(const QLocale& locale) { |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
68 if (!IsLocaleAvailable(locale)) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
69 return false; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
70 if (_locale == locale) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
71 return true; /* we're... already on this locale :) */ |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
72 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
73 _locale = locale; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
74 QLocale::setDefault(_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 /* we can still do stuff even if one thing fails! */ |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
77 bool return_value = true; |
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 const QString name = _locale.name(); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
80 if (!SwitchTranslator(_translator, QString(":/locale/%1.qm").arg(name))) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
81 return_value = false; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
82 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
83 const QString path = qApp->applicationDirPath(); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
84 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
|
85 /* 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
|
86 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
|
87 const int underscore_index = name.lastIndexOf("_"); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
88 if (!underscore_index) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
89 return false; |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
90 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
91 const QString short_name = name.mid(0, underscore_index); |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
92 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
|
93 return_value = false; |
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 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
96 return return_value; |
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 bool Locale::SwitchTranslator(QTranslator& translator, const QString& path) { |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
100 qApp->removeTranslator(&translator); |
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 if (!translator.load(path)) |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
103 return false; |
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 |
79714c95a145
*: add translation files and locale files
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
108 } |