[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [4481] trunk/py/scripts/addons/ ui_translate: UI translation: now addons' authors have all needed tools to manage their own translations.

Bastien Montagne montagne29 at wanadoo.fr
Thu Apr 18 17:39:28 CEST 2013


Revision: 4481
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=4481
Author:   mont29
Date:     2013-04-18 15:39:28 +0000 (Thu, 18 Apr 2013)
Log Message:
-----------
UI translation: now addons' authors have all needed tools to manage their own translations.
* Update: update (or create) an addon's translation data (as some py code).
* Export PO: export an addon's translation data in a set of pot/po's files.
* Import PO: import a set of po's files to update an addon's translations.

Note: current i18n doc on wiki is completely outdated, I will try to fix this in following days.

Modified Paths:
--------------
    trunk/py/scripts/addons/ui_translate/__init__.py
    trunk/py/scripts/addons/ui_translate/update_addon.py
    trunk/py/scripts/addons/ui_translate/update_ui.py

Modified: trunk/py/scripts/addons/ui_translate/__init__.py
===================================================================
--- trunk/py/scripts/addons/ui_translate/__init__.py	2013-04-17 10:01:07 UTC (rev 4480)
+++ trunk/py/scripts/addons/ui_translate/__init__.py	2013-04-18 15:39:28 UTC (rev 4481)
@@ -21,7 +21,7 @@
 bl_info = {
     "name": "Manage UI translations",
     "author": "Bastien Montagne",
-    "version": (1, 0, 1),
+    "version": (1, 1, 0),
     "blender": (2, 66, 5),
     "location": "Main \"File\" menu, text editor, any UI control",
     "description": "Allow to manage UI translations directly from Blender (update main po files, "

Modified: trunk/py/scripts/addons/ui_translate/update_addon.py
===================================================================
--- trunk/py/scripts/addons/ui_translate/update_addon.py	2013-04-17 10:01:07 UTC (rev 4480)
+++ trunk/py/scripts/addons/ui_translate/update_addon.py	2013-04-18 15:39:28 UTC (rev 4481)
@@ -215,8 +215,83 @@
         return {'FINISHED'}
 
 
+class UI_OT_i18n_addon_translation_import(bpy.types.Operator):
+    """Import given addon's translation data from PO files"""
+    bl_idname = "ui.i18n_addon_translation_import"
+    bl_label = "I18n Addon Import"
+
+    module_name = EnumProperty(items=enum_addons, name="Addon", description="Addon to process", options=set())
+    directory = StringProperty(maxlen=1024, subtype='FILE_PATH', options={'HIDDEN', 'SKIP_SAVE'})
+
+    def _dst(self, trans, path, uid, kind):
+        if kind == 'PO':
+            if uid == self.settings.PARSER_TEMPLATE_ID:
+                return os.path.join(self.directory, "blender.pot")
+            path = os.path.join(self.directory, uid)
+            if os.path.isdir(path):
+                return os.path.join(path, uid + ".po")
+            return path + ".po"
+        elif kind == 'PY':
+            return trans._dst(trans, path, uid, kind)
+        return path
+
+    def invoke(self, context, event):
+        if not hasattr(self, "settings"):
+            self.settings = settings.settings
+        module_name, mod = validate_module(self, context)
+        if mod:
+            self.directory = os.path.dirname(mod.__file__)
+            self.module_name = module_name
+        context.window_manager.fileselect_add(self)
+        return {'RUNNING_MODAL'}
+
+    def execute(self, context):
+        if not hasattr(self, "settings"):
+            self.settings = settings.settings
+        i18n_sett = context.window_manager.i18n_update_svn_settings
+
+        module_name, mod = validate_module(self, context)
+        if not (module_name and mod):
+            return {'CANCELLED'}
+
+        path = mod.__file__
+        if path.endswith("__init__.py"):
+            path = os.path.dirname(path)
+
+        trans = utils_i18n.I18n(kind='PY', src=path, settings=self.settings)
+
+        # Now search given dir, to find po's matching given languages...
+        # Mapping po_uid: po_file.
+        po_files = dict(utils_i18n.get_po_files_from_dir(self.directory))
+
+        # Note: uids in i18n_sett.langs and addon's py code should be the same (both taken from the locale's languages
+        #       file). So we just try to find the best match in po's for each enabled uid.
+        for lng in i18n_sett.langs:
+            if lng.uid in self.settings.IMPORT_LANGUAGES_SKIP:
+                print("Skipping {} language ({}), edit settings if you want to enable it.".format(lng.name, lng.uid))
+                continue
+            if not lng.use:
+                print("Skipping {} language ({}).".format(lng.name, lng.uid))
+                continue
+            uid = lng.uid
+            po_uid = utils_i18n.find_best_isocode_matches(uid, po_files.keys())
+            if not po_uid:
+                print("Skipping {} language, no PO file found for it ({}).".format(lng.name, uid))
+                continue
+            po_uid = po_uid[0]
+            msgs = utils_i18n.I18nMessages(uid=uid, kind='PO', key=uid, src=po_files[po_uid], settings=self.settings)
+            if uid in trans.trans:
+                trans.trans[uid].merge(msgs, replace=True)
+            else:
+                trans.trans[uid] = msgs
+
+        trans.write(kind='PY')
+
+        return {'FINISHED'}
+
+
 class UI_OT_i18n_addon_translation_export(bpy.types.Operator):
-    """Export given addon's translation data as a PO file"""
+    """Export given addon's translation data as PO files"""
     bl_idname = "ui.i18n_addon_translation_export"
     bl_label = "I18n Addon Export"
 
@@ -291,5 +366,3 @@
         trans.write(kind='PO', langs=set(uids))
 
         return {'FINISHED'}
-
-

Modified: trunk/py/scripts/addons/ui_translate/update_ui.py
===================================================================
--- trunk/py/scripts/addons/ui_translate/update_ui.py	2013-04-17 10:01:07 UTC (rev 4480)
+++ trunk/py/scripts/addons/ui_translate/update_ui.py	2013-04-18 15:39:28 UTC (rev 4481)
@@ -129,10 +129,12 @@
             layout.separator()
             layout.label("Addons:")
             row = layout.row()
+            op = row.operator("ui.i18n_addon_translation_invoke", text="Refresh I18n Data...")
+            op.op_id = "ui.i18n_addon_translation_update"
             op = row.operator("ui.i18n_addon_translation_invoke", text="Export PO...")
             op.op_id = "ui.i18n_addon_translation_export"
-            op = row.operator("ui.i18n_addon_translation_invoke", text="Refresh I18n Data...")
-            op.op_id = "ui.i18n_addon_translation_update"
+            op = row.operator("ui.i18n_addon_translation_invoke", text="Import PO...")
+            op.op_id = "ui.i18n_addon_translation_import"
 
 
 ##### Operators #####



More information about the Bf-extensions-cvs mailing list