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();
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");
}
}
LocalizeUI();
// fill colour db
wxTheColourDatabase->AddColour("ERROR", wxColour(0xCC, 0, 0));
@ -231,33 +209,44 @@ int CemuApp::FilterEvent(wxEvent& 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");
if (!exists(path))
return {};
std::vector<const wxLanguageInfo*> result;
for (const auto& p : fs::directory_iterator(path))
std::unique_ptr<wxTranslations> translationsMgr(new wxTranslations());
m_availableTranslations = GetAvailableTranslationLanguages(translationsMgr.get());
const sint32 configuredLanguage = GetConfig().language;
bool isTranslationAvailable = std::any_of(m_availableTranslations.begin(), m_availableTranslations.end(),
[configuredLanguage](const wxLanguageInfo* info) { return info->Language == configuredLanguage; });
if (configuredLanguage == wxLANGUAGE_DEFAULT || isTranslationAvailable)
{
if (!fs::is_directory(p))
continue;
translationsMgr->SetLanguage(static_cast<wxLanguage>(configuredLanguage));
translationsMgr->AddCatalog("cemu");
const auto& path = p.path();
auto filename = path.filename();
if (translationsMgr->IsLoaded("cemu") && wxLocale::IsAvailable(configuredLanguage))
m_locale.Init(configuredLanguage);
const auto* lang_info = wxLocale::FindLanguageInfo(filename.c_str());
if (!lang_info)
continue;
const auto language_file = path / "cemu.mo";
if (!fs::exists(language_file))
continue;
result.emplace_back(lang_info);
// This must be run after wxLocale::Init, as the latter sets up its own wxTranslations instance which we want to override
wxTranslations::Set(translationsMgr.release());
}
}
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)

View File

@ -13,8 +13,7 @@ public:
void OnAssertFailure(const wxChar* file, int line, const wxChar* func, const wxChar* cond, const wxChar* msg) override;
int FilterEvent(wxEvent& event) override;
const std::vector<const wxLanguageInfo*>& GetLanguages() const { return m_languages; }
static std::vector<const wxLanguageInfo*> GetAvailableLanguages();
std::vector<const wxLanguageInfo*> GetLanguages() const;
static void CreateDefaultFiles(bool first_start = false);
static bool TrySelectMLCPath(fs::path path);
@ -22,11 +21,13 @@ public:
private:
void ActivateApp(wxActivateEvent& event);
void LocalizeUI();
static std::vector<const wxLanguageInfo*> GetAvailableTranslationLanguages(wxTranslations* translationsMgr);
MainWindow* m_mainFrame = nullptr;
wxLocale m_locale;
std::vector<const wxLanguageInfo*> m_languages;
std::vector<const wxLanguageInfo*> m_availableTranslations;
};
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);
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->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"));
@ -928,8 +928,6 @@ void GeneralSettings2::StoreConfig()
auto selection = m_language->GetSelection();
if (selection == 0)
GetConfig().language = wxLANGUAGE_DEFAULT;
else if (selection == 1)
GetConfig().language = wxLANGUAGE_ENGLISH;
else
{
const auto language = m_language->GetStringSelection();

View File

@ -1027,7 +1027,7 @@ void wxGameList::OnGameEntryUpdatedByTitleId(wxTitleIdEvent& event)
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);
SetItem(index, ColumnGameStarted, tmp.FormatISODate());
SetItem(index, ColumnGameStarted, tmp.FormatDate());
}
else
SetItem(index, ColumnGameStarted, _("never"));