Skip to content

Commit

Permalink
Fix several language selection issues (#994)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fs00 authored Oct 16, 2023
1 parent d4a2a8e commit 13a50a9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 51 deletions.
77 changes: 33 additions & 44 deletions src/gui/CemuApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -231,33 +209,44 @@ int CemuApp::FilterEvent(wxEvent& event)
return wxApp::FilterEvent(event);
}

std::vector<const wxLanguageInfo*> CemuApp::GetAvailableLanguages()
{
const auto path = ActiveSettings::GetDataPath("resources");
if (!exists(path))
return {};

std::vector<const wxLanguageInfo*> result;
for (const auto& p : fs::directory_iterator(path))
{
if (!fs::is_directory(p))
continue;
std::vector<const wxLanguageInfo *> CemuApp::GetLanguages() const {
std::vector availableLanguages(m_availableTranslations);
availableLanguages.insert(availableLanguages.begin(), wxLocale::GetLanguageInfo(wxLANGUAGE_ENGLISH));
return availableLanguages;
}

const auto& path = p.path();
auto filename = path.filename();
void CemuApp::LocalizeUI()
{
std::unique_ptr<wxTranslations> translationsMgr(new wxTranslations());
m_availableTranslations = GetAvailableTranslationLanguages(translationsMgr.get());

const auto* lang_info = wxLocale::FindLanguageInfo(filename.c_str());
if (!lang_info)
continue;
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)
{
translationsMgr->SetLanguage(static_cast<wxLanguage>(configuredLanguage));
translationsMgr->AddCatalog("cemu");

const auto language_file = path / "cemu.mo";
if (!fs::exists(language_file))
continue;
if (translationsMgr->IsLoaded("cemu") && wxLocale::IsAvailable(configuredLanguage))
m_locale.Init(configuredLanguage);

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)
Expand Down
7 changes: 4 additions & 3 deletions src/gui/CemuApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@ class CemuApp : public wxApp
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);
static bool SelectMLCPath(wxWindow* parent = nullptr);

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);
4 changes: 1 addition & 3 deletions src/gui/GeneralSettings2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/components/wxGameList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down

0 comments on commit 13a50a9

Please sign in to comment.