Mercurial > minori
comparison src/gui/locale.cc @ 195:975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
also the general application stuff and anime list is separated in settings
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Thu, 07 Dec 2023 11:14:01 -0500 |
| parents | 9613d72b097e |
| children | 862d0d8619f6 |
comparison
equal
deleted
inserted
replaced
| 194:8548dc425697 | 195:975a3f0965e2 |
|---|---|
| 1 #include "gui/locale.h" | 1 #include "gui/locale.h" |
| 2 #include "core/strings.h" | 2 #include "core/strings.h" |
| 3 | |
| 3 #include <QTranslator> | 4 #include <QTranslator> |
| 4 #include <QLocale> | 5 #include <QLocale> |
| 5 #include <QDir> | 6 #include <QDir> |
| 6 #include <QString> | 7 #include <QString> |
| 7 #include <QApplication> | 8 #include <QApplication> |
| 9 | |
| 10 #include <QDebug> | |
| 11 | |
| 8 #include <iostream> | 12 #include <iostream> |
| 9 | 13 |
| 10 namespace Locale { | 14 namespace Locale { |
| 11 | 15 |
| 12 std::string GetLocaleFullName(const QLocale& locale) { | 16 std::string GetLocaleFullName(const QLocale& locale) { |
| 16 return Strings::ToUtf8String(res); | 20 return Strings::ToUtf8String(res); |
| 17 } | 21 } |
| 18 | 22 |
| 19 Locale::Locale() { | 23 Locale::Locale() { |
| 20 RefreshAvailableLocales(); | 24 RefreshAvailableLocales(); |
| 21 | 25 SetActiveLocale(QLocale("en_US")); |
| 22 /* default to en_US */ | |
| 23 if (!IsLocaleAvailable(QLocale())) | |
| 24 SetActiveLocale(QLocale("en_US")); | |
| 25 else /* TODO: is this needed? */ | |
| 26 SetActiveLocale(QLocale()); | |
| 27 } | 26 } |
| 28 | 27 |
| 29 Locale::Locale(const std::string& name) { | 28 Locale::Locale(const std::string& name) { |
| 30 RefreshAvailableLocales(); | 29 RefreshAvailableLocales(); |
| 31 SetActiveLocale(QLocale(Strings::ToQString(name))); | 30 SetActiveLocale(QLocale(Strings::ToQString(name))); |
| 50 return; | 49 return; |
| 51 | 50 |
| 52 QStringList translations = dir.entryList({"*.qm"}, QDir::Files); | 51 QStringList translations = dir.entryList({"*.qm"}, QDir::Files); |
| 53 | 52 |
| 54 _available_translations.reserve(translations.size()); | 53 _available_translations.reserve(translations.size()); |
| 55 for (const QString& str : translations) { | 54 for (const QString& str : translations) |
| 56 _available_translations.push_back(QLocale(str.mid(0, str.lastIndexOf(".")))); | 55 _available_translations.push_back(QLocale(str.mid(0, str.lastIndexOf(".")))); |
| 57 } | |
| 58 } | 56 } |
| 59 | 57 |
| 60 bool Locale::IsLocaleAvailable(const QLocale& locale) const { | 58 bool Locale::IsLocaleAvailable(const QLocale& locale) const { |
| 61 for (const QLocale& l : _available_translations) | 59 for (const QLocale& l : _available_translations) |
| 62 if (l == locale) | 60 if (l == locale) |
| 63 return true; | 61 return true; |
| 64 return false; | 62 return false; |
| 65 } | 63 } |
| 66 | 64 |
| 67 bool Locale::SetActiveLocale(const QLocale& locale) { | 65 bool Locale::SetActiveLocale(const QLocale& locale) { |
| 68 if (!IsLocaleAvailable(locale)) | 66 if (!IsLocaleAvailable(locale) || !qApp) |
| 69 return false; | 67 return false; |
| 68 | |
| 70 if (_locale == locale) | 69 if (_locale == locale) |
| 71 return true; /* we're... already on this locale :) */ | 70 return true; /* we're... already on this locale :) */ |
| 72 | 71 |
| 73 _locale = locale; | 72 _locale = locale; |
| 74 QLocale::setDefault(_locale); | 73 QLocale::setDefault(_locale); |
