[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [48757] trunk/blender: UI translation from inside Blender UI: first part.

Bastien Montagne montagne29 at wanadoo.fr
Mon Jul 9 16:25:35 CEST 2012


Revision: 48757
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=48757
Author:   mont29
Date:     2012-07-09 14:25:35 +0000 (Mon, 09 Jul 2012)
Log Message:
-----------
UI translation from inside Blender UI: first part.

This commit reshapes a bit runtime button info getter, by adding a new uiButGetStrInfo() which accepts a variable number of uiStringInfo parameters, and tries to fill them with the requested strings, for the given button (label, tip, context, RNA identifier, keymap, etc.). Currently used mostly by existing ui_tooltip_create(), and new UI_OT_edittranslation_init operator.

It also adds a few getters (to get RNA i18n context, and current language iso code).

Finally, it adds to C operators needed for the py ui_translation addon:
*UI_OT_edittranslation_init, which gathers requested data and launch the py operator.
*UI_OT_reloadtranslation, which forces a full reload of the whole UI translation (including rechecking the directory containing mo files).

For the first operator to work, it also adds a new user preferences path: i18n_branches_directory, to point to the /branch part of a bf-translation checkout.

Modified Paths:
--------------
    trunk/blender/release/scripts/startup/bl_ui/space_userpref.py
    trunk/blender/source/blender/blenfont/BLF_translation.h
    trunk/blender/source/blender/blenfont/intern/blf_lang.c
    trunk/blender/source/blender/editors/include/UI_interface.h
    trunk/blender/source/blender/editors/interface/interface.c
    trunk/blender/source/blender/editors/interface/interface_handlers.c
    trunk/blender/source/blender/editors/interface/interface_ops.c
    trunk/blender/source/blender/editors/interface/interface_regions.c
    trunk/blender/source/blender/makesdna/DNA_userdef_types.h
    trunk/blender/source/blender/makesrna/RNA_access.h
    trunk/blender/source/blender/makesrna/intern/rna_access.c
    trunk/blender/source/blender/makesrna/intern/rna_userdef.c

Property Changed:
----------------
    trunk/blender/source/tools/

Modified: trunk/blender/release/scripts/startup/bl_ui/space_userpref.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_ui/space_userpref.py	2012-07-09 13:54:34 UTC (rev 48756)
+++ trunk/blender/release/scripts/startup/bl_ui/space_userpref.py	2012-07-09 14:25:35 UTC (rev 48757)
@@ -787,6 +787,7 @@
         sub.label(text="Scripts:")
         sub.label(text="Sounds:")
         sub.label(text="Temp:")
+        sub.label(text="I18n Branches:")
         sub.label(text="Image Editor:")
         sub.label(text="Animation Player:")
 
@@ -797,6 +798,7 @@
         sub.prop(paths, "script_directory", text="")
         sub.prop(paths, "sound_directory", text="")
         sub.prop(paths, "temporary_directory", text="")
+        sub.prop(paths, "i18n_branches_directory", text="")
         sub.prop(paths, "image_editor", text="")
         subsplit = sub.split(percentage=0.3)
         subsplit.prop(paths, "animation_player_preset", text="")

Modified: trunk/blender/source/blender/blenfont/BLF_translation.h
===================================================================
--- trunk/blender/source/blender/blenfont/BLF_translation.h	2012-07-09 13:54:34 UTC (rev 48756)
+++ trunk/blender/source/blender/blenfont/BLF_translation.h	2012-07-09 14:25:35 UTC (rev 48757)
@@ -54,6 +54,8 @@
 
 /* Set the current locale. */
 void BLF_lang_set(const char *);
+/* Get the current locale (short code, e.g. es_ES). */
+const char *BLF_lang_get(void);
 
 /* Set the current encoding name. */
 void BLF_lang_encoding(const char *str);

Modified: trunk/blender/source/blender/blenfont/intern/blf_lang.c
===================================================================
--- trunk/blender/source/blender/blenfont/intern/blf_lang.c	2012-07-09 13:54:34 UTC (rev 48756)
+++ trunk/blender/source/blender/blenfont/intern/blf_lang.c	2012-07-09 14:25:35 UTC (rev 48757)
@@ -112,7 +112,8 @@
 void BLF_lang_init(void)
 {
 	char *messagepath = BLI_get_folder(BLENDER_DATAFILES, "locale");
-	
+/*	printf("%s\n", messagepath);*/
+
 	BLI_strncpy(global_encoding_name, SYSTEM_ENCODING_DEFAULT, sizeof(global_encoding_name));
 	
 	if (messagepath) {
@@ -276,6 +277,11 @@
 	bind_textdomain_codeset(TEXT_DOMAIN_NAME, global_encoding_name);
 }
 
+const char *BLF_lang_get(void)
+{
+	return locales[2 * U.language + 1];
+}
+
 void BLF_lang_encoding(const char *str)
 {
 	BLI_strncpy(global_encoding_name, str, sizeof(global_encoding_name));
@@ -301,4 +307,9 @@
 	return;
 }
 
+const char *BLF_lang_get(void)
+{
+	return "";
+}
+
 #endif /* WITH_INTERNATIONAL */

Modified: trunk/blender/source/blender/editors/include/UI_interface.h
===================================================================
--- trunk/blender/source/blender/editors/include/UI_interface.h	2012-07-09 13:54:34 UTC (rev 48756)
+++ trunk/blender/source/blender/editors/include/UI_interface.h	2012-07-09 14:25:35 UTC (rev 48757)
@@ -515,6 +515,34 @@
 void uiButSetUnitType(uiBut *but, const int unit_type);
 int uiButGetUnitType(uiBut *but);
 
+enum {
+	BUT_GET_RNAPROP_IDENTIFIER = 1,
+	BUT_GET_RNASTRUCT_IDENTIFIER,
+	BUT_GET_RNAENUM_IDENTIFIER,
+	BUT_GET_LABEL,
+	BUT_GET_RNA_LABEL,
+	BUT_GET_RNAENUM_LABEL,
+	BUT_GET_RNA_LABEL_CONTEXT, /* Context specified in CTX_XXX_ macros are just unreachable! */
+	BUT_GET_TIP,
+	BUT_GET_RNA_TIP,
+	BUT_GET_RNAENUM_TIP,
+	BUT_GET_OP_KEYMAP,
+};
+
+typedef struct uiStringInfo {
+	int type;
+	char *strinfo;
+} uiStringInfo; 
+
+/* Note: Expects pointers to uiStringInfo structs as parameters.
+ *       Will fill them with translated strings, when possible.
+ *       Strings in uiStringInfo must be MEM_freeN'ed by caller. */
+void uiButGetStrInfo(struct bContext *C, uiBut *but, const int nbr, ...);
+
+/* Edit i18n stuff. */
+/* Name of the main py op from i18n addon. */
+#define EDTSRC_I18N_OP_NAME "UI_OT_edittranslation"
+
 /* Special Buttons
  *
  * Buttons with a more specific purpose:

Modified: trunk/blender/source/blender/editors/interface/interface.c
===================================================================
--- trunk/blender/source/blender/editors/interface/interface.c	2012-07-09 13:54:34 UTC (rev 48756)
+++ trunk/blender/source/blender/editors/interface/interface.c	2012-07-09 14:25:35 UTC (rev 48757)
@@ -3691,6 +3691,134 @@
 	wm_event_add(win, &event);
 }
 
+void uiButGetStrInfo(bContext *C, uiBut *but, int nbr, ...)
+{
+	va_list args;
+
+	EnumPropertyItem *items = NULL, *item = NULL;
+	int totitems, free_items = FALSE;
+
+	va_start(args, nbr);
+	while (nbr--) {
+		uiStringInfo *si = (uiStringInfo*) va_arg(args, void*);
+		int type = si->type;
+		char *tmp = NULL;
+
+		if (type == BUT_GET_LABEL) {
+			if (but->str) {
+				/* Menu labels can have some complex formating stuff marked by pipes, we don't want those here! */
+				char *tc = strchr(but->str, '|');
+				if (tc)
+					tmp = BLI_strdupn(but->str, tc - but->str);
+				else
+					tmp = BLI_strdup(but->str);
+			}
+			else
+				type = BUT_GET_RNA_LABEL; /* Fail-safe solution... */
+		}
+		else if (type == BUT_GET_TIP) {
+			if (but->tip && but->tip[0])
+				tmp = BLI_strdup(but->tip);
+			else
+				type = BUT_GET_RNA_TIP; /* Fail-safe solution... */
+		}
+		if (type == BUT_GET_RNAPROP_IDENTIFIER) {
+			if (but->rnaprop)
+				tmp = BLI_strdup(RNA_property_identifier(but->rnaprop));
+		}
+		else if (type == BUT_GET_RNASTRUCT_IDENTIFIER) {
+			if (but->rnaprop)
+				tmp = BLI_strdup(RNA_struct_identifier(but->rnapoin.type));
+			else if (but->optype)
+				tmp = BLI_strdup(but->optype->idname);
+			else if (ELEM(but->type, MENU, PULLDOWN)) {
+				MenuType *mt = uiButGetMenuType(but);
+				if (mt)
+					tmp = BLI_strdup(mt->idname);
+			}
+		}
+		else if (ELEM(type, BUT_GET_RNA_LABEL, BUT_GET_RNA_TIP)) {
+			if (but->rnaprop) {
+				if (type == BUT_GET_RNA_LABEL)
+					tmp = BLI_strdup(RNA_property_ui_name(but->rnaprop));
+				else {
+					const char *t = RNA_property_ui_description(but->rnaprop);
+					if (t && t[0])
+						tmp = BLI_strdup(t);
+				}
+			}
+			else if (but->optype) {
+				if (type == BUT_GET_RNA_LABEL)
+					tmp = BLI_strdup(RNA_struct_ui_name(but->optype->srna));
+				else {
+					const char *t = RNA_struct_ui_description(but->optype->srna);
+					if (t && t[0])
+						tmp = BLI_strdup(t);
+				}
+			}
+			else if (ELEM(but->type, MENU, PULLDOWN)) {
+				MenuType *mt = uiButGetMenuType(but);
+				if (mt) {
+					if (type == BUT_GET_RNA_LABEL)
+						tmp = BLI_strdup(RNA_struct_ui_name(mt->ext.srna));
+					else {
+						const char *t = RNA_struct_ui_description(mt->ext.srna);
+						if (t && t[0])
+							tmp = BLI_strdup(t);
+					}
+				}
+			}
+		}
+		else if (type == BUT_GET_RNA_LABEL_CONTEXT) {
+			if (but->rnaprop)
+				tmp = BLI_strdup(RNA_property_translation_context(but->rnaprop));
+			else if (but->optype)
+				tmp = BLI_strdup(RNA_struct_translation_context(but->optype->srna));
+			else if (ELEM(but->type, MENU, PULLDOWN)) {
+				MenuType *mt = uiButGetMenuType(but);
+				if (mt)
+					tmp = BLI_strdup(RNA_struct_translation_context(mt->ext.srna));
+			}
+		}
+		else if (ELEM3(type, BUT_GET_RNAENUM_IDENTIFIER, BUT_GET_RNAENUM_LABEL, BUT_GET_RNAENUM_TIP)) {
+			if (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_ENUM) {
+				if (!item) {
+					int i;
+					int value = (but->type == ROW) ? (int)but->hardmax : (int)ui_get_but_val(but);
+					RNA_property_enum_items_gettexted(C, &but->rnapoin, but->rnaprop, &items, &totitems, &free_items);
+
+					for (i = 0, item = items; i < totitems; i++, item++) {
+						if (item->identifier[0] && item->value == value)
+							break;
+					}
+				}
+				if (type == BUT_GET_RNAENUM_IDENTIFIER)
+					tmp = BLI_strdup(item->identifier);
+				else if (type == BUT_GET_RNAENUM_LABEL)
+					tmp = BLI_strdup(item->name);
+				else if (item->description && item->description[0])
+					tmp = BLI_strdup(item->description);
+			}
+		}
+		else if (type == BUT_GET_OP_KEYMAP) {
+			if (but->optype && !(but->block->flag & UI_BLOCK_LOOP)) {
+				/* operator keymap (not menus, they already have it) */
+				IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
+				char buf[512];
+
+				if (WM_key_event_operator_string(C, but->optype->idname, but->opcontext, prop, TRUE,
+				                                 buf, sizeof(buf)))
+					tmp = BLI_strdup(buf);
+			}
+		}
+
+		si->strinfo = tmp;
+	}
+
+	if (free_items && items)
+		MEM_freeN(items);
+}
+
 /* Program Init/Exit */
 
 void UI_init(void)

Modified: trunk/blender/source/blender/editors/interface/interface_handlers.c
===================================================================
--- trunk/blender/source/blender/editors/interface/interface_handlers.c	2012-07-09 13:54:34 UTC (rev 48756)
+++ trunk/blender/source/blender/editors/interface/interface_handlers.c	2012-07-09 14:25:35 UTC (rev 48757)
@@ -4399,23 +4399,32 @@
 	uiPopupMenu *pup;
 	uiLayout *layout;
 	int length;
-	const char *name;
+	char *name;
+	uiStringInfo label = {BUT_GET_LABEL, NULL};
 
-	if ((but->rnapoin.data && but->rnaprop) == 0 && but->optype == NULL)
-		return 0;
+/*	if ((but->rnapoin.data && but->rnaprop) == 0 && but->optype == NULL)*/
+/*		return 0;*/
 	
 	button_timers_tooltip_remove(C, but);
 
+#if 0
 	if (but->rnaprop)
 		name = RNA_property_ui_name(but->rnaprop);
 	else if (but->optype && but->optype->srna)
 		name = RNA_struct_ui_name(but->optype->srna);
 	else
 		name = IFACE_("<needs_name>");  // XXX - should never happen.
+#else
+	uiButGetStrInfo(C, but, 1, &label);
+	name = label.strinfo;
+#endif
 
 	pup = uiPupMenuBegin(C, name, ICON_NONE);
 	layout = uiPupMenuLayout(pup);
-	
+
+	if (label.strinfo)
+		MEM_freeN(label.strinfo);
+
 	uiLayoutSetOperatorContext(layout, WM_OP_INVOKE_DEFAULT);
 
 	if (but->rnapoin.data && but->rnaprop) {
@@ -4642,8 +4651,8 @@
 	}
 
 	/* perhaps we should move this into (G.debug & G_DEBUG) - campbell */
-	uiItemFullO(layout, "UI_OT_editsource", CTX_IFACE_(BLF_I18NCONTEXT_OPERATOR_DEFAULT, "Edit Source"),
-	            ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0);
+	uiItemFullO(layout, "UI_OT_editsource", NULL, ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0);
+	uiItemFullO(layout, "UI_OT_edittranslation_init", NULL, ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0);
 
 	uiPupMenuEnd(C, pup);
 

Modified: trunk/blender/source/blender/editors/interface/interface_ops.c
===================================================================
--- trunk/blender/source/blender/editors/interface/interface_ops.c	2012-07-09 13:54:34 UTC (rev 48756)

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list