From 4c0ebedc66c849e2b019f3932bd2208dd9667428 Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Mon, 21 Jan 2013 15:10:22 +0000 Subject: On second thought, exposes bpy.app.translations also when built without i18n support, this will avoid the need for py scripts to test for its presence everywhere! --- source/blender/python/intern/bpy_app.c | 8 +-- .../blender/python/intern/bpy_app_translations.c | 70 +++++++++++++--------- 2 files changed, 43 insertions(+), 35 deletions(-) (limited to 'source/blender') diff --git a/source/blender/python/intern/bpy_app.c b/source/blender/python/intern/bpy_app.c index 9ffee3ec1f5..5195f821d56 100644 --- a/source/blender/python/intern/bpy_app.c +++ b/source/blender/python/intern/bpy_app.c @@ -36,9 +36,7 @@ #include "bpy_app_ffmpeg.h" #include "bpy_app_build_options.h" -#ifdef WITH_INTERNATIONAL -# include "bpy_app_translations.h" -#endif +#include "bpy_app_translations.h" #include "bpy_app_handlers.h" #include "bpy_driver.h" @@ -90,9 +88,7 @@ static PyStructSequence_Field app_info_fields[] = { {(char *)"ffmpeg", (char *)"FFmpeg library information backend"}, {(char *)"build_options", (char *)"A set containing most important enabled optional build features"}, {(char *)"handlers", (char *)"Application handler callbacks"}, -#ifdef WITH_INTERNATIONAL {(char *)"translations", (char *)"Application and addons internationalization API"}, -#endif {NULL}, }; @@ -159,9 +155,7 @@ static PyObject *make_app_info(void) SetObjItem(BPY_app_ffmpeg_struct()); SetObjItem(BPY_app_build_options_struct()); SetObjItem(BPY_app_handlers_struct()); -#ifdef WITH_INTERNATIONAL SetObjItem(BPY_app_translations_struct()); -#endif #undef SetIntItem #undef SetStrItem diff --git a/source/blender/python/intern/bpy_app_translations.c b/source/blender/python/intern/bpy_app_translations.c index 83772682646..ea5a27976bf 100644 --- a/source/blender/python/intern/bpy_app_translations.c +++ b/source/blender/python/intern/bpy_app_translations.c @@ -32,19 +32,15 @@ /* XXX Why bloody hell isn't that included in Python.h???? */ #include -/* Need this one before BPY_extern.h, for bool def! */ +#include "BLI_string.h" +#include "BLI_ghash.h" #include "BLI_utildefines.h" #include "BPY_extern.h" #include "bpy_app_translations.h" -#ifdef WITH_INTERNATIONAL - #include "MEM_guardedalloc.h" -#include "BLI_string.h" -#include "BLI_ghash.h" - #include "BLF_translation.h" #include "RNA_types.h" @@ -67,6 +63,8 @@ typedef struct /* Our singleton instance pointer */ static BlenderAppTranslations *_translations = NULL; +#ifdef WITH_INTERNATIONAL + /***** Helpers for ghash *****/ typedef struct GHashKey { const char *msgctxt; @@ -295,11 +293,15 @@ const char *BPY_app_translations_py_pgettext(const char *msgctxt, const char *ms #undef STATIC_LOCALE_SIZE } +#endif /* WITH_INTERNATIONAL */ + PyDoc_STRVAR(app_translations_py_messages_register_doc, ".. method:: register(module_name, translations_dict)\n" "\n" " Registers an addon's UI translations.\n" "\n" +" Note: Does nothing when Blender is built without internationalization support.\n" +"\n" " :arg module_name: The name identifying the addon.\n" " :type module_name: string\n" " :arg translations_dict: A dictionary built like that:\n" @@ -309,6 +311,7 @@ PyDoc_STRVAR(app_translations_py_messages_register_doc, ); static PyObject *app_translations_py_messages_register(BlenderAppTranslations *self, PyObject *args, PyObject *kw) { +#ifdef WITH_INTERNATIONAL static const char *kwlist[] = {"module_name", "translations_dict", NULL}; PyObject *module_name, *uuid_dict; @@ -329,6 +332,11 @@ static PyObject *app_translations_py_messages_register(BlenderAppTranslations *s /* Clear cached messages dict! */ _clear_translations_cache(); +#else + (void)self; + (void)args; + (void)kw; +#endif /* And we are done! */ Py_RETURN_NONE; @@ -339,12 +347,15 @@ PyDoc_STRVAR(app_translations_py_messages_unregister_doc, "\n" " Unregisters an addon's UI translations.\n" "\n" +" Note: Does nothing when Blender is built without internationalization support.\n" +"\n" " :arg module_name: The name identifying the addon.\n" " :type module_name: string\n" "\n" ); static PyObject *app_translations_py_messages_unregister(BlenderAppTranslations *self, PyObject *args, PyObject *kw) { +#ifdef WITH_INTERNATIONAL static const char *kwlist[] = {"module_name", NULL}; PyObject *module_name; @@ -359,13 +370,18 @@ static PyObject *app_translations_py_messages_unregister(BlenderAppTranslations /* Clear cached messages ghash! */ _clear_translations_cache(); } +#else + (void)self; + (void)args; + (void)kw; +#endif /* And we are done! */ Py_RETURN_NONE; } - /***** C-defined contexts *****/ +/* This is always available (even when WITH_INTERNATIONAL is not defined). */ static PyTypeObject BlenderAppTranslationsContextsType; @@ -433,7 +449,10 @@ PyMemberDef app_translations_members[] = { {NULL} }; -PyDoc_STRVAR(app_translations_locale_doc, "The actual locale currently in use."); +PyDoc_STRVAR(app_translations_locale_doc, + "The actual locale currently in use (will always return a void string when Blender is built without " + "internationalization support)." +); static PyObject *app_translations_locale_get(PyObject *UNUSED(self), void *UNUSED(userdata)) { return PyUnicode_FromString(BLF_lang_get()); @@ -447,23 +466,23 @@ static PyObject *app_translations_locales_get(PyObject *UNUSED(self), void *UNUS EnumPropertyItem *it, *items = BLF_RNA_lang_enum_properties(); int num_locales = 0, pos = 0; - /* This is not elegant, but simple! */ - for (it = items; it->identifier; it++) { - if (it->value) - num_locales++; + if (items) { + /* This is not elegant, but simple! */ + for (it = items; it->identifier; it++) { + if (it->value) + num_locales++; + } } ret = PyTuple_New(num_locales); -#define TupleSetItem() PyTuple_SET_ITEM(ret, pos++, PyUnicode_FromString(it->description)) - - for (it = items; it->identifier; it++) { - if (it->value) - TupleSetItem(); + if (items) { + for (it = items; it->identifier; it++) { + if (it->value) + PyTuple_SET_ITEM(ret, pos++, PyUnicode_FromString(it->description)); + } } -#undef TupleSetItem - return ret; } @@ -482,6 +501,7 @@ PyDoc_STRVAR(app_translations_pgettext_doc, " single-parameter calls (context then defaults to BLF_I18NCONTEXT_DEFAULT).\n" " NOTE: You should really rarely need to use this function in regular addon code, as all translation should be\n" " handled by Blender internal code.\n" +" Note: Does nothing when Blender is built without internationalization support (hence always returns msgid).\n" "\n" " :arg msgid: The string to translate.\n" " :type msgid: string\n" @@ -493,6 +513,9 @@ PyDoc_STRVAR(app_translations_pgettext_doc, ); static PyObject *app_translations_pgettext(BlenderAppTranslations *UNUSED(self), PyObject *args, PyObject *kw) { + /* Note we could optimize this a bit when WITH_INTERNATIONAL is not defined, but don't think "code complexity" would + * be worth it, as this func should not often be used! + */ static const char *kwlist[] = {"msgid", "msgctxt", NULL}; char *msgid, *msgctxt = NULL; @@ -712,12 +735,3 @@ PyObject *BPY_app_translations_struct(void) return ret; } - -#else /* WITH_INTERNATIONAL */ - -PyObject *BPY_app_translations_struct(void) -{ - Py_RETURN_NONE; -} - -#endif /* WITH_INTERNATIONAL */ -- cgit v1.2.3