From 5ea211489a6943fd33e2e15ea3e7ec4d2662beff Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 9 Jun 2018 17:31:19 +0200 Subject: [PATCH] add methods to set/get locales --- src/Gui/Application.h | 2 ++ src/Gui/ApplicationPy.cpp | 41 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/Gui/Application.h b/src/Gui/Application.h index 6feef81112..eb1113dcf6 100644 --- a/src/Gui/Application.h +++ b/src/Gui/Application.h @@ -214,6 +214,8 @@ public: PYFUNCDEF_S(sUpdateGui); PYFUNCDEF_S(sUpdateLocale); PYFUNCDEF_S(sGetLocale); + PYFUNCDEF_S(sSetLocale); + PYFUNCDEF_S(sSupportedLocales); PYFUNCDEF_S(sCreateDialog); PYFUNCDEF_S(sAddPreferencePage); diff --git a/src/Gui/ApplicationPy.cpp b/src/Gui/ApplicationPy.cpp index 6c0eacf872..b66a3f8750 100644 --- a/src/Gui/ApplicationPy.cpp +++ b/src/Gui/ApplicationPy.cpp @@ -111,6 +111,13 @@ PyMethodDef Application::Methods[] = { {"getLocale", (PyCFunction) Application::sGetLocale, 1, "getLocale() -> string\n\n" "Returns the locale currently used by FreeCAD"}, + {"setLocale", (PyCFunction) Application::sSetLocale, 1, + "getLocale(string) -> None\n\n" + "Sets the locale used by FreeCAD. You can set it by\n" + "top-level domain (e.g. \"de\") or the language name (e.g. \"German\")"}, + {"supportedLocales", (PyCFunction) Application::sSupportedLocales, 1, + "supportedLocales() -> dict\n\n" + "Returns a dict of all supported languages/top-level domains"}, {"createDialog", (PyCFunction) Application::sCreateDialog, 1, "createDialog(string) -- Open a UI file"}, {"addPreferencePage", (PyCFunction) Application::sAddPreferencePage,1, @@ -624,6 +631,40 @@ PyObject* Application::sGetLocale(PyObject * /*self*/, PyObject *args,PyObject * #endif } +PyObject* Application::sSetLocale(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/) +{ + char* name; + if (!PyArg_ParseTuple(args, "s", &name)) + return NULL; + + std::string cname(name); + TStringMap map = Translator::instance()->supportedLocales(); + for (const auto& it : map) { + if (it.first == cname || it.second == cname) { + Translator::instance()->activateLanguage(it.first.c_str()); + break; + } + } + + Py_INCREF(Py_None); + return Py_None; +} + +PyObject* Application::sSupportedLocales(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/) +{ + if (!PyArg_ParseTuple(args, "")) + return NULL; + + TStringMap map = Translator::instance()->supportedLocales(); + Py::Dict dict; + for (const auto& it : map) { + Py::String key(it.first); + Py::String val(it.second); + dict.setItem(key, val); + } + return Py::new_reference_to(dict); +} + PyObject* Application::sCreateDialog(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/) { char* fn = 0;