common language interface and wordreference backend

Conflicts:

	modules/wordreference/pages.py
	weboob/applications/translaboob/translaboob.py
This commit is contained in:
lucien 2012-04-03 09:41:38 +02:00 committed by Romain Bignon
commit 657e2213ac
11 changed files with 287 additions and 19 deletions

View file

@ -19,7 +19,7 @@
"backend for http://translate.google.com"
from weboob.capabilities.translate import ICapTranslate, Translation, TranslationFail
from weboob.capabilities.translate import ICapTranslate, Translation, TranslationFail, LanguageNotSupported
from weboob.tools.backend import BaseBackend
from .browser import GoogleTranslateBrowser
@ -36,12 +36,30 @@ class GoogleTranslateBackend(BaseBackend, ICapTranslate):
NAME = 'googletranslate'
DESCRIPTION = u'Google translation web service'
BROWSER = GoogleTranslateBrowser
GOOGLELANGUAGE = {
'Arabic':'ar', 'Afrikaans':'af', 'Albanian':'sq', 'Armenian':'hy', 'Azerbaijani':'az', 'Basque':'eu', 'Belarusian':'be',
'Bengali':'bn', 'Bulgarian':'bg', 'Catalan':'ca', 'Chinese':'zh-CN', 'Croatian':'hr', 'Czech':'cs', 'Danish':'da',
'Dutch':'nl', 'English':'en', 'Esperanto':'eo', 'Estonian':'et', 'Filipino':'tl', 'Finnish':'fi', 'French':'fr',
'Galician':'gl', 'Georgian':'ka', 'German':'de', 'Greek':'gr', 'Gujarati':'gu', 'Haitian':'ht', 'Hebrew':'iw',
'Hindi':'hi', 'Hungaric':'hu', 'Icelandic':'is', 'Indonesian':'id', 'Irish':'ga', 'Italian':'it', 'Japanese':'ja',
'Kannada':'kn', 'Korean':'ko', 'Latin':'la', 'Latvian':'lv', 'Lithuanian':'lt', 'Macedonian':'mk', 'Malay':'ms',
'Maltese':'mt', 'Norwegian':'no', 'Persian':'fa', 'Polish':'pl', 'Portuguese':'pt', 'Romanian':'ro', 'Russian':'ru',
'Serbian':'sr', 'Slovak':'sk', 'Slovenian':'sl', 'Spanish':'es', 'Swahili':'sw', 'Swedish':'sv', 'Tamil':'ta',
'Telugu':'te', 'Thai':'th', 'Turkish':'tr', 'Ukrainian':'uk', 'Urdu':'ur', 'Vietnamese':'vi', 'Welsh':'cy', 'Yiddish':'yi',
}
def translate(self, lan_from, lan_to, text):
if not lan_from in self.GOOGLELANGUAGE.keys():
raise LanguageNotSupported()
if not lan_to in self.GOOGLELANGUAGE.keys():
raise LanguageNotSupported()
translation = Translation(0)
translation.lang_src = unicode(lan_from)
translation.lang_dst = unicode(lan_to)
translation.text = self.browser.translate(lan_from, lan_to, text)
translation.lang_src = unicode(self.GOOGLELANGUAGE[lan_from])
translation.lang_dst = unicode(self.GOOGLELANGUAGE[lan_to])
translation.text = self.browser.translate(self.GOOGLELANGUAGE[lan_from], self.GOOGLELANGUAGE[lan_to], text)
if translation.text is None:
raise TranslationFail()