Skip to content

Commit

Permalink
add methods to set/get locales
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jun 9, 2018
1 parent cb13376 commit 5ea2114
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Gui/Application.h
Expand Up @@ -214,6 +214,8 @@ class GuiExport Application
PYFUNCDEF_S(sUpdateGui);
PYFUNCDEF_S(sUpdateLocale);
PYFUNCDEF_S(sGetLocale);
PYFUNCDEF_S(sSetLocale);
PYFUNCDEF_S(sSupportedLocales);
PYFUNCDEF_S(sCreateDialog);
PYFUNCDEF_S(sAddPreferencePage);

Expand Down
41 changes: 41 additions & 0 deletions src/Gui/ApplicationPy.cpp
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5ea2114

Please sign in to comment.