Save language preset.

* Added global variable g_local_dir to get "localization" directory.
* Chage/Set language works correctly now.
* Probably, fixed work on Linux
This commit is contained in:
YuSanka 2018-02-12 08:57:32 +01:00
parent d18a200b0f
commit badeb2f64c
7 changed files with 60 additions and 38 deletions

View File

@ -43,6 +43,7 @@ use FindBin;
# Let the XS module know where the GUI resources reside.
set_resources_dir(decode_path($FindBin::Bin) . (($^O eq 'darwin') ? '/../Resources' : '/resources'));
set_var_dir(resources_dir() . "/icons");
set_local_dir(resources_dir() . "/localization/");
use Moo 1.003001;

View File

@ -150,7 +150,6 @@ sub OnInit {
sub recreate_GUI{
my ($self) = @_;
print "Inside recreate_GUI \n";
my $topwindow = $self->GetTopWindow();
$self->{mainframe} = my $frame = Slic3r::GUI::MainFrame->new(
# If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.

View File

@ -19,8 +19,11 @@ std::string var(const std::string &file_name);
void set_resources_dir(const std::string &path);
// Return a full path to the resources directory.
const std::string& resources_dir();
// Set a path with GUI localization files.
void set_local_dir(const std::string &path);
// Return a full path to the localization directory.
std::string localization_dir();
const std::string& localization_dir();
// Set a path with preset files.
void set_data_dir(const std::string &path);

View File

@ -103,9 +103,16 @@ const std::string& resources_dir()
return g_resources_dir;
}
std::string localization_dir()
static std::string g_local_dir;
void set_local_dir(const std::string &dir)
{
return resources_dir() + "\\localization\\";
g_local_dir = dir;
}
const std::string& localization_dir()
{
return g_local_dir;
}
static std::string g_data_dir;

View File

@ -35,7 +35,7 @@
#include "Tab.hpp"
#include "TabIface.hpp"
#include "AppConfig.hpp"
//#include <wx/config.h>
#include <wx/config.h>
#include <wx/dir.h>
#include <wx/filename.h>
#include "Utils.hpp"
@ -168,8 +168,7 @@ wxNotebook *g_wxTabPanel = nullptr;
std::vector<Tab *> g_tabs_list;
wxLocale* m_Locale;
std::string m_local_dir;
wxLocale* g_wxLocale;
void set_wxapp(wxApp *app)
{
@ -211,17 +210,27 @@ bool select_language(wxArrayString & names,
{
wxCHECK_MSG(names.Count() == identifiers.Count(), false,
_L("Array of language names and identifiers should have the same size."));
long index = wxGetSingleChoiceIndex(_L("Select the language"),
_L("Language"), names);
int init_selection = 0;
long current_language = g_wxLocale ? g_wxLocale->GetLanguage() : wxLANGUAGE_UNKNOWN;
for (auto lang : identifiers){
if (lang == current_language)
break;
else
++init_selection;
}
if (init_selection == identifiers.size())
init_selection = 0;
long index = wxGetSingleChoiceIndex(_L("Select the language"), _L("Language"),
names, init_selection);
if (index != -1)
{
m_Locale = new wxLocale;
m_Locale->Init(identifiers[index]);
m_Locale->AddCatalogLookupPathPrefix(wxPathOnly(m_local_dir));
g_wxLocale = new wxLocale;
g_wxLocale->Init(identifiers[index]);
g_wxLocale->AddCatalogLookupPathPrefix(wxPathOnly(localization_dir()));
wxLogTrace(wxTraceMask(),
_L("Slic3rPE: Path Prefix = \"%s\""),
wxPathOnly(m_local_dir).GetData());
m_Locale->AddCatalog(g_wxApp->GetAppName());
wxPathOnly(localization_dir()).GetData());
g_wxLocale->AddCatalog(g_wxApp->GetAppName());
wxLogTrace(wxTraceMask(),
_L("Slic3rPE: Catalog Name = \"%s\""),
g_wxApp->GetAppName().GetData());
@ -232,44 +241,41 @@ bool select_language(wxArrayString & names,
bool load_language()
{
// wxConfig config(g_wxApp->GetAppName());
wxConfig config(g_wxApp->GetAppName());
long language;
// if (!config.Read(wxT("wxTranslation_Language"),
// &language, wxLANGUAGE_UNKNOWN))
if (!config.Read(wxT("wxTranslation_Language"),
&language, wxLANGUAGE_UNKNOWN))
{
language = wxLANGUAGE_ENGLISH_US;// wxLANGUAGE_UKRAINIAN;// wxLANGUAGE_UNKNOWN;
language = wxLANGUAGE_UNKNOWN;
}
if (language == wxLANGUAGE_UNKNOWN) return false;
wxArrayString names;
wxArrayLong identifiers;
if (language == wxLANGUAGE_UNKNOWN)
return false;
wxArrayString names;
wxArrayLong identifiers;
get_installed_languages(names, identifiers);
for (size_t i = 0; i < identifiers.Count(); i++)
{
if (identifiers[i] == language)
{
m_Locale = new wxLocale;
m_Locale->Init(identifiers[i]);
m_Locale->AddCatalogLookupPathPrefix(wxPathOnly(m_local_dir));
m_Locale->AddCatalog(g_wxApp->GetAppName());
g_wxLocale = new wxLocale;
g_wxLocale->Init(identifiers[i]);
g_wxLocale->AddCatalogLookupPathPrefix(wxPathOnly(localization_dir()));
g_wxLocale->AddCatalog(g_wxApp->GetAppName());
return true;
}
}
return false;
}
void save_language(bool bReset)
void save_language()
{
// wxConfig config(g_wxApp->GetAppName());
wxConfig config(g_wxApp->GetAppName());
long language = wxLANGUAGE_UNKNOWN;
if (!bReset)
{
if (m_Locale)
{
language = m_Locale->GetLanguage();
}
if (g_wxLocale) {
language = g_wxLocale->GetLanguage();
}
// config.Write(wxT("wxTranslation_Language"), language);
// config.Flush();
config.Write(wxT("wxTranslation_Language"), language);
config.Flush();
}
void get_installed_languages(wxArrayString & names,
@ -277,9 +283,8 @@ void get_installed_languages(wxArrayString & names,
{
names.Clear();
identifiers.Clear();
m_local_dir = localization_dir();
wxDir dir(wxPathOnly(m_local_dir));
wxDir dir(wxPathOnly(localization_dir()));
wxString filename;
const wxLanguageInfo * langinfo;
wxString name = wxLocale::GetLanguageName(wxLANGUAGE_DEFAULT);
@ -319,6 +324,7 @@ void add_debug_menu(wxMenuBar *menu, int event_language_change)
wxArrayLong identifiers;
get_installed_languages(names, identifiers);
if (select_language(names, identifiers)){
save_language();
show_info(g_wxTabPanel, "Application will be restarted", "Attention!");
if (event_language_change > 0) {
wxCommandEvent event(event_language_change);

View File

@ -77,7 +77,7 @@ void show_info(wxWindow* parent, wxString message, wxString title);
// load language saved at application config
bool load_language();
// save language at application config
void save_language(bool bReset);
void save_language();
// get list of installed languages
void get_installed_languages(wxArrayString & names, wxArrayLong & identifiers);
// select language from the list of installed languages

View File

@ -54,6 +54,12 @@ set_var_dir(dir)
CODE:
Slic3r::set_var_dir(dir);
void
set_local_dir(dir)
char *dir;
CODE:
Slic3r::set_local_dir(dir);
char*
var_dir()
CODE: