diff --git a/ui_translate/__init__.py b/ui_translate/__init__.py index d374044c4505a65f415dad8515a497b9685594d4..8c7e66c8d4a6f9749f13f3a8d30dcaeee054cd87 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 752f16ceab5e8e206a60d7ca8d0df35542dcd7b0..d26267354a617782ac60d84813fa2958763b5e60 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 758f7cbe2a32f73a768b82429be6254812a8c6df..aebf2a72983ee65e303666c7b011dbe54a866bfd 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 #####