Set default locale on saved choice or environment

Guess the default locale from the envinroment (GetLocaleInfo() on Windows and
the setlocale() on Linux/Unix) or from the saved settings for mmg.
This commit is contained in:
Moritz Bunkus 2009-02-10 21:26:09 +01:00
parent a286a51927
commit d82ded5d62
4 changed files with 77 additions and 21 deletions

View File

@ -47,8 +47,10 @@ translation_c::translation_c(const std::string &unix_locale,
void
translation_c::initialize_available_translations() {
ms_available_translations.clear();
#if defined(HAVE_LIBINTL_H)
ms_available_translations.push_back(translation_c("en_US", "english", "English", "English"));
ms_available_translations.push_back(translation_c("de_DE", "german", "German", "Deutsch"));
#endif
}
int
@ -67,6 +69,47 @@ translation_c::look_up_translation(const std::string &locale) {
return -1;
}
std::string
translation_c::get_default_ui_locale() {
std::string locale;
#if defined(HAVE_LIBINTL_H)
# if defined(SYS_WINDOWS)
char *data;
int len = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SENGLANGUAGE, NULL, 0);
if (0 < len) {
data = (char *)safemalloc(len);
memset(data, 0, len);
if (0 != GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SENGLANGUAGE, data, len))
locale = data;
safefree(data);
}
# else // SYS_WINDOWS
char *data = setlocale(LC_MESSAGES, NULL);
if (NULL != data) {
std::string previous_locale = data;
setlocale(LC_MESSAGES, "");
data = setlocale(LC_MESSAGES, NULL);
if (NULL != data) {
std::vector<std::string> parts = split(data, ".");
locale = parts[0];
}
setlocale(LC_MESSAGES, previous_locale.c_str());
}
# endif // SYS_WINDOWS
#endif // HAVE_LIBINTL_H
if (!locale.empty() && (-1 == look_up_translation(locale)))
locale = "";
return locale;
}
std::string
translation_c::get_locale() {
#if defined(SYS_WINDOWS)
@ -84,25 +127,13 @@ init_locales(std::string locale) {
string locale_dir;
if (!locale.empty()) {
int idx = translation_c::look_up_translation(locale);
if (-1 == idx)
locale = "";
}
if (-1 == translation_c::look_up_translation(locale))
locale = "";
if (locale.empty())
locale = translation_c::get_default_ui_locale();
# if defined(SYS_WINDOWS)
if (locale.empty()) {
char *data;
int len = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SENGLANGUAGE, NULL, 0);
if (0 < len) {
data = (char *)safemalloc(len);
memset(data, 0, len);
if (0 != GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SENGLANGUAGE, data, len))
locale = data;
safefree(data);
}
}
if (!locale.empty()) {
// The Windows system headers define LC_MESSAGES but
// Windows itself doesn't know it and treats "set_locale(LC_MESSAGES, ...)"

View File

@ -35,6 +35,7 @@ public:
static void initialize_available_translations();
static int look_up_translation(const std::string &locale);
static std::string get_default_ui_locale();
};
void MTX_DLL_API init_locales(std::string locale = "");

View File

@ -2160,6 +2160,25 @@ BEGIN_EVENT_TABLE(mmg_dialog, wxFrame)
EVT_CLOSE(mmg_dialog::on_close)
END_EVENT_TABLE();
void
mmg_app::init_ui_locale(wxConfigBase *cfg) {
wxString w_locale;
std::string locale;
translation_c::initialize_available_translations();
cfg->SetPath(wxT("/GUI"));
if (cfg->Read(wxT("ui_locale"), &w_locale)) {
locale = wxMB(w_locale);
if (-1 == translation_c::look_up_translation(locale))
locale = "";
}
m_ui_locale = locale;
init_locales(locale);
}
bool
mmg_app::OnInit() {
wxConfigBase *cfg;
@ -2167,14 +2186,15 @@ mmg_app::OnInit() {
wxString k, v;
int index;
cfg = new wxConfig(wxT("mkvmergeGUI"));
wxConfigBase::Set(cfg);
init_stdio();
init_locales();
init_ui_locale(cfg);
mm_file_io_c::setup();
cc_local_utf8 = utf8_init("");
xml_element_map_init();
cfg = new wxConfig(wxT("mkvmergeGUI"));
wxConfigBase::Set(cfg);
cfg->SetPath(wxT("/GUI"));
cfg->Read(wxT("last_directory"), &last_open_dir, wxEmptyString);
for (i = 0; i < 4; i++) {

View File

@ -19,9 +19,10 @@
#include "os.h"
#include "wx/string.h"
#include "wx/app.h"
#include "wx/combobox.h"
#include "wx/config.h"
#include "wx/string.h"
#include "ebml/EbmlUnicodeString.h"
@ -179,9 +180,12 @@ wxString format_tooltip(const wxString &s);
#endif
class mmg_app: public wxApp {
public:
std::string m_ui_locale;
public:
virtual bool OnInit();
virtual int OnExit();
virtual void init_ui_locale(wxConfigBase *cfg);
};
extern mmg_app *app;