Refactor language switch, infer user's language on fresh startup

This commit is contained in:
Vojtech Kral 2019-05-03 16:27:56 +02:00
parent ba297a929a
commit 658e9c2b1b
2 changed files with 59 additions and 53 deletions

View file

@ -3,6 +3,8 @@
#include "GUI_ObjectManipulation.hpp" #include "GUI_ObjectManipulation.hpp"
#include "I18N.hpp" #include "I18N.hpp"
#include <algorithm>
#include <iterator>
#include <exception> #include <exception>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
@ -21,6 +23,7 @@
#include <wx/sysopt.h> #include <wx/sysopt.h>
#include <wx/msgdlg.h> #include <wx/msgdlg.h>
#include <wx/log.h> #include <wx/log.h>
#include <wx/intl.h>
#include "libslic3r/Utils.hpp" #include "libslic3r/Utils.hpp"
#include "libslic3r/Model.hpp" #include "libslic3r/Model.hpp"
@ -516,10 +519,7 @@ void GUI_App::import_model(wxWindow *parent, wxArrayString& input_files)
bool GUI_App::switch_language() bool GUI_App::switch_language()
{ {
wxArrayString names; if (select_language()) {
wxArrayLong identifiers;
get_installed_languages(names, identifiers);
if (select_language(names, identifiers)) {
save_language(); save_language();
_3DScene::remove_all_canvases(); _3DScene::remove_all_canvases();
recreate_GUI(); recreate_GUI();
@ -530,34 +530,48 @@ 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( wxArrayString & names, bool GUI_App::select_language()
wxArrayLong & identifiers)
{ {
wxCHECK_MSG(names.Count() == identifiers.Count(), false, const auto langs = get_installed_languages();
_(L("Array of language names and identifiers should have the same size."))); wxArrayString names;
int init_selection = 0; names.Alloc(langs.size());
long current_language = m_wxLocale ? m_wxLocale->GetLanguage() : wxLANGUAGE_UNKNOWN;
for (auto lang : identifiers) { int init_selection = -1;
if (lang == current_language) const auto current_language = m_wxLocale ? m_wxLocale->GetLanguage() : wxLocale::GetSystemLanguage();
break;
++init_selection; for (size_t i = 0; i < langs.size(); i++) {
const auto lang = langs[i]->Language;
const bool is_english = lang >= wxLANGUAGE_ENGLISH && lang <= wxLANGUAGE_ENGLISH_ZIMBABWE;
if (lang == current_language || (current_language == wxLANGUAGE_UNKNOWN && is_english)) {
init_selection = i;
}
names.Add(langs[i]->Description);
} }
if (init_selection == identifiers.size())
init_selection = 0; const long index = wxGetSingleChoiceIndex(
long index = wxGetSingleChoiceIndex(_(L("Select the language")), _(L("Language")), _(L("Select the language")),
names, init_selection); _(L("Language")), names, init_selection >= 0 ? init_selection : 0);
if (index != -1)
{ if (index != -1) {
m_wxLocale = new wxLocale; const wxLanguageInfo *lang = langs[index];
m_wxLocale->Init(identifiers[index]); if (lang->Language == current_language) {
// 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->AddCatalogLookupPathPrefix(from_u8(localization_dir()));
m_wxLocale->AddCatalog("Slic3rPE"); m_wxLocale->AddCatalog("Slic3rPE");
//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();
m_imgui->set_language(m_wxLocale->GetCanonicalName().ToUTF8().data()); m_imgui->set_language(into_u8(lang->CanonicalName));
return true; return true;
} }
return false; return false;
} }
@ -570,21 +584,20 @@ bool GUI_App::load_language()
if (language.IsEmpty()) if (language.IsEmpty())
return false; return false;
wxArrayString names;
wxArrayLong identifiers; const auto langs = get_installed_languages();
get_installed_languages(names, identifiers); for (const wxLanguageInfo *info : langs)
for (size_t i = 0; i < identifiers.Count(); i++)
{ {
if (wxLocale::GetLanguageCanonicalName(identifiers[i]) == language) if (info->CanonicalName == language)
{ {
m_wxLocale = new wxLocale; m_wxLocale = new wxLocale;
m_wxLocale->Init(identifiers[i]); m_wxLocale->Init(info->Language);
m_wxLocale->AddCatalogLookupPathPrefix(from_u8(localization_dir())); m_wxLocale->AddCatalogLookupPathPrefix(from_u8(localization_dir()));
m_wxLocale->AddCatalog("Slic3rPE"); m_wxLocale->AddCatalog("Slic3rPE");
//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();
m_imgui->set_language(m_wxLocale->GetCanonicalName().ToUTF8().data()); m_imgui->set_language(into_u8(info->CanonicalName));
return true; return true;
} }
} }
@ -602,36 +615,31 @@ void GUI_App::save_language()
app_config->save(); app_config->save();
} }
// get list of installed languages // Get a list of installed languages
void GUI_App::get_installed_languages(wxArrayString & names, wxArrayLong & identifiers) std::vector<const wxLanguageInfo*> GUI_App::get_installed_languages()
{ {
names.Clear(); std::vector<const wxLanguageInfo*> res;
identifiers.Clear();
wxDir dir(from_u8(localization_dir())); wxDir dir(from_u8(localization_dir()));
wxString filename; wxString filename;
const wxLanguageInfo * langinfo; const wxLanguageInfo * langinfo;
wxString name = wxLocale::GetLanguageName(wxLANGUAGE_DEFAULT); wxString name = wxLocale::GetLanguageName(wxLANGUAGE_DEFAULT);
if (!name.IsEmpty()) if (!name.IsEmpty()) {
{ res.push_back(wxLocale::GetLanguageInfo(wxLANGUAGE_DEFAULT));
names.Add(_(L("Default")));
identifiers.Add(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); langinfo = wxLocale::FindLanguageInfo(filename);
if (langinfo != NULL) if (langinfo != NULL) {
{
auto full_file_name = dir.GetName() + wxFileName::GetPathSeparator() + auto full_file_name = dir.GetName() + wxFileName::GetPathSeparator() +
filename + wxFileName::GetPathSeparator() + "Slic3rPE" + wxT(".mo"); filename + wxFileName::GetPathSeparator() + "Slic3rPE" + wxT(".mo");
if (wxFileExists(full_file_name)) if (wxFileExists(full_file_name)) {
{ res.push_back(langinfo);
names.Add(langinfo->Description);
identifiers.Add(langinfo->Language);
} }
} }
} }
return res;
} }
Tab* GUI_App::get_tab(Preset::Type type) Tab* GUI_App::get_tab(Preset::Type type)

View file

@ -19,6 +19,7 @@ class wxMenuItem;
class wxMenuBar; class wxMenuBar;
class wxTopLevelWindow; class wxTopLevelWindow;
class wxNotebook; class wxNotebook;
class wxLanguageInfo;
namespace Slic3r { namespace Slic3r {
class AppConfig; class AppConfig;
@ -119,19 +120,16 @@ public:
void keyboard_shortcuts(); void keyboard_shortcuts();
void load_project(wxWindow *parent, wxString& input_file); void load_project(wxWindow *parent, wxString& input_file);
void import_model(wxWindow *parent, wxArrayString& input_files); void import_model(wxWindow *parent, wxArrayString& input_files);
static bool catch_error(std::function<void()> cb, static bool catch_error(std::function<void()> cb, const std::string& err);
// wxMessageDialog* message_dialog,
const std::string& err);
// void notify(/*message*/);
void persist_window_geometry(wxTopLevelWindow *window, bool default_maximized = false); void persist_window_geometry(wxTopLevelWindow *window, bool default_maximized = false);
void update_ui_from_settings(); void update_ui_from_settings();
bool switch_language(); bool switch_language();
bool select_language(wxArrayString & names, wxArrayLong & identifiers); bool select_language();
bool load_language(); bool load_language();
void save_language(); void save_language();
void get_installed_languages(wxArrayString & names, wxArrayLong & identifiers); std::vector<const wxLanguageInfo*> get_installed_languages();
Tab* get_tab(Preset::Type type); Tab* get_tab(Preset::Type type);
ConfigOptionMode get_mode(); ConfigOptionMode get_mode();