Skip to content

Commit

Permalink
support User defined language translation
Browse files Browse the repository at this point in the history
  • Loading branch information
rexdf committed Jul 20, 2016
1 parent 9e3d9cd commit c3d67ed
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 45 deletions.
47 changes: 47 additions & 0 deletions Localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': (
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
146 changes: 101 additions & 45 deletions Main.sublime-menu
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
]
}
]
}
]
30 changes: 30 additions & 0 deletions Menu.json
Original file line number Diff line number Diff line change
@@ -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":"重置"
}
}
}

0 comments on commit c3d67ed

Please sign in to comment.