add methods to set/get locales

This commit is contained in:
wmayer
2018-06-09 17:31:19 +02:00
parent cb13376746
commit 5ea211489a
2 changed files with 43 additions and 0 deletions

View File

@@ -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);

View File

@@ -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;