Google: Keep the list of supported languages in one place.

Signed-off-by: James Vega <jamessan@users.sourceforge.net>
(cherry picked from commit 461f943d97)
This commit is contained in:
James Vega 2009-06-26 18:21:16 -04:00
parent 65a180798f
commit 2b0b74dfe0
2 changed files with 40 additions and 24 deletions

View File

@ -1,6 +1,6 @@
### ###
# Copyright (c) 2005, Jeremiah Fincher # Copyright (c) 2005, Jeremiah Fincher
# Copyright (c) 2008, James Vega # Copyright (c) 2008-2009, James Vega
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -48,15 +48,41 @@ def configure(advanced):
conf.supybot.plugins.Google.searchSnarfer.setValue(True) conf.supybot.plugins.Google.searchSnarfer.setValue(True)
class Language(registry.OnlySomeStrings): class Language(registry.OnlySomeStrings):
validStrings = ['lang_' + s for s in 'ar bg ca zh-CN zh-TW hr cs da nl en ' transLangs = {'Afrikaans': 'af', 'Albanian': 'sq', 'Amharic': 'am',
'et fi fr de el iw hu is id it ja ko ' 'Arabic': 'ar', 'Armenian': 'hy', 'Azerbaijani': 'az',
'lv lt no pl pt ro ru sr sk sl es sv ' 'Basque': 'eu', 'Belarusian': 'be', 'Bengali': 'bn',
'tr'.split()] 'Bulgarian': 'bg', 'Burmese': 'my', 'Catalan': 'ca',
'Chinese': 'zh', 'Chinese_simplified': 'zh-CN',
'Chinese_traditional': 'zh-TW', 'Croatian': 'hr',
'Czech': 'cs', 'Danish': 'da', 'Dhivehi': 'dv',
'Dutch': 'nl', 'English': 'en', 'Esperanto': 'eo',
'Estonian': 'et', 'Filipino': 'tl', 'Finnish': 'fi',
'French': 'fr', 'Galician': 'gl', 'Georgian': 'ka',
'German': 'de', 'Greek': 'el', 'Gujarati': 'gu',
'Hebrew': 'iw', 'Hindi': 'hi', 'Hungarian': 'hu',
'Icelandic': 'is', 'Indonesian': 'id', 'Inuktitut': 'iu',
'Italian': 'it', 'Japanese': 'ja', 'Kannada': 'kn',
'Kazakh': 'kk', 'Khmer': 'km', 'Korean': 'ko',
'Kurdish': 'ku', 'Kyrgyz': 'ky', 'Laothian': 'lo',
'Latvian': 'lv', 'Lithuanian': 'lt', 'Macedonian': 'mk',
'Malay': 'ms', 'Malayalam': 'ml', 'Maltese': 'mt',
'Marathi': 'mr', 'Mongolian': 'mn', 'Nepali': 'ne',
'Norwegian': 'no', 'Oriya': 'or', 'Pashto': 'ps',
'Persian': 'fa', 'Polish': 'pl', 'Portuguese': 'pt-PT',
'Punjabi': 'pa', 'Romanian': 'ro', 'Russian': 'ru',
'Sanskrit': 'sa', 'Serbian': 'sr', 'Sindhi': 'sd',
'Sinhalese': 'si', 'Slovak': 'sk', 'Slovenian': 'sl',
'Spanish': 'es', 'Swedish': 'sv', 'Tajik': 'tg',
'Tamil': 'ta', 'Tagalog': 'tl', 'Telugu': 'te',
'Thai': 'th', 'Tibetan': 'bo', 'Turkish': 'tr',
'Ukranian': 'uk', 'Urdu': 'ur', 'Uzbek': 'uz',
'Uighur': 'ug', 'Vietnamese': 'vi'}
validStrings = ['lang_' + s for s in transLangs.values()]
validStrings.append('') validStrings.append('')
def normalize(self, s): def normalize(self, s):
if s and not s.startswith('lang_'): if s and not s.startswith('lang_'):
s = 'lang_' + s s = 'lang_' + s
if not s.endswith('CN') or s.endswith('TW'): if not s.endswith('CN') or s.endswith('TW') or s.endswith('PT'):
s = s.lower() s = s.lower()
else: else:
s = s.lower()[:-2] + s[-2:] s = s.lower()[:-2] + s[-2:]

View File

@ -231,16 +231,6 @@ class Google(callbacks.PluginRegexp):
irc.reply(s) irc.reply(s)
_gtranslateUrl='http://ajax.googleapis.com/ajax/services/language/translate' _gtranslateUrl='http://ajax.googleapis.com/ajax/services/language/translate'
_transLangs = {'Arabic': 'ar', 'Bulgarian': 'bg',
'Chinese_simplified': 'zh-CN',
'Chinese_traditional': 'zh-TW', 'Croatian': 'hr',
'Czech': 'cs', 'Danish': 'da', 'Dutch': 'nl',
'English': 'en', 'Finnish': 'fi', 'French': 'fr',
'German': 'de', 'Greek': 'el', 'Hindi': 'hi',
'Italian': 'it', 'Japanese': 'ja', 'Korean': 'ko',
'Norwegian': 'no', 'Polish': 'pl', 'Portuguese': 'pt',
'Romanian': 'ro', 'Russian': 'ru', 'Spanish': 'es',
'Swedish': 'sv'}
def translate(self, irc, msg, args, fromLang, toLang, text): def translate(self, irc, msg, args, fromLang, toLang, text):
"""<from-language> [to] <to-language> <text> """<from-language> [to] <to-language> <text>
@ -257,22 +247,22 @@ class Google(callbacks.PluginRegexp):
headers['Referer'] = ref headers['Referer'] = ref
opts = {'q': text, 'v': '1.0'} opts = {'q': text, 'v': '1.0'}
lang = conf.supybot.plugins.Google.defaultLanguage lang = conf.supybot.plugins.Google.defaultLanguage
if fromLang.capitalize() in self._transLangs: if fromLang.capitalize() in lang.transLangs:
fromLang = self._transLangs[fromLang.capitalize()] fromLang = lang.transLangs[fromLang.capitalize()]
elif lang.normalize('lang_'+fromLang)[5:] \ elif lang.normalize('lang_'+fromLang)[5:] \
not in self._transLangs.values(): not in lang.transLangs.values():
irc.errorInvalid('from language', fromLang, irc.errorInvalid('from language', fromLang,
format('Valid languages are: %L', format('Valid languages are: %L',
self._transLangs.keys())) lang.transLangs.keys()))
else: else:
fromLang = lang.normalize('lang_'+fromLang)[5:] fromLang = lang.normalize('lang_'+fromLang)[5:]
if toLang.capitalize() in self._transLangs: if toLang.capitalize() in lang.transLangs:
toLang = self._transLangs[toLang.capitalize()] toLang = lang.transLangs[toLang.capitalize()]
elif lang.normalize('lang_'+toLang)[5:] \ elif lang.normalize('lang_'+toLang)[5:] \
not in self._transLangs.values(): not in lang.transLangs.values():
irc.errorInvalid('to language', toLang, irc.errorInvalid('to language', toLang,
format('Valid languages are: %L', format('Valid languages are: %L',
self._transLangs.keys())) lang.transLangs.keys()))
else: else:
toLang = lang.normalize('lang_'+toLang)[5:] toLang = lang.normalize('lang_'+toLang)[5:]
opts['langpair'] = '%s|%s' % (fromLang, toLang) opts['langpair'] = '%s|%s' % (fromLang, toLang)