From 9208bd771af897c13dc7be43496b7619a2b47be2 Mon Sep 17 00:00:00 2001
From: Bastien Montagne <montagne29@wanadoo.fr>
Date: Thu, 18 Apr 2013 15:39:28 +0000
Subject: [PATCH] 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.
---
 ui_translate/__init__.py     |  2 +-
 ui_translate/update_addon.py | 79 ++++++++++++++++++++++++++++++++++--
 ui_translate/update_ui.py    |  6 ++-
 3 files changed, 81 insertions(+), 6 deletions(-)

diff --git a/ui_translate/__init__.py b/ui_translate/__init__.py
index d374044c4..8c7e66c8d 100644
--- a/ui_translate/__init__.py
+++ b/ui_translate/__init__.py
@@ -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, "
diff --git a/ui_translate/update_addon.py b/ui_translate/update_addon.py
index 752f16cea..d26267354 100644
--- a/ui_translate/update_addon.py
+++ b/ui_translate/update_addon.py
@@ -215,8 +215,83 @@ class UI_OT_i18n_addon_translation_update(bpy.types.Operator):
         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 @@ class UI_OT_i18n_addon_translation_export(bpy.types.Operator):
         trans.write(kind='PO', langs=set(uids))
 
         return {'FINISHED'}
-
-
diff --git a/ui_translate/update_ui.py b/ui_translate/update_ui.py
index 758f7cbe2..aebf2a729 100644
--- a/ui_translate/update_ui.py
+++ b/ui_translate/update_ui.py
@@ -129,10 +129,12 @@ class UI_PT_i18n_update_translations_settings(bpy.types.Panel):
             layout.separator()
             layout.label("Addons:")
             row = layout.row()
-            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="Export PO...")
+            op.op_id = "ui.i18n_addon_translation_export"
+            op = row.operator("ui.i18n_addon_translation_invoke", text="Import PO...")
+            op.op_id = "ui.i18n_addon_translation_import"
 
 
 ##### Operators #####
-- 
GitLab