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_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
InitializeEmulator(m_gui_settings->GetCurrentUser().toStdString(), true, m_show_gui);
@ -52,6 +46,12 @@ void gui_application::Init()
if (m_show_gui)
{
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.
@ -80,7 +80,7 @@ void gui_application::Init()
#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
removeTranslator(&translator);
@ -96,22 +96,26 @@ void gui_application::SwitchTranslator(QTranslator& translator, const QString& f
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());
// 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;
}
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());
QLocale::setDefault(locale);
@ -120,19 +124,29 @@ void gui_application::LoadLanguage(const QString& language)
// As per QT recommendations to avoid conflicts for POSIX functions
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)
{
m_main_window->RetranslateUI();
const QString default_code = QLocale(QLocale::English).bcp47Name();
QStringList language_codes = GetAvailableLanguageCodes();
if (!language_codes.contains(default_code))
{
language_codes.prepend(default_code);
}
m_main_window->RetranslateUI(language_codes, m_language_code);
}
gui_log.notice("Current language changed to %s (%s)", locale_name.toStdString(), language.toStdString());
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::GetAvailableLocales()
QStringList gui_application::GetAvailableLanguageCodes()
{
QStringList locales;
QStringList language_codes;
const QString language_path = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
@ -143,15 +157,15 @@ QStringList gui_application::GetAvailableLocales()
for (const auto& filename : filenames)
{
QString locale = filename; // "rpcs3_en.qm"
locale.truncate(locale.lastIndexOf('.')); // "rpcs3_en"
locale.remove(0, locale.indexOf('_') + 1); // "en"
QString language_code = filename; // "rpcs3_en.qm"
language_code.truncate(language_code.lastIndexOf('.')); // "rpcs3_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()
@ -163,6 +177,7 @@ void gui_application::InitializeConnects()
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::NotifyEmuSettingsChange, this, &gui_application::OnEmuSettingsChange);

View File

@ -47,9 +47,9 @@ private:
return thread();
}
void SwitchTranslator(QTranslator& translator, const QString& filename, const QString& language);
void LoadLanguage(const QString& language);
QStringList GetAvailableLocales();
void SwitchTranslator(QTranslator& translator, const QString& filename, const QString& language_code);
void LoadLanguage(const QString& language_code);
QStringList GetAvailableLanguageCodes();
void InitializeCallbacks();
void InitializeConnects();
@ -59,7 +59,7 @@ private:
QTranslator m_translator;
QTranslator m_translator_qt;
QString m_language;
QString m_language_code;
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)
, 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()
@ -79,10 +83,6 @@ main_window::~main_window()
*/
void main_window::Init()
{
Q_INIT_RESOURCE(resources);
ui->setupUi(this);
setAcceptDrops(true);
// 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));
}
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()
{
if (m_gameListFrame)
@ -1168,11 +1194,11 @@ void main_window::RepaintGui()
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)
{

View File

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

View File

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