comparison src/gui/locale.cc @ 202:71832ffe425a

animia: re-add kvm fd source this is all being merged from my wildly out-of-date laptop. SORRY! in other news, I edited the CI file to install the wayland client as well, so the linux CI build might finally get wayland stuff.
author Paper <paper@paper.us.eu.org>
date Tue, 02 Jan 2024 06:05:06 -0500
parents 975a3f0965e2
children 862d0d8619f6
comparison
equal deleted inserted replaced
201:8f6f8dd2eb23 202:71832ffe425a
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);
99 bool Locale::SwitchTranslator(QTranslator& translator, const QString& path) { 98 bool Locale::SwitchTranslator(QTranslator& translator, const QString& path) {
100 qApp->removeTranslator(&translator); 99 qApp->removeTranslator(&translator);
101 100
102 if (!translator.load(path)) 101 if (!translator.load(path))
103 return false; 102 return false;
103
104 qApp->installTranslator(&translator); 104 qApp->installTranslator(&translator);
105 return true; 105 return true;
106 } 106 }
107 107
108 } 108 }