Qt: add language menu

This commit is contained in:
Megamouse 2020-02-05 11:07:34 +01:00
parent efe907ffae
commit c13d345604
5 changed files with 96 additions and 35 deletions

View file

@ -39,12 +39,6 @@ void gui_application::Init()
m_gui_settings.reset(new gui_settings()); m_gui_settings.reset(new gui_settings());
m_persistent_settings.reset(new persistent_settings()); m_persistent_settings.reset(new persistent_settings());
const auto locales = GetAvailableLocales();
const auto language = m_gui_settings->GetValue(gui::loc_language).toString();
const auto index = locales.indexOf(language);
LoadLanguage(index < 0 ? QLocale(QLocale::English).bcp47Name() : locales.at(index));
// Force init the emulator // Force init the emulator
InitializeEmulator(m_gui_settings->GetCurrentUser().toStdString(), true, m_show_gui); InitializeEmulator(m_gui_settings->GetCurrentUser().toStdString(), true, m_show_gui);
@ -52,6 +46,12 @@ void gui_application::Init()
if (m_show_gui) if (m_show_gui)
{ {
m_main_window = new main_window(m_gui_settings, m_emu_settings, m_persistent_settings, nullptr); m_main_window = new main_window(m_gui_settings, m_emu_settings, m_persistent_settings, nullptr);
const auto codes = GetAvailableLanguageCodes();
const auto language = m_gui_settings->GetValue(gui::loc_language).toString();
const auto index = codes.indexOf(language);
LoadLanguage(index < 0 ? QLocale(QLocale::English).bcp47Name() : codes.at(index));
} }
// Create callbacks from the emulator, which reference the handlers. // Create callbacks from the emulator, which reference the handlers.
@ -80,7 +80,7 @@ void gui_application::Init()
#endif #endif
} }
void gui_application::SwitchTranslator(QTranslator& translator, const QString& filename, const QString& language) void gui_application::SwitchTranslator(QTranslator& translator, const QString& filename, const QString& language_code)
{ {
// remove the old translator // remove the old translator
removeTranslator(&translator); removeTranslator(&translator);
@ -96,22 +96,26 @@ void gui_application::SwitchTranslator(QTranslator& translator, const QString& f
installTranslator(&translator); installTranslator(&translator);
} }
} }
else if (language != QLocale(QLocale::English).bcp47Name()) // ignore default case "en", since it is handled in source code else if (const QString default_code = QLocale(QLocale::English).bcp47Name(); language_code != default_code)
{ {
// show error, but ignore default case "en", since it is handled in source code
gui_log.error("No translation file found in: %s", file_path.toStdString()); gui_log.error("No translation file found in: %s", file_path.toStdString());
// reset current language to default "en"
m_language_code = default_code;
} }
} }
void gui_application::LoadLanguage(const QString& language) void gui_application::LoadLanguage(const QString& language_code)
{ {
if (m_language == language) if (m_language_code == language_code)
{ {
return; return;
} }
m_language = language; m_language_code = language_code;
const QLocale locale = QLocale(language); const QLocale locale = QLocale(language_code);
const QString locale_name = QLocale::languageToString(locale.language()); const QString locale_name = QLocale::languageToString(locale.language());
QLocale::setDefault(locale); QLocale::setDefault(locale);
@ -120,19 +124,29 @@ void gui_application::LoadLanguage(const QString& language)
// As per QT recommendations to avoid conflicts for POSIX functions // As per QT recommendations to avoid conflicts for POSIX functions
std::setlocale(LC_NUMERIC, "C"); std::setlocale(LC_NUMERIC, "C");
SwitchTranslator(m_translator, QStringLiteral("rpcs3_%1.qm").arg(language), language); SwitchTranslator(m_translator, QStringLiteral("rpcs3_%1.qm").arg(language_code), language_code);
if (m_main_window) if (m_main_window)
{ {
m_main_window->RetranslateUI(); const QString default_code = QLocale(QLocale::English).bcp47Name();
} QStringList language_codes = GetAvailableLanguageCodes();
gui_log.notice("Current language changed to %s (%s)", locale_name.toStdString(), language.toStdString()); if (!language_codes.contains(default_code))
}
QStringList gui_application::GetAvailableLocales()
{ {
QStringList locales; language_codes.prepend(default_code);
}
m_main_window->RetranslateUI(language_codes, m_language_code);
}
m_gui_settings->SetValue(gui::loc_language, m_language_code);
gui_log.notice("Current language changed to %s (%s)", locale_name.toStdString(), language_code.toStdString());
}
QStringList gui_application::GetAvailableLanguageCodes()
{
QStringList language_codes;
const QString language_path = QLibraryInfo::location(QLibraryInfo::TranslationsPath); const QString language_path = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
@ -143,15 +157,15 @@ QStringList gui_application::GetAvailableLocales()
for (const auto& filename : filenames) for (const auto& filename : filenames)
{ {
QString locale = filename; // "rpcs3_en.qm" QString language_code = filename; // "rpcs3_en.qm"
locale.truncate(locale.lastIndexOf('.')); // "rpcs3_en" language_code.truncate(language_code.lastIndexOf('.')); // "rpcs3_en"
locale.remove(0, locale.indexOf('_') + 1); // "en" language_code.remove(0, language_code.indexOf('_') + 1); // "en"
locales << locale; language_codes << language_code;
} }
} }
return locales; return language_codes;
} }
void gui_application::InitializeConnects() void gui_application::InitializeConnects()
@ -163,6 +177,7 @@ void gui_application::InitializeConnects()
if (m_main_window) if (m_main_window)
{ {
connect(m_main_window, &main_window::RequestLanguageChange, this, &gui_application::LoadLanguage);
connect(m_main_window, &main_window::RequestGlobalStylesheetChange, this, &gui_application::OnChangeStyleSheetRequest); connect(m_main_window, &main_window::RequestGlobalStylesheetChange, this, &gui_application::OnChangeStyleSheetRequest);
connect(m_main_window, &main_window::NotifyEmuSettingsChange, this, &gui_application::OnEmuSettingsChange); connect(m_main_window, &main_window::NotifyEmuSettingsChange, this, &gui_application::OnEmuSettingsChange);

View file

@ -47,9 +47,9 @@ private:
return thread(); return thread();
} }
void SwitchTranslator(QTranslator& translator, const QString& filename, const QString& language); void SwitchTranslator(QTranslator& translator, const QString& filename, const QString& language_code);
void LoadLanguage(const QString& language); void LoadLanguage(const QString& language_code);
QStringList GetAvailableLocales(); QStringList GetAvailableLanguageCodes();
void InitializeCallbacks(); void InitializeCallbacks();
void InitializeConnects(); void InitializeConnects();
@ -59,7 +59,7 @@ private:
QTranslator m_translator; QTranslator m_translator;
QTranslator m_translator_qt; QTranslator m_translator_qt;
QString m_language; QString m_language_code;
QElapsedTimer m_timer_playtime; QElapsedTimer m_timer_playtime;

View file

@ -67,6 +67,10 @@ main_window::main_window(std::shared_ptr<gui_settings> guiSettings, std::shared_
, m_sys_menu_opened(false) , m_sys_menu_opened(false)
, ui(new Ui::main_window) , ui(new Ui::main_window)
{ {
Q_INIT_RESOURCE(resources);
// We have to setup the ui before using a translation
ui->setupUi(this);
} }
main_window::~main_window() main_window::~main_window()
@ -79,10 +83,6 @@ main_window::~main_window()
*/ */
void main_window::Init() void main_window::Init()
{ {
Q_INIT_RESOURCE(resources);
ui->setupUi(this);
setAcceptDrops(true); setAcceptDrops(true);
// add toolbar widgets (crappy Qt designer is not able to) // add toolbar widgets (crappy Qt designer is not able to)
@ -1145,6 +1145,32 @@ void main_window::AddRecentAction(const q_string_pair& entry)
guiSettings->SetValue(gui::rg_entries, guiSettings->List2Var(m_rg_entries)); guiSettings->SetValue(gui::rg_entries, guiSettings->List2Var(m_rg_entries));
} }
void main_window::UpdateLanguageActions(const QStringList& language_codes, const QString& language_code)
{
ui->languageMenu->clear();
for (const auto& code : language_codes)
{
const QLocale locale = QLocale(code);
const QString locale_name = QLocale::languageToString(locale.language());
// create new action
QAction* act = new QAction(locale_name, this);
act->setData(code);
act->setToolTip(locale_name);
act->setCheckable(true);
act->setChecked(code == language_code);
// connect to language changer
connect(act, &QAction::triggered, [this, code]()
{
RequestLanguageChange(code);
});
ui->languageMenu->addAction(act);
}
}
void main_window::RepaintGui() void main_window::RepaintGui()
{ {
if (m_gameListFrame) if (m_gameListFrame)
@ -1168,11 +1194,11 @@ void main_window::RepaintGui()
Q_EMIT RequestTrophyManagerRepaint(); Q_EMIT RequestTrophyManagerRepaint();
} }
void main_window::RetranslateUI() void main_window::RetranslateUI(const QStringList& language_codes, const QString& language)
{ {
ui->retranslateUi(this); UpdateLanguageActions(language_codes, language);
RepaintGui(); ui->retranslateUi(this);
if (m_gameListFrame) if (m_gameListFrame)
{ {

View file

@ -74,6 +74,7 @@ public:
QIcon GetAppIcon(); QIcon GetAppIcon();
Q_SIGNALS: Q_SIGNALS:
void RequestLanguageChange(const QString& language);
void RequestGlobalStylesheetChange(const QString& sheetFilePath); void RequestGlobalStylesheetChange(const QString& sheetFilePath);
void RequestTrophyManagerRepaint(); void RequestTrophyManagerRepaint();
void NotifyEmuSettingsChange(); void NotifyEmuSettingsChange();
@ -86,7 +87,7 @@ public Q_SLOTS:
void OnEmuReady(); void OnEmuReady();
void RepaintGui(); void RepaintGui();
void RetranslateUI(); void RetranslateUI(const QStringList& language_codes, const QString& language);
private Q_SLOTS: private Q_SLOTS:
void OnPlayOrPause(); void OnPlayOrPause();
@ -131,6 +132,9 @@ private:
QAction* CreateRecentAction(const q_string_pair& entry, const uint& sc_idx); QAction* CreateRecentAction(const q_string_pair& entry, const uint& sc_idx);
void BootRecentAction(const QAction* act); void BootRecentAction(const QAction* act);
void AddRecentAction(const q_string_pair& entry); void AddRecentAction(const q_string_pair& entry);
void UpdateLanguageActions(const QStringList& language_codes, const QString& language);
void RemoveDiskCache(); void RemoveDiskCache();
q_pair_list m_rg_entries; q_pair_list m_rg_entries;

View file

@ -141,7 +141,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1058</width> <width>1058</width>
<height>20</height> <height>21</height>
</rect> </rect>
</property> </property>
<property name="contextMenuPolicy"> <property name="contextMenuPolicy">
@ -299,8 +299,16 @@
<property name="title"> <property name="title">
<string>Help</string> <string>Help</string>
</property> </property>
<widget class="QMenu" name="languageMenu">
<property name="title">
<string>Language</string>
</property>
<addaction name="languageAct0"/>
</widget>
<addaction name="updateAct"/> <addaction name="updateAct"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="languageMenu"/>
<addaction name="separator"/>
<addaction name="aboutAct"/> <addaction name="aboutAct"/>
<addaction name="aboutQtAct"/> <addaction name="aboutQtAct"/>
</widget> </widget>
@ -1017,6 +1025,14 @@
<string>Cheats</string> <string>Cheats</string>
</property> </property>
</action> </action>
<action name="languageAct0">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>English</string>
</property>
</action>
</widget> </widget>
<layoutdefault spacing="6" margin="11"/> <layoutdefault spacing="6" margin="11"/>
<resources> <resources>