[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [53951] trunk/blender/source/blender: I18n fix: "" context is not the same as NULL context!

Bastien Montagne montagne29 at wanadoo.fr
Mon Jan 21 11:52:38 CET 2013


Revision: 53951
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=53951
Author:   mont29
Date:     2013-01-21 10:52:34 +0000 (Mon, 21 Jan 2013)
Log Message:
-----------
I18n fix: "" context is not the same as NULL context!

This bug did not appear earlier because the "" default context was actually never used, always NULL context was passed instead. But bpy.app.translations uses "" as default context in its keys (simplifies the hash/comp functions of internal py messages cache)...

So now, Blender prefers NULL (None in python) as default context value, but understands also "" as such.

Modified Paths:
--------------
    trunk/blender/source/blender/blenfont/BLF_translation.h
    trunk/blender/source/blender/blenfont/intern/blf_translation.c
    trunk/blender/source/blender/python/intern/bpy_app_translations.c

Modified: trunk/blender/source/blender/blenfont/BLF_translation.h
===================================================================
--- trunk/blender/source/blender/blenfont/BLF_translation.h	2013-01-21 10:48:18 UTC (rev 53950)
+++ trunk/blender/source/blender/blenfont/BLF_translation.h	2013-01-21 10:52:34 UTC (rev 53951)
@@ -50,7 +50,7 @@
 
 /* Set the current locale. */
 void BLF_lang_set(const char *);
-/* Get the current locale (short code, e.g. es_ES). */
+/* Get the current locale ([partial] ISO code, e.g. es_ES). */
 const char *BLF_lang_get(void);
 
 /* Get locale's elements (if relevant pointer is not NULL and element actually exists, e.g. if there is no variant,
@@ -110,8 +110,11 @@
  * things, and limit the number of existing contexts!
  */
 
-/* Default, void context. Just in case... */
-#define BLF_I18NCONTEXT_DEFAULT ""
+/* Default, void context.
+ * WARNING! The "" context is not the same as no (NULL) context at mo/boost::locale level!
+ */
+#define BLF_I18NCONTEXT_DEFAULT NULL  /* Translated as None in Python. */
+#define BLF_I18NCONTEXT_DEFAULT_BPY_INTERN ""  /* Only used in code, never exposed to user! */
 
 /* Default context for operator names/labels. */
 #define BLF_I18NCONTEXT_OPERATOR_DEFAULT "Operator"

Modified: trunk/blender/source/blender/blenfont/intern/blf_translation.c
===================================================================
--- trunk/blender/source/blender/blenfont/intern/blf_translation.c	2013-01-21 10:48:18 UTC (rev 53950)
+++ trunk/blender/source/blender/blenfont/intern/blf_translation.c	2013-01-21 10:52:34 UTC (rev 53951)
@@ -91,7 +91,14 @@
 {
 #ifdef WITH_INTERNATIONAL
 	if (msgid && msgid[0]) {
-		const char *ret = bl_locale_pgettext(msgctxt, msgid);
+		const char *ret;
+
+		/*if (msgctxt && !strcmp(msgctxt, BLF_I18NCONTEXT_DEFAULT_BPY_INTERN)) { */
+		if (msgctxt && !msgctxt[0]) {
+			/* BLF_I18NCONTEXT_DEFAULT_BPY_INTERN context is reserved and considered the same as default NULL one. */
+			msgctxt = NULL;
+		}
+		ret = bl_locale_pgettext(msgctxt, msgid);
 		/* We assume if the returned string is the same (memory level) as the msgid, no translation was found,
 		 * and we can try py scripts' ones!
 		 */

Modified: trunk/blender/source/blender/python/intern/bpy_app_translations.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_app_translations.c	2013-01-21 10:48:18 UTC (rev 53950)
+++ trunk/blender/source/blender/python/intern/bpy_app_translations.c	2013-01-21 10:52:34 UTC (rev 53951)
@@ -76,7 +76,7 @@
 static GHashKey *_ghashutil_keyalloc(const void *msgctxt, const void *msgid)
 {
 	GHashKey *key = MEM_mallocN(sizeof(GHashKey), "GHashPair");
-	key->msgctxt = BLI_strdup(msgctxt);
+	key->msgctxt = BLI_strdup(msgctxt ? msgctxt : BLF_I18NCONTEXT_DEFAULT_BPY_INTERN);
 	key->msgid = BLI_strdup(msgid);
 	return key;
 }
@@ -186,7 +186,10 @@
 				}
 
 				tmp = PyTuple_GET_ITEM(pykey, 0);
-				if (PyUnicode_Check(tmp)) {
+				if (tmp == Py_None) {
+					msgctxt = BLF_I18NCONTEXT_DEFAULT;
+				}
+				else if (PyUnicode_Check(tmp)) {
 					msgctxt = _PyUnicode_AsString(tmp);
 				}
 				tmp = PyTuple_GET_ITEM(pykey, 1);
@@ -194,7 +197,7 @@
 					msgid = _PyUnicode_AsString(tmp);
 				}
 
-				if (!(msgctxt && msgid)) {
+				if (!msgid) {
 					continue;
 				}
 
@@ -247,7 +250,7 @@
 	}
 
 	/* And now, simply create the key (context, messageid) and find it in the cached dict! */
-	key = _ghashutil_keyalloc(msgctxt ? msgctxt : BLF_I18NCONTEXT_DEFAULT, msgid);
+	key = _ghashutil_keyalloc(msgctxt, msgid);
 
 	tmp = BLI_ghash_lookup(_translations_cache, key);
 
@@ -359,12 +362,19 @@
 	}
 
 #define SetObjString(item) PyStructSequence_SET_ITEM(translations_contexts, pos++, PyUnicode_FromString((item)))
+#define SetObjNone() Py_INCREF(Py_None); PyStructSequence_SET_ITEM(translations_contexts, pos++, Py_None)
 
 	for (ctxt = _contexts; ctxt->c_id; ctxt++) {
-		SetObjString(ctxt->value);
+		if (ctxt->value) {
+			SetObjString(ctxt->value);
+		}
+		else {
+			SetObjNone();
+		}
 	}
 
 #undef SetObjString
+#undef SetObjNone
 
 	return translations_contexts;
 }
@@ -372,7 +382,9 @@
 /***** Main BlenderAppTranslations Py object definition *****/
 
 PyDoc_STRVAR(app_translations_contexts_doc,
-	"A named tuple containing all pre-defined translation contexts."
+	"A named tuple containing all pre-defined translation contexts.\n"
+	"WARNING: do not use the \"" BLF_I18NCONTEXT_DEFAULT_BPY_INTERN "\" context, it is internally assimilated as the "
+	"default one!\n"
 );
 
 PyDoc_STRVAR(app_translations_contexts_C_to_py_doc,
@@ -433,14 +445,14 @@
 "\n"
 "   Try to translate the given msgid (with optional msgctxt).\n"
 "   NOTE: The (msgid, msgctxt) parameter orders has been switched compared to gettext function, to allow\n"
-"         single-parameter calls (context then defaults to BLF_I18NCONTEXT_DEFAULT)."
+"         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."
+"         handled by Blender internal code.\n"
 "\n"
 "   :arg msgid: The string to translate.\n"
 "   :type msgid: string\n"
 "   :arg msgctxt: The translation context.\n"
-"   :type msgctxt: string\n"
+"   :type msgctxt: string or None\n"
 "   :default msgctxt: BLF_I18NCONTEXT_DEFAULT value.\n"
 "   :return: The translated string (or msgid if no translation was found).\n"
 "\n"
@@ -450,7 +462,7 @@
 	static const char *kwlist[] = {"msgid", "msgctxt", NULL};
 	char *msgid, *msgctxt = NULL;
 
-	if (!PyArg_ParseTupleAndKeywords(args, kw, "s|s:bpy.app.translations.pgettext()", (char **)kwlist,
+	if (!PyArg_ParseTupleAndKeywords(args, kw, "s|z:bpy.app.translations.pgettext", (char **)kwlist,
 	                                 &msgid, &msgctxt))
 	{
 		return NULL;
@@ -466,7 +478,7 @@
 	{(char *)"unregister", (PyCFunction)app_translations_py_messages_unregister, METH_VARARGS | METH_KEYWORDS,
 	                       app_translations_py_messages_unregister_doc},
 	{(char *)"pgettext", (PyCFunction)app_translations_pgettext, METH_VARARGS | METH_KEYWORDS | METH_STATIC,
-	                      app_translations_pgettext_doc},
+	                     app_translations_pgettext_doc},
 	{NULL}
 };
 




More information about the Bf-blender-cvs mailing list