@@ -0,0 +1,2185 @@
+%0A# encoding: utf-8%0A%0Aimport microsofttranslator as _microsofttranslator%0A%0A%0Aclass EmptyTranslationChainError(Exception):%0A    %22%22%22%0A    Error to indicate translation chaining list must contain%0A    more than two language labels.%0A    %22%22%22%0A    pass%0A%0A%0Aclass ChainTranslator(object):%0A    %22%22%22%0A    Chain multiple translation using Microsoft Translator API%0A    (http://api.microsofttranslator.com/V2/Http.svc/Translate),%0A     using 'microsofttranslator' module%0A    (git://github.com/openlabs/Microsoft-Translator-Python-API.git)%0A    for API access.%0A%0A    This class supplies simple wrapper for 'Translator' class in %0A    microsofttranslator module, and ease the operations of translation%0A    requests.%0A%0A    Usage:%0A        %3E%3E%3E from mstranschain import ChainTranslator%0A        %3E%3E%3E chain_translator = ChainTranslator('%3CYour Client ID%3E', '%3CYour Client Secret%3E')%0A        %3E%3E%3E print(chain_translator.translate(%22%E3%81%8A%E3%81%AF%E3%82%88%E3%81%86%E3%80%81%E8%AB%B8%E5%90%9B%E3%80%82%22, chain=%5B'ja', 'en', 'pt', 'ja'%5D)%0A        u%22%22%0A    %22%22%22%0A%0A    def __init__(self, client_id, client_secret):%0A        self._translator = _microsofttranslator.Translator(client_id, client_secret)%0A%0A    def translate(self, text, lang_chain):%0A        %22%22%22%0A        Translate a text string through languages in lang_chain list.%0A        lang_chain must contain two or more string labels for languages.%0A        %22%22%22%0A        # Shortage of lang_chain error.%0A        if len(lang_chain) %3C 2:%0A            raise EmptyTranslationChainError('ChainTranslator.translate takes two or more languages to chain translation.')%0A        # Shrink lang_chain if sequences of identical lang-labels found.%0A        lang_chain_shrink = %5Blang_chain.pop(0)%5D%0A        for lang in lang_chain:%0A            if lang == lang_chain_shrink%5B-1%5D:%0A                continue%0A            else:%0A                lang_chain_shrink.append(lang)%0A        # Shorten into less than pair by shrinking, results no error & returns results.%0A        if len(lang_chain_shrink) == 1:%0A            return text%0A        # Translation process.%0A        result = text%0A        for from_lang, to_lang in zip(lang_chain_shrink, lang_chain_shrink%5B1:%5D):%0A            result = self._translator.translate(result, to_lang=to_lang, from_lang=from_lang)%0A        return result%0A

Implement ChainTranslator.