From c3d67edfbc73473952655740050a9674448f32a7 Mon Sep 17 00:00:00 2001 From: rexdf Date: Wed, 20 Jul 2016 18:25:38 +0800 Subject: [PATCH] support User defined language translation --- Localization.py | 47 +++++++++++++++ Main.sublime-menu | 146 ++++++++++++++++++++++++++++++++-------------- Menu.json | 30 ++++++++++ 3 files changed, 178 insertions(+), 45 deletions(-) create mode 100644 Menu.json diff --git a/Localization.py b/Localization.py index 22c8cf4..a865d72 100755 --- a/Localization.py +++ b/Localization.py @@ -20,6 +20,8 @@ "zipfile": "JA_JP.zip", 'syntax_md5sum': "037128b8f8d2616c7239d8e9a7183b4c" }, + "Unknown": { + }, # "EN": { # This item is deprecated and not used in programming. # "zipfile": "EN.zip", # 'syntax_md5sum': ( @@ -85,6 +87,10 @@ def set_language(lang, force=False): return else: is_en = False + if lang == "Unknown": + lang = get_setting('language_name') + sublime.status_message("%s has loaded." % lang) + return PACKAGES_PATH = sublime.packages_path() DEFAULT_PATH = os.path.join(PACKAGES_PATH, "Default") SYN_PATH = os.path.join(DEFAULT_PATH, "Syntax.sublime-menu") @@ -212,6 +218,47 @@ def is_checked(self, language): return get_setting('language') == language +def get_command(language, action): + PACKAGE_NAME = __name__.split('.')[0] + MENU_RES = "Packages/{}/Menu.json".format(PACKAGE_NAME) + content = sublime.load_resource(MENU_RES) + import json + menu = json.loads(content, "utf-8") + if language not in menu["supports"]: + language = "EN" + return menu["translation"][language][action] + + +class LocalizeToolCommand(sublime_plugin.WindowCommand): + + def run(self, action, file=None): + if action == 'open_file': + sublime.run_command(action, {'file': file}) + elif action == 'new_locale': + language = self.window.show_input_panel( + "Input a locale name", "EN", self.on_done_locale, None, None) + elif action == 'reset': + pass + + def on_done_locale(self, locale): + self.__locale = locale + self.window.show_input_panel( + "Input a language name", "English", self.on_done_lang, None, None) + + def on_done_lang(self, lang): + self.__lang = lang + self.on_done_new() + + def on_done_new(self): + restore_setting('language_locale', self.__locale) + restore_setting('language_name', self.__lang) + restore_setting('language', 'Unknown') + + def description(self, action, file): + language = get_setting('language') + return get_command(language, action) + + def plugin_loaded(): """Load and unzip the files.""" sublime.set_timeout(init, 200) diff --git a/Main.sublime-menu b/Main.sublime-menu index c7603fb..0b38193 100755 --- a/Main.sublime-menu +++ b/Main.sublime-menu @@ -1,50 +1,106 @@ [ - { + { + "children": [ + { + "caption": "Language", "children": [ - { - "caption": "Language", - "children": [ - { - "args": { - "language": "ZH_CN" - }, - "caption": "Simplified Chinese 简体中文", - "checkbox": true, - "command": "toggle_language", - "mnemonic": "S" - }, - { - "args": { - "language": "ZH_TW" - }, - "caption": "Traditional Chinese 正體中文", - "checkbox": true, - "command": "toggle_language", - "mnemonic": "T" - }, - { - "args": { - "language": "JA_JP" - }, - "caption": "Japanese 日本語", - "checkbox": true, - "command": "toggle_language", - "mnemonic": "J" - }, - { - "args": { - "language": "EN" - }, - "caption": "English", - "checkbox": true, - "command": "toggle_language", - "mnemonic": "E" - } - ], - "id": "locale", - "mnemonic": "L" + { + "command": "localize_tool", + "args": { + "action": "open_file", + "file": "${packages}/Default/Main.sublime-menu" } + }, + { + "command": "localize_tool", + "args": { + "action": "reset", + "file": "" + } + }, + { + "command": "localize_tool", + "args": { + "action": "new_locale", + "file": "" + } + }, + { + "caption": "-" + }, + { + "args": { + "language": "ZH_CN" + }, + "caption": "Simplified Chinese 简体中文", + "checkbox": true, + "command": "toggle_language", + "mnemonic": "S" + }, + { + "args": { + "language": "ZH_TW" + }, + "caption": "Traditional Chinese 正體中文", + "checkbox": true, + "command": "toggle_language", + "mnemonic": "T" + }, + { + "args": { + "language": "JA_JP" + }, + "caption": "Japanese 日本語", + "checkbox": true, + "command": "toggle_language", + "mnemonic": "J" + }, + { + "args": { + "language": "EN" + }, + "caption": "English", + "checkbox": true, + "command": "toggle_language", + "mnemonic": "E" + }, + { + "args": { + "language": "Unknown" + }, + "caption": "User Define", + "checkbox": true, + "command": "toggle_language", + "mnemonic": "U" + } ], - "id": "help", - } + "id": "locale", + "mnemonic": "L" + } + ], + "id": "help" + }, + { + "id": "preferences", + "children": [ + { + "caption": "Package Settings", + "id": "package-settings", + "children": [ + { + "caption": "Localization", + "children": [ + { + "caption": "Settings", + "command": "open_file", + "args": { + "file": "${packages}/User/Localization.sublime-settings" + } + } + ] + } + ] + } + ] + } ] \ No newline at end of file diff --git a/Menu.json b/Menu.json new file mode 100644 index 0000000..337d3f7 --- /dev/null +++ b/Menu.json @@ -0,0 +1,30 @@ +{ + "supports": [ + "EN", + "JA_JP", + "ZH_CN", + "ZH_TW" + ], + "translation": { + "EN": { + "open_file": "Edit", + "new_locale": "New Localization", + "reset":"Reset" + }, + "JA_JP": { + "open_file": "Edit", + "new_locale": "New Localization", + "reset":"Reset" + }, + "ZH_CN": { + "open_file": "编辑", + "new_locale": "新翻译", + "reset":"重置" + }, + "ZH_TW": { + "open_file": "編輯", + "new_locale": "新翻譯", + "reset":"重置" + } + } +} \ No newline at end of file