Skip to content

Commit ef320c6

Browse files
committed
Qt: Cache app locale object
1 parent f91ee34 commit ef320c6

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

src/duckstation-qt/mainwindow.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ void MainWindow::populateGameListContextMenu(const GameList::Entry* entry, QWidg
887887
if (!entry->serial.empty())
888888
{
889889
std::vector<SaveStateInfo> available_states(System::GetAvailableSaveStates(entry->serial));
890-
const QString timestamp_format = QLocale::system().dateTimeFormat(QLocale::ShortFormat);
890+
const QString timestamp_format = QtHost::GetApplicationLocale().dateTimeFormat(QLocale::ShortFormat);
891891
for (SaveStateInfo& ssi : available_states)
892892
{
893893
if (ssi.global)
@@ -957,7 +957,7 @@ void MainWindow::populateGameListContextMenu(const GameList::Entry* entry, QWidg
957957
QString MainWindow::formatTimestampForSaveStateMenu(u64 timestamp)
958958
{
959959
const QDateTime qtime(QDateTime::fromSecsSinceEpoch(static_cast<qint64>(timestamp)));
960-
return qtime.toString(QLocale::system().dateTimeFormat(QLocale::ShortFormat));
960+
return qtime.toString(QtHost::GetApplicationLocale().dateTimeFormat(QLocale::ShortFormat));
961961
}
962962

963963
void MainWindow::populateLoadStateMenu(std::string_view game_serial, QMenu* menu)

src/duckstation-qt/qthost.cpp

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@
5959
#include <QtCore/QDateTime>
6060
#include <QtCore/QDebug>
6161
#include <QtCore/QEventLoop>
62-
#include <QtCore/QTranslator>
6362
#include <QtCore/QFile>
6463
#include <QtCore/QTimer>
64+
#include <QtCore/QTranslator>
6565
#include <QtCore/QtLogging>
6666
#include <QtGui/QClipboard>
6767
#include <QtGui/QKeyEvent>
@@ -123,6 +123,7 @@ static void MigrateSettings();
123123
static void SaveSettings();
124124
static bool RunSetupWizard();
125125
static void UpdateFontOrder(std::string_view language);
126+
static void UpdateApplicationLocale(std::string_view language);
126127
static std::optional<bool> DownloadFile(QWidget* parent, const QString& title, std::string url, std::vector<u8>* data);
127128
static void InitializeEarlyConsole();
128129
static void HookSignals();
@@ -135,6 +136,7 @@ static bool ParseCommandLineParametersAndInitializeConfig(QApplication& app,
135136
static INISettingsInterface s_base_settings_interface;
136137
static std::unique_ptr<QTimer> s_settings_save_timer;
137138
static std::vector<QTranslator*> s_translators;
139+
static QLocale s_app_locale;
138140
static bool s_batch_mode = false;
139141
static bool s_nogui_mode = false;
140142
static bool s_start_fullscreen_ui = false;
@@ -2180,7 +2182,6 @@ bool Host::CopyTextToClipboard(std::string_view text)
21802182

21812183
std::string Host::FormatNumber(NumberFormatType type, s64 value)
21822184
{
2183-
const QLocale loc = QLocale::system();
21842185
std::string ret;
21852186

21862187
if (type >= NumberFormatType::ShortDate && type <= NumberFormatType::LongDateTime)
@@ -2190,18 +2191,20 @@ std::string Host::FormatNumber(NumberFormatType type, s64 value)
21902191
{
21912192
case NumberFormatType::ShortDate:
21922193
case NumberFormatType::LongDate:
2193-
format = loc.dateFormat((type == NumberFormatType::LongDate) ? QLocale::LongFormat : QLocale::ShortFormat);
2194+
format =
2195+
s_app_locale.dateFormat((type == NumberFormatType::LongDate) ? QLocale::LongFormat : QLocale::ShortFormat);
21942196
break;
21952197

21962198
case NumberFormatType::ShortTime:
21972199
case NumberFormatType::LongTime:
2198-
format = loc.timeFormat((type == NumberFormatType::LongTime) ? QLocale::LongFormat : QLocale::ShortFormat);
2200+
format =
2201+
s_app_locale.timeFormat((type == NumberFormatType::LongTime) ? QLocale::LongFormat : QLocale::ShortFormat);
21992202
break;
22002203

22012204
case NumberFormatType::ShortDateTime:
22022205
case NumberFormatType::LongDateTime:
2203-
format =
2204-
loc.dateTimeFormat((type == NumberFormatType::LongDateTime) ? QLocale::LongFormat : QLocale::ShortFormat);
2206+
format = s_app_locale.dateTimeFormat((type == NumberFormatType::LongDateTime) ? QLocale::LongFormat :
2207+
QLocale::ShortFormat);
22052208
break;
22062209

22072210
DefaultCaseIsUnreachable();
@@ -2211,22 +2214,21 @@ std::string Host::FormatNumber(NumberFormatType type, s64 value)
22112214
}
22122215
else
22132216
{
2214-
ret = loc.toString(value).toStdString();
2217+
ret = s_app_locale.toString(value).toStdString();
22152218
}
22162219

22172220
return ret;
22182221
}
22192222

22202223
std::string Host::FormatNumber(NumberFormatType type, double value)
22212224
{
2222-
const QLocale loc = QLocale::system();
22232225
std::string ret;
22242226

22252227
switch (type)
22262228
{
22272229
case NumberFormatType::Number:
22282230
default:
2229-
ret = loc.toString(value).toStdString();
2231+
ret = s_app_locale.toString(value).toStdString();
22302232
break;
22312233
}
22322234

@@ -2309,6 +2311,7 @@ void QtHost::UpdateApplicationLanguage(QWidget* dialog_parent)
23092311

23102312
// We end up here both on language change, and on startup.
23112313
UpdateFontOrder(language);
2314+
UpdateApplicationLocale(language);
23122315
}
23132316

23142317
s32 Host::Internal::GetTranslatedStringImpl(std::string_view context, std::string_view msg,
@@ -2423,6 +2426,28 @@ void QtHost::UpdateFontOrder(std::string_view language)
24232426
}
24242427
}
24252428

2429+
const QLocale& QtHost::GetApplicationLocale()
2430+
{
2431+
return s_app_locale;
2432+
}
2433+
2434+
void QtHost::UpdateApplicationLocale(std::string_view language)
2435+
{
2436+
#if 0
2437+
// Only for testing purposes. Keep the system locale for users.
2438+
if (language == "ja")
2439+
s_app_locale = QLocale(QLocale::Japanese, QLocale::Japan);
2440+
else if (language == "ko")
2441+
s_app_locale = QLocale(QLocale::Korean, QLocale::SouthKorea);
2442+
else if (language == "zh-CN")
2443+
s_app_locale = QLocale(QLocale::Chinese, QLocale::China);
2444+
else
2445+
s_app_locale = QLocale::system();
2446+
#else
2447+
s_app_locale = QLocale::system();
2448+
#endif
2449+
}
2450+
24262451
void Host::ReportDebuggerMessage(std::string_view message)
24272452
{
24282453
INFO_LOG("Debugger message: {}", message);

src/duckstation-qt/qthost.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ class QtAsyncTask : public QObject
333333
extern EmuThread* g_emu_thread;
334334

335335
namespace QtHost {
336+
/// Returns the locale to use for date/time formatting, etc.
337+
const QLocale& GetApplicationLocale();
338+
336339
/// Default theme name for the platform.
337340
const char* GetDefaultThemeName();
338341

0 commit comments

Comments
 (0)