mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-07-11 08:47:52 -06:00
Reworked switching of the language:
1) Language is
This commit is contained in:
parent
cd35fd9b34
commit
9e406e4214
2 changed files with 78 additions and 88 deletions
|
@ -229,7 +229,11 @@ bool GUI_App::on_init_inner()
|
||||||
init_label_colours();
|
init_label_colours();
|
||||||
init_fonts();
|
init_fonts();
|
||||||
|
|
||||||
load_language();
|
wxString language = wxEmptyString;
|
||||||
|
if (app_config->has("translation_language"))
|
||||||
|
language = app_config->get("translation_language");
|
||||||
|
// If load_language() fails, the application closes.
|
||||||
|
load_language(language);
|
||||||
|
|
||||||
// Suppress the '- default -' presets.
|
// Suppress the '- default -' presets.
|
||||||
preset_bundle->set_default_suppressed(app_config->get("no_defaults") == "1");
|
preset_bundle->set_default_suppressed(app_config->get("no_defaults") == "1");
|
||||||
|
@ -585,7 +589,6 @@ void GUI_App::import_model(wxWindow *parent, wxArrayString& input_files) const
|
||||||
bool GUI_App::switch_language()
|
bool GUI_App::switch_language()
|
||||||
{
|
{
|
||||||
if (select_language()) {
|
if (select_language()) {
|
||||||
save_language();
|
|
||||||
_3DScene::remove_all_canvases();
|
_3DScene::remove_all_canvases();
|
||||||
recreate_GUI();
|
recreate_GUI();
|
||||||
return true;
|
return true;
|
||||||
|
@ -597,57 +600,43 @@ bool GUI_App::switch_language()
|
||||||
// select language from the list of installed languages
|
// select language from the list of installed languages
|
||||||
bool GUI_App::select_language()
|
bool GUI_App::select_language()
|
||||||
{
|
{
|
||||||
const auto langs = get_installed_languages();
|
const std::vector<const wxLanguageInfo*> langs = get_installed_languages();
|
||||||
wxArrayString names;
|
wxArrayString names;
|
||||||
names.Alloc(langs.size());
|
names.Alloc(langs.size());
|
||||||
|
|
||||||
int init_selection = -1;
|
// Some valid language should be selected since the application start up.
|
||||||
const auto current_language = m_wxLocale ? m_wxLocale->GetLanguage() : wxLocale::GetSystemLanguage();
|
assert(m_wxLocale != nullptr);
|
||||||
|
const auto current_language = m_wxLocale->GetLanguage();
|
||||||
for (size_t i = 0; i < langs.size(); i++) {
|
int init_selection = 0;
|
||||||
const auto lang = langs[i]->Language;
|
for (size_t i = 0; i < langs.size(); ++ i) {
|
||||||
const bool is_english = lang >= wxLANGUAGE_ENGLISH && lang <= wxLANGUAGE_ENGLISH_ZIMBABWE;
|
if (langs[i]->Language == current_language)
|
||||||
|
|
||||||
if (lang == current_language || (current_language == wxLANGUAGE_UNKNOWN && is_english)) {
|
|
||||||
init_selection = i;
|
init_selection = i;
|
||||||
}
|
|
||||||
|
|
||||||
names.Add(langs[i]->Description);
|
names.Add(langs[i]->Description);
|
||||||
}
|
}
|
||||||
|
|
||||||
const long index = wxGetSingleChoiceIndex(
|
const long index = wxGetSingleChoiceIndex(_(L("Select the language")), _(L("Language")), names, init_selection);
|
||||||
_(L("Select the language")),
|
// Try to load a new language.
|
||||||
_(L("Language")), names, init_selection >= 0 ? init_selection : 0);
|
if (index != -1 && langs[index]->Language != current_language && this->load_language(langs[index]->CanonicalName)) {
|
||||||
|
// Save language at application config.
|
||||||
if (index != -1) {
|
app_config->set("translation_language", m_wxLocale->GetCanonicalName().ToUTF8().data());
|
||||||
const wxLanguageInfo *lang = langs[index];
|
app_config->save();
|
||||||
if (lang->Language == current_language) {
|
return true;
|
||||||
// There was no change
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_wxLocale = new wxLocale; // FIXME: leak?
|
|
||||||
m_wxLocale->Init(lang->Language);
|
|
||||||
m_wxLocale->AddCatalogLookupPathPrefix(from_u8(localization_dir()));
|
|
||||||
m_wxLocale->AddCatalog(SLIC3R_APP_KEY);
|
|
||||||
//FIXME This is a temporary workaround, the correct solution is to switch to "C" locale during file import / export only.
|
|
||||||
wxSetlocale(LC_NUMERIC, "C");
|
|
||||||
Preset::update_suffix_modified();
|
|
||||||
m_imgui->set_language(into_u8(lang->CanonicalName));
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get the language code before underscore, if there is underscore.
|
||||||
|
static wxString language_code_short(const wxString &language_code)
|
||||||
|
{
|
||||||
|
size_t idx_underscore = language_code.find('_');
|
||||||
|
return (idx_underscore != wxString::npos) ? language_code.substr(0, idx_underscore) : language_code;
|
||||||
|
}
|
||||||
|
|
||||||
// Load gettext translation files and activate them at the start of the application,
|
// Load gettext translation files and activate them at the start of the application,
|
||||||
// based on the "translation_language" key stored in the application config.
|
// based on the "translation_language" key stored in the application config.
|
||||||
bool GUI_App::load_language()
|
bool GUI_App::load_language(wxString language)
|
||||||
{
|
{
|
||||||
wxString language = wxEmptyString;
|
|
||||||
if (app_config->has("translation_language"))
|
|
||||||
language = app_config->get("translation_language");
|
|
||||||
|
|
||||||
if (language.IsEmpty()) {
|
if (language.IsEmpty()) {
|
||||||
int lang = wxLocale::GetSystemLanguage();
|
int lang = wxLocale::GetSystemLanguage();
|
||||||
if (lang != wxLANGUAGE_UNKNOWN) {
|
if (lang != wxLANGUAGE_UNKNOWN) {
|
||||||
|
@ -657,67 +646,74 @@ bool GUI_App::load_language()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (language.IsEmpty())
|
||||||
|
language = "en_US";
|
||||||
|
|
||||||
const wxLanguageInfo *info = nullptr;
|
const wxLanguageInfo *info = nullptr;
|
||||||
if (! language.IsEmpty()) {
|
for (const wxLanguageInfo *this_info : get_installed_languages()) {
|
||||||
const auto langs = get_installed_languages();
|
if (this_info->CanonicalName == language) {
|
||||||
for (const wxLanguageInfo *this_info : langs)
|
// The language matches exactly, including the country suffix. Use it.
|
||||||
if (this_info->CanonicalName == language) {
|
info = this_info;
|
||||||
info = this_info;
|
break;
|
||||||
break;
|
}
|
||||||
}
|
if (language_code_short(this_info->CanonicalName) == language_code_short(language))
|
||||||
|
// Alternatively try to match just the language without the country suffix.
|
||||||
|
info = this_info;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_wxLocale = new wxLocale;
|
wxString canonical_name = info->CanonicalName;
|
||||||
if (info == nullptr) {
|
if (info == nullptr) {
|
||||||
m_wxLocale->Init(wxLANGUAGE_DEFAULT);
|
// Fallback for user languages, for which we do not have dictionaries.
|
||||||
m_imgui->set_language("en");
|
canonical_name = "en_EN";
|
||||||
} else {
|
info = wxLocale::GetLanguageInfo(wxLANGUAGE_ENGLISH_US);
|
||||||
m_wxLocale->Init(info->Language);
|
}
|
||||||
m_wxLocale->AddCatalogLookupPathPrefix(from_u8(localization_dir()));
|
|
||||||
m_wxLocale->AddCatalog(SLIC3R_APP_KEY);
|
wxLocale *new_locale = new wxLocale;
|
||||||
m_imgui->set_language(into_u8(info->CanonicalName));
|
if (info == nullptr || ! new_locale->Init(info->Language)) {
|
||||||
|
// Loading the language dictionary failed.
|
||||||
|
wxString message = "Switching PrusaSlicer to language " + canonical_name + " failed.";
|
||||||
|
#if !defined(_WIN32) && !defined(__APPLE__)
|
||||||
|
// likely some linux system
|
||||||
|
"\nYou may need to reconfigure the missing locales, likely by running the \"locale-gen\" and \"dpkg-reconfigure locales\" commands.\n";
|
||||||
|
#endif
|
||||||
|
delete new_locale;
|
||||||
|
if (m_wxLocale == nullptr)
|
||||||
|
message + "\n\nApplication will close.";
|
||||||
|
wxMessageBox(message, "PrusaSlicer - Switching language failed", wxOK | wxICON_ERROR);
|
||||||
|
if (m_wxLocale == nullptr)
|
||||||
|
std::terminate();
|
||||||
|
else
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
delete m_wxLocale;
|
||||||
|
m_wxLocale = new_locale;
|
||||||
|
m_wxLocale->AddCatalogLookupPathPrefix(from_u8(localization_dir()));
|
||||||
|
m_wxLocale->AddCatalog(SLIC3R_APP_KEY);
|
||||||
|
m_imgui->set_language(into_u8(info->CanonicalName));
|
||||||
//FIXME This is a temporary workaround, the correct solution is to switch to "C" locale during file import / export only.
|
//FIXME This is a temporary workaround, the correct solution is to switch to "C" locale during file import / export only.
|
||||||
wxSetlocale(LC_NUMERIC, "C");
|
wxSetlocale(LC_NUMERIC, "C");
|
||||||
Preset::update_suffix_modified();
|
Preset::update_suffix_modified();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// save language at application config
|
// Get a list of installed languages (languages for which we have dictionaries).
|
||||||
void GUI_App::save_language()
|
|
||||||
{
|
|
||||||
wxString language = wxEmptyString;
|
|
||||||
if (m_wxLocale)
|
|
||||||
language = m_wxLocale->GetCanonicalName();
|
|
||||||
|
|
||||||
app_config->set("translation_language", language.ToUTF8().data());
|
|
||||||
app_config->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get a list of installed languages
|
|
||||||
std::vector<const wxLanguageInfo*> GUI_App::get_installed_languages()
|
std::vector<const wxLanguageInfo*> GUI_App::get_installed_languages()
|
||||||
{
|
{
|
||||||
std::vector<const wxLanguageInfo*> res;
|
|
||||||
|
|
||||||
wxDir dir(from_u8(localization_dir()));
|
wxDir dir(from_u8(localization_dir()));
|
||||||
wxString filename;
|
wxString filename;
|
||||||
const wxLanguageInfo * langinfo;
|
std::vector<const wxLanguageInfo*> res;
|
||||||
wxString name = wxLocale::GetLanguageName(wxLANGUAGE_DEFAULT);
|
res.emplace_back(wxLocale::FindLanguageInfo("en_EN"));
|
||||||
if (!name.IsEmpty()) {
|
|
||||||
res.push_back(wxLocale::GetLanguageInfo(wxLANGUAGE_DEFAULT));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (bool cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_DIRS); cont; cont = dir.GetNext(&filename)) {
|
for (bool cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_DIRS); cont; cont = dir.GetNext(&filename)) {
|
||||||
langinfo = wxLocale::FindLanguageInfo(filename);
|
const wxLanguageInfo *langinfo = wxLocale::FindLanguageInfo(filename);
|
||||||
if (langinfo != NULL) {
|
if (langinfo != nullptr) {
|
||||||
auto full_file_name = dir.GetName() + wxFileName::GetPathSeparator() +
|
auto full_file_name = dir.GetName() + wxFileName::GetPathSeparator() +
|
||||||
filename + wxFileName::GetPathSeparator() + SLIC3R_APP_KEY + wxT(".mo");
|
filename + wxFileName::GetPathSeparator() + SLIC3R_APP_KEY + wxT(".mo");
|
||||||
if (wxFileExists(full_file_name)) {
|
if (wxFileExists(full_file_name))
|
||||||
res.push_back(langinfo);
|
res.push_back(langinfo);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Remove duplicated "en_EN" and possible others.
|
||||||
|
sort_remove_duplicates(res);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1001,10 +997,6 @@ int GUI_App::extruders_edited_cnt() const
|
||||||
wxString GUI_App::current_language_code_safe() const
|
wxString GUI_App::current_language_code_safe() const
|
||||||
{
|
{
|
||||||
// Translate the language code to a code, for which Prusa Research maintains translations.
|
// Translate the language code to a code, for which Prusa Research maintains translations.
|
||||||
wxString language_code = this->current_language_code();
|
|
||||||
size_t idx_underscore = language_code.find(language_code);
|
|
||||||
if (idx_underscore != wxString::npos)
|
|
||||||
language_code = language_code.substr(0, idx_underscore);
|
|
||||||
const std::map<wxString, wxString> mapping {
|
const std::map<wxString, wxString> mapping {
|
||||||
{ "cs", "cs_CZ", },
|
{ "cs", "cs_CZ", },
|
||||||
{ "sk", "cs_CZ", },
|
{ "sk", "cs_CZ", },
|
||||||
|
@ -1018,6 +1010,7 @@ wxString GUI_App::current_language_code_safe() const
|
||||||
{ "uk", "uk_UA", },
|
{ "uk", "uk_UA", },
|
||||||
{ "zh", "zh_CN", },
|
{ "zh", "zh_CN", },
|
||||||
};
|
};
|
||||||
|
wxString language_code = language_code_short(this->current_language_code());
|
||||||
auto it = mapping.find(language_code);
|
auto it = mapping.find(language_code);
|
||||||
if (it != mapping.end())
|
if (it != mapping.end())
|
||||||
language_code = it->second;
|
language_code = it->second;
|
||||||
|
|
|
@ -132,9 +132,7 @@ public:
|
||||||
void update_ui_from_settings();
|
void update_ui_from_settings();
|
||||||
|
|
||||||
bool switch_language();
|
bool switch_language();
|
||||||
// Load gettext translation files and activate them at the start of the application,
|
bool load_language(wxString language);
|
||||||
// based on the "translation_language" key stored in the application config.
|
|
||||||
bool load_language();
|
|
||||||
|
|
||||||
Tab* get_tab(Preset::Type type);
|
Tab* get_tab(Preset::Type type);
|
||||||
ConfigOptionMode get_mode();
|
ConfigOptionMode get_mode();
|
||||||
|
@ -146,9 +144,9 @@ public:
|
||||||
bool checked_tab(Tab* tab);
|
bool checked_tab(Tab* tab);
|
||||||
void load_current_presets();
|
void load_current_presets();
|
||||||
|
|
||||||
wxString current_language_code() const { return m_wxLocale != nullptr ? m_wxLocale->GetCanonicalName() : wxString("en_US"); }
|
wxString current_language_code() const { assert(m_wxLocale != nullptr); return m_wxLocale->GetCanonicalName(); }
|
||||||
// Translate the language code to a code, for which Prusa Research maintains translations. Defaults to "en_US".
|
// Translate the language code to a code, for which Prusa Research maintains translations. Defaults to "en_US".
|
||||||
wxString current_language_code_safe() const;
|
wxString current_language_code_safe() const;
|
||||||
|
|
||||||
virtual bool OnExceptionInMainLoop();
|
virtual bool OnExceptionInMainLoop();
|
||||||
|
|
||||||
|
@ -189,7 +187,6 @@ private:
|
||||||
void window_pos_restore(wxTopLevelWindow* window, const std::string &name, bool default_maximized = false);
|
void window_pos_restore(wxTopLevelWindow* window, const std::string &name, bool default_maximized = false);
|
||||||
void window_pos_sanitize(wxTopLevelWindow* window);
|
void window_pos_sanitize(wxTopLevelWindow* window);
|
||||||
bool select_language();
|
bool select_language();
|
||||||
void save_language();
|
|
||||||
std::vector<const wxLanguageInfo*> get_installed_languages();
|
std::vector<const wxLanguageInfo*> get_installed_languages();
|
||||||
#ifdef __WXMSW__
|
#ifdef __WXMSW__
|
||||||
void associate_3mf_files();
|
void associate_3mf_files();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue