# -*- coding: utf-8 -*-
import sys, os, gettext, platform, utils
reload(sys)
#Pluginsystem support
import pluginsystem
#Configsystem support
import configsystem
#Google support
import translator
#UTF-8 support
sys.setdefaultencoding('utf-8')
#Locale support
try:
LOCALE_DIR = "plugins" + os.path.sep + "google-translator" + os.path.sep + "locale"
t = gettext.translation("main", LOCALE_DIR)
_ = t.ugettext
except:
_ = utils.fake_locale
#Global vars
capabilities = [_("!google-translator"), _("!google-translator-config"), _("?google-translator"), _("?google-translator-config")]
config = (
((_("Languages")),
(
("preferedlanguagefrom", ("list", 0, (_("Prefered 'from' language")), ((_("Auto")), (_("Arabic")), (_("Bulgarian")), (_("Catalan")), (_("Chinese")), (_("Croatian")), (_("Czech")), (_("Danish")), (_("Dutch")), (_("English")), (_("Filipino")), (_("Finnish")), (_("French")), (_("German")), (_("Greek")), (_("Hebrew")), (_("Hindi")), (_("Indonesian")), (_("Italian")), (_("Japanese")), (_("Korean")), (_("Latvian")), (_("Lithuanian")), (_("Norwegian")), (_("Polish")), (_("Portuguese")), (_("Romanian")), (_("Russian")), (_("Serbian")), (_("Slovak")), (_("Slovenian")), (_("Spanish")), (_("Swedish")), (_("Ukrainian")), (_("Vietnamese"))))),
("preferedlanguageto", ("list", 0, (_("Prefered 'to' language")), ((_("Arabic")), (_("Bulgarian")), (_("Catalan")), (_("Chinese")), (_("Croatian")), (_("Czech")), (_("Danish")), (_("Dutch")), (_("English")), (_("Filipino")), (_("Finnish")), (_("French")), (_("German")), (_("Greek")), (_("Hebrew")), (_("Hindi")), (_("Indonesian")), (_("Italian")), (_("Japanese")), (_("Korean")), (_("Latvian")), (_("Lithuanian")), (_("Norwegian")), (_("Polish")), (_("Portuguese")), (_("Romanian")), (_("Russian")), (_("Serbian")), (_("Slovak")), (_("Slovenian")), (_("Spanish")), (_("Swedish")), (_("Ukrainian")), (_("Vietnamese"))))),
)
),
((_("Appearance")),
(
("plaintextcolor", ("color", "#C3C3C3", (_("Color of plain text")))),
)
),
(3)
)
config_preferedlanguagefrom = ['auto', 'ar', 'bg', 'ca', 'zh-CN', 'hr', 'cs', 'da', 'nl', 'en', 'tl', 'fi', 'fr', 'de', 'el', 'iw', 'hi', 'id', 'it', 'ja', 'ko', 'lv', 'lt', 'no', 'pl', 'pt', 'ro', 'ru', 'sr', 'sk', 'sl', 'es', 'sv', 'uk', 'vi']
config_preferedlanguageto = ['ar', 'bg', 'ca', 'zh-CN', 'hr', 'cs', 'da', 'nl', 'en', 'tl', 'fi', 'fr', 'de', 'el', 'iw', 'hi', 'id', 'it', 'ja', 'ko', 'lv', 'lt', 'no', 'pl', 'pt', 'ro', 'ru', 'sr', 'sk', 'sl', 'es', 'sv', 'uk', 'vi']
#Init event
@pluginsystem.register("init")
def init(*args, **kwargs):
global config
tmp_config = configsystem.load_config("google-translator", config)
if not tmp_config == None:
config = tmp_config
#Reload-config event to make a specific plugin reload it's config
@pluginsystem.register("reload_config")
def reload_config(*args, **kwargs):
global config
if "plugin" in kwargs:
if kwargs["plugin"] == "google-translator":
tmp_config = configsystem.load_config("google-translator", config)
if not tmp_config == None:
config = tmp_config
else:
print _("WARNING from Google-translator plugin: reload_config event was called with inappropiate number of arguments! Please check code.")
#Triggered after return/enter hit
@pluginsystem.register("analyze_command")
def analyze_command(*args, **kwargs):
global config
if "data" in kwargs and "obj" in kwargs:
qcmd = kwargs["data"] #in case we need the QString instead of...
cmd = str(kwargs["data"]) #...python string
console = kwargs["obj"] #save the console from which we got the call
#Check config to see from what language we should translate.
languagefrom = config_preferedlanguagefrom[configsystem.value(config, 'preferedlanguagefrom')]
#Check config to see to what language we should translate.
languageto = config_preferedlanguageto[configsystem.value(config, 'preferedlanguageto')]
if cmd.startswith(_("!google-translator")) and qcmd.at(len(_("!google-translator"))).isSpace():
searchstring = cmd[len(_("!google-translator"))+1:]
#Make the search
result = translator.translate(searchstring, languagefrom, languageto)
if result == None:
console.printString("<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + _("No text to show or an error occurred.") + "</font>", False, False)
return
output = "<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + result + "</font>"
console.printString(output, False, False)
elif cmd == _("!google-translator-config"):
configsystem.config_dialog(config, "google-translator", console)
elif cmd == _("?google-translator"):
console.printString("<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + _("The command !google-translator will translate given string and will print the results in this console. Example of usage: !google-translator This is a text I want to translate to Spanish.") + "</font>", False, False)
elif cmd == _("?google-translator-config"):
console.printString("<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + _("The command !google-translator-config will show up a config dialog which will let you configure the behavior of this plugin. Choose from and to what language you want to translate.") + "</font>", False, False)
else:
print _("WARNING from Google-translator plugin: analyze_command event was called with inappropiate number of arguments! Please check code.")
#Send all available commands to the plugin system
@pluginsystem.register("get_capabilities")
def get_capabilities(*args, **kwargs):
if "cap" in kwargs:
kwargs["cap"].extend(capabilities)
else:
print _("WARNING from Google-translator plugin: get_capabilities event was called with inappropiate number of arguments! Please check code.")