Skip to content

Commit 6866848

Browse files
committed
Added Automatic Locale Detection
1 parent f410b52 commit 6866848

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

OpenRCT2Launcher/configuration.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,64 @@ Configuration::Configuration(QString file, QWidget *parent) :
112112
ui->fullscreenWidth->setValue(display.width());
113113
ui->fullscreenHeight->setValue(display.height());
114114
}
115+
116+
// Locale Stuff
117+
{
118+
QLocale locale;
119+
if (!config.value("language").isValid()) {
120+
QString name = locale.name();
121+
name = name.replace('_', '-');
122+
int ind = ui->languages->findData(langEquiv.value(name, name), Qt::UserRole, Qt::MatchExactly);
123+
124+
if (ind < 0) {
125+
QStringList languages = locale.uiLanguages();
126+
for (QString lang : languages) {
127+
lang = lang.replace('_', '-');
128+
ind = ui->languages->findData(langEquiv.value(lang, lang), Qt::UserRole, Qt::MatchExactly);
129+
if (ind >= 0) break;
130+
}
131+
}
132+
133+
if (ind < 0) {
134+
QStringList languages = locale.uiLanguages();
135+
for (QString lang : languages) {
136+
lang = lang.replace('_', '-');
137+
if (lang.contains('-')) lang.truncate(lang.indexOf('-'));
138+
ind = ui->languages->findData(lang, Qt::UserRole, Qt::MatchStartsWith);
139+
if (ind >= 0) break;
140+
}
141+
}
142+
143+
ui->languages->setCurrentIndex(ind);
144+
}
145+
if (!config.value("currency_format").isValid()) {
146+
int ind = ui->currencies->findData(locale.currencySymbol(QLocale::CurrencyIsoCode));
147+
if (ind >= 0) ui->currencies->setCurrentIndex(ind);
148+
}
149+
if (!config.value("measurement_format").isValid()) {
150+
ui->measurements->setCurrentIndex((locale.measurementSystem() == QLocale::MetricSystem) ? 1 : 0);
151+
}
152+
if (!config.value("temperature_format").isValid()) {
153+
ui->temperatures->setCurrentIndex((locale.measurementSystem() == QLocale::ImperialUSSystem) ? 1 : 0);
154+
}
155+
if (!config.value("date_format").isValid()) {
156+
QString df = locale.dateFormat();
157+
// dateFormat is not quite what we need, so I'll translate it
158+
int day = df.indexOf('d', 0, Qt::CaseInsensitive);
159+
int month = df.indexOf('M', 0, Qt::CaseInsensitive);
160+
int year = df.indexOf('y', 0, Qt::CaseInsensitive);
161+
// Assume that if year is not first, it's last
162+
if (day >= 0 && month >= 0 && year >= 0) {
163+
if (month < day) {
164+
if (year < month) ui->dates->setCurrentIndex(2);
165+
else ui->dates->setCurrentIndex(1);
166+
} else {
167+
if (year < day) ui->dates->setCurrentIndex(3);
168+
else ui->dates->setCurrentIndex(0);
169+
}
170+
}
171+
}
172+
}
115173
}
116174

117175
Configuration::~Configuration()

OpenRCT2Launcher/configuration.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define CONFIGURATION_H
33

44
#include <QDialog>
5+
#include <QMap>
56
#include <QSettings>
67

78
namespace Ui {
@@ -23,6 +24,7 @@ public slots:
2324
Ui::Configuration *ui;
2425
void setComboBoxData();
2526
QSettings config;
27+
QMap<QString, QString> langEquiv;
2628
};
2729

2830
#endif // CONFIGURATION_H

OpenRCT2Launcher/configuration.ui

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<item>
1818
<widget class="QTabWidget" name="tabWidget">
1919
<property name="currentIndex">
20-
<number>0</number>
20+
<number>3</number>
2121
</property>
2222
<widget class="QWidget" name="launcherTab">
2323
<attribute name="title">

OpenRCT2Launcher/configuration_data.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ void Configuration::setComboBoxData() {
6464
ui->titleSequence->setItemData(4, QStringLiteral("*OPENRCT2"));
6565

6666
ui->soundDevices->setItemData(0, QStringLiteral(""));
67+
68+
langEquiv.insert("en-CA", "en-US");
69+
langEquiv.insert("zh-CN", "zh-Hant");
70+
langEquiv.insert("zh-TW", "zh-Hans");
6771
}
6872

6973
#endif // CONFIGURATION_DATA_H

0 commit comments

Comments
 (0)