[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [53962] trunk/blender/source/blender/ python/intern: 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!

Bastien Montagne montagne29 at wanadoo.fr
Mon Jan 21 16:10:22 CET 2013


Revision: 53962
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=53962
Author:   mont29
Date:     2013-01-21 15:10:22 +0000 (Mon, 21 Jan 2013)
Log Message:
-----------
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!

Modified Paths:
--------------
    trunk/blender/source/blender/python/intern/bpy_app.c
    trunk/blender/source/blender/python/intern/bpy_app_translations.c

Modified: trunk/blender/source/blender/python/intern/bpy_app.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_app.c	2013-01-21 14:15:57 UTC (rev 53961)
+++ trunk/blender/source/blender/python/intern/bpy_app.c	2013-01-21 15:10:22 UTC (rev 53962)
@@ -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 @@
 	{(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 @@
 	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

Modified: trunk/blender/source/blender/python/intern/bpy_app_translations.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_app_translations.c	2013-01-21 14:15:57 UTC (rev 53961)
+++ trunk/blender/source/blender/python/intern/bpy_app_translations.c	2013-01-21 15:10:22 UTC (rev 53962)
@@ -32,19 +32,15 @@
 /* XXX Why bloody hell isn't that included in Python.h???? */
 #include <structmember.h>
 
-/* 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 @@
 /* Our singleton instance pointer */
 static BlenderAppTranslations *_translations = NULL;
 
+#ifdef WITH_INTERNATIONAL
+
 /***** Helpers for ghash *****/
 typedef struct GHashKey {
 	const char *msgctxt;
@@ -295,11 +293,15 @@
 #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 @@
 );
 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 @@
 
 	/* 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 @@
 "\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 @@
 		/* 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 @@
 	{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 @@
 	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 @@
 "         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 @@
 );
 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 @@
 
 	return ret;
 }
-
-#else  /* WITH_INTERNATIONAL */
-
-PyObject *BPY_app_translations_struct(void)
-{
-	Py_RETURN_NONE;
-}
-
-#endif  /* WITH_INTERNATIONAL */




More information about the Bf-blender-cvs mailing list