Fix several language selection issues (#994)

This commit is contained in:
Francesco Saltori 2023-10-16 13:41:06 +02:00 committed by GitHub
parent d4a2a8e8de
commit 13a50a915e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 51 deletions

View File

@ -99,29 +99,7 @@ bool CemuApp::OnInit()
wxInitAllImageHandlers(); wxInitAllImageHandlers();
LocalizeUI();
m_languages = GetAvailableLanguages();
const sint32 language = GetConfig().language;
const auto it = std::find_if(m_languages.begin(), m_languages.end(), [language](const wxLanguageInfo* info) { return info->Language == language; });
if (it != m_languages.end() && wxLocale::IsAvailable(language))
{
if (m_locale.Init(language))
{
m_locale.AddCatalogLookupPathPrefix(ActiveSettings::GetDataPath("resources").generic_string());
m_locale.AddCatalog("cemu");
}
}
if (!m_locale.IsOk())
{
if (!wxLocale::IsAvailable(wxLANGUAGE_DEFAULT) || !m_locale.Init(wxLANGUAGE_DEFAULT))
{
m_locale.Init(wxLANGUAGE_ENGLISH);
m_locale.AddCatalogLookupPathPrefix(ActiveSettings::GetDataPath("resources").generic_string());
m_locale.AddCatalog("cemu");
}
}
// fill colour db // fill colour db
wxTheColourDatabase->AddColour("ERROR", wxColour(0xCC, 0, 0)); wxTheColourDatabase->AddColour("ERROR", wxColour(0xCC, 0, 0));
@ -231,33 +209,44 @@ int CemuApp::FilterEvent(wxEvent& event)
return wxApp::FilterEvent(event); return wxApp::FilterEvent(event);
} }
std::vector<const wxLanguageInfo*> CemuApp::GetAvailableLanguages() std::vector<const wxLanguageInfo *> CemuApp::GetLanguages() const {
std::vector availableLanguages(m_availableTranslations);
availableLanguages.insert(availableLanguages.begin(), wxLocale::GetLanguageInfo(wxLANGUAGE_ENGLISH));
return availableLanguages;
}
void CemuApp::LocalizeUI()
{ {
const auto path = ActiveSettings::GetDataPath("resources"); std::unique_ptr<wxTranslations> translationsMgr(new wxTranslations());
if (!exists(path)) m_availableTranslations = GetAvailableTranslationLanguages(translationsMgr.get());
return {};
const sint32 configuredLanguage = GetConfig().language;
std::vector<const wxLanguageInfo*> result; bool isTranslationAvailable = std::any_of(m_availableTranslations.begin(), m_availableTranslations.end(),
for (const auto& p : fs::directory_iterator(path)) [configuredLanguage](const wxLanguageInfo* info) { return info->Language == configuredLanguage; });
if (configuredLanguage == wxLANGUAGE_DEFAULT || isTranslationAvailable)
{ {
if (!fs::is_directory(p)) translationsMgr->SetLanguage(static_cast<wxLanguage>(configuredLanguage));
continue; translationsMgr->AddCatalog("cemu");
const auto& path = p.path(); if (translationsMgr->IsLoaded("cemu") && wxLocale::IsAvailable(configuredLanguage))
auto filename = path.filename(); m_locale.Init(configuredLanguage);
const auto* lang_info = wxLocale::FindLanguageInfo(filename.c_str()); // This must be run after wxLocale::Init, as the latter sets up its own wxTranslations instance which we want to override
if (!lang_info) wxTranslations::Set(translationsMgr.release());
continue;
const auto language_file = path / "cemu.mo";
if (!fs::exists(language_file))
continue;
result.emplace_back(lang_info);
} }
}
return result; std::vector<const wxLanguageInfo*> CemuApp::GetAvailableTranslationLanguages(wxTranslations* translationsMgr)
{
wxFileTranslationsLoader::AddCatalogLookupPathPrefix(wxHelper::FromPath(ActiveSettings::GetDataPath("resources")));
std::vector<const wxLanguageInfo*> languages;
for (const auto& langName : translationsMgr->GetAvailableTranslations("cemu"))
{
const auto* langInfo = wxLocale::FindLanguageInfo(langName);
if (langInfo)
languages.emplace_back(langInfo);
}
return languages;
} }
void CemuApp::CreateDefaultFiles(bool first_start) void CemuApp::CreateDefaultFiles(bool first_start)

View File

@ -13,8 +13,7 @@ public:
void OnAssertFailure(const wxChar* file, int line, const wxChar* func, const wxChar* cond, const wxChar* msg) override; void OnAssertFailure(const wxChar* file, int line, const wxChar* func, const wxChar* cond, const wxChar* msg) override;
int FilterEvent(wxEvent& event) override; int FilterEvent(wxEvent& event) override;
const std::vector<const wxLanguageInfo*>& GetLanguages() const { return m_languages; } std::vector<const wxLanguageInfo*> GetLanguages() const;
static std::vector<const wxLanguageInfo*> GetAvailableLanguages();
static void CreateDefaultFiles(bool first_start = false); static void CreateDefaultFiles(bool first_start = false);
static bool TrySelectMLCPath(fs::path path); static bool TrySelectMLCPath(fs::path path);
@ -22,11 +21,13 @@ public:
private: private:
void ActivateApp(wxActivateEvent& event); void ActivateApp(wxActivateEvent& event);
void LocalizeUI();
static std::vector<const wxLanguageInfo*> GetAvailableTranslationLanguages(wxTranslations* translationsMgr);
MainWindow* m_mainFrame = nullptr; MainWindow* m_mainFrame = nullptr;
wxLocale m_locale; wxLocale m_locale;
std::vector<const wxLanguageInfo*> m_languages; std::vector<const wxLanguageInfo*> m_availableTranslations;
}; };
wxDECLARE_APP(CemuApp); wxDECLARE_APP(CemuApp);

View File

@ -123,7 +123,7 @@ wxPanel* GeneralSettings2::AddGeneralPage(wxNotebook* notebook)
first_row->Add(new wxStaticText(box, wxID_ANY, _("Language"), wxDefaultPosition, wxDefaultSize, 0), 0, wxALIGN_CENTER_VERTICAL | wxALL, 5); first_row->Add(new wxStaticText(box, wxID_ANY, _("Language"), wxDefaultPosition, wxDefaultSize, 0), 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
wxString language_choices[] = { _("Default"), "English" }; wxString language_choices[] = { _("Default") };
m_language = new wxChoice(box, wxID_ANY, wxDefaultPosition, wxDefaultSize, std::size(language_choices), language_choices); m_language = new wxChoice(box, wxID_ANY, wxDefaultPosition, wxDefaultSize, std::size(language_choices), language_choices);
m_language->SetSelection(0); m_language->SetSelection(0);
m_language->SetToolTip(_("Changes the interface language of Cemu\nAvailable languages are stored in the translation directory\nA restart will be required after changing the language")); m_language->SetToolTip(_("Changes the interface language of Cemu\nAvailable languages are stored in the translation directory\nA restart will be required after changing the language"));
@ -928,8 +928,6 @@ void GeneralSettings2::StoreConfig()
auto selection = m_language->GetSelection(); auto selection = m_language->GetSelection();
if (selection == 0) if (selection == 0)
GetConfig().language = wxLANGUAGE_DEFAULT; GetConfig().language = wxLANGUAGE_DEFAULT;
else if (selection == 1)
GetConfig().language = wxLANGUAGE_ENGLISH;
else else
{ {
const auto language = m_language->GetStringSelection(); const auto language = m_language->GetStringSelection();

View File

@ -1027,7 +1027,7 @@ void wxGameList::OnGameEntryUpdatedByTitleId(wxTitleIdEvent& event)
if (playTimeStat.last_played.year != 0) if (playTimeStat.last_played.year != 0)
{ {
const wxDateTime tmp((wxDateTime::wxDateTime_t)playTimeStat.last_played.day, (wxDateTime::Month)playTimeStat.last_played.month, (wxDateTime::wxDateTime_t)playTimeStat.last_played.year, 0, 0, 0, 0); const wxDateTime tmp((wxDateTime::wxDateTime_t)playTimeStat.last_played.day, (wxDateTime::Month)playTimeStat.last_played.month, (wxDateTime::wxDateTime_t)playTimeStat.last_played.year, 0, 0, 0, 0);
SetItem(index, ColumnGameStarted, tmp.FormatISODate()); SetItem(index, ColumnGameStarted, tmp.FormatDate());
} }
else else
SetItem(index, ColumnGameStarted, _("never")); SetItem(index, ColumnGameStarted, _("never"));