Ejemplo n.º 1
0
class TMModel(BaseTMModel):
    """This is a Google Translate translation memory model.

    The plugin uses the U{Google AJAX Languages API<http://code.google.com/apis/ajaxlanguage/>}
    to query Google's machine translation services.  The implementation makes use of the 
    U{RESTful<http://code.google.com/apis/ajaxlanguage/documentation/#fonje>} interface for 
    Non-JavaScript environments.
    """

    __gtype_name__ = 'GoogleTranslateTMModel'
    #l10n: The name of Google Translate in your language (translated in most languages). See http://translate.google.com/
    display_name = _('Google Translate')
    description = _(
        "Unreviewed machine translations from Google's translation service")
    default_config = {'api_key': ''}

    translate_url = "https://www.googleapis.com/language/translate/v2?key=%(key)s&q=%(message)s&source=%(from)s&target=%(to)s"
    languages_url = "https://www.googleapis.com/language/translate/v2/languages?key=%(key)s"

    # INITIALIZERS #
    def __init__(self, internal_name, controller):
        self.internal_name = internal_name
        super(TMModel, self).__init__(controller)
        self.load_config()
        if not self.config['api_key']:
            self._disable_all(
                "An API key is needed to use the Google Translate plugin")
            return
        self.client = HTTPClient()
        self._languages = set()
        langreq = RESTRequest(self.url_getlanguages % self.config, '')
        self.client.add(langreq)
        langreq.connect('http-success',
                        lambda langreq, response: self.got_languages(response))

    # METHODS #
    def query(self, tmcontroller, unit):
        query_str = unit.source
        # Google's Terms of Service says the whole URL must be less than "2K"
        # characters.
        query_str = query_str[:2000 - len(self.translate_url)]
        source_lang = code_translation.get(self.source_lang,
                                           self.source_lang).replace('_', '-')
        target_lang = code_translation.get(self.target_lang,
                                           self.target_lang).replace('_', '-')
        if source_lang not in self._languages or target_lang not in self._languages:
            logging.debug('language pair not supported: %s => %s' %
                          (source_lang, target_lang))
            return

        if self.cache.has_key(query_str):
            self.emit('match-found', query_str, self.cache[query_str])
        else:
            real_url = self.translate_url % {
                'key': self.config['api_key'],
                'message': urllib.quote_plus(query_str.encode('utf-8')),
                'from': source_lang,
                'to': target_lang,
            }

            req = RESTRequest(real_url, '')
            self.client.add(req)
            # Google's Terms of Service says we need a proper HTTP referrer
            req.curl.setopt(pycurl.REFERER, virtaal_referrer)
            req.connect(
                'http-success', lambda req, response: self.got_translation(
                    response, query_str))
            req.connect(
                'http-client-error',
                lambda req, response: self.got_error(response, query_str))
            req.connect(
                'http-server-error',
                lambda req, response: self.got_error(response, query_str))

    def got_translation(self, val, query_str):
        """Handle the response from the web service now that it came in."""
        # In December 2011 version 1 of the API was deprecated, and we had to
        # release code to handle the eminent disappearance of the API. Although
        # version 2 is now supported, the code is a bit more careful (as most
        # code probably should be) and in case of error we make the list of
        # supported languages empty so that no unnecesary network activity is
        # performed if we can't communicate with the available API any more.
        try:
            data = json.loads(val)
            # We try to access the members to validate that the dictionary is
            # formed in the way we expect.
            data['data']
            data['data']['translations']
            text = data['data']['translations'][0]['translatedText']
        except Exception, e:
            self._disable_all("Error with json response: %s" % e)
            return

        target_unescaped = unescape_html_entities(text)
        if not isinstance(target_unescaped, unicode):
            target_unescaped = unicode(target_unescaped, 'utf-8')
        match = {
            'source': query_str,
            'target': target_unescaped,
            #l10n: Try to keep this as short as possible. Feel free to transliterate.
            'tmsource': _('Google')
        }
        self.cache[query_str] = [match]
        self.emit('match-found', query_str, [match])
Ejemplo n.º 2
0
class TMModel(BaseTMModel):
    """This is a Google Translate translation memory model.

    The plugin uses the U{Google AJAX Languages API<http://code.google.com/apis/ajaxlanguage/>}
    to query Google's machine translation services.  The implementation makes use of the 
    U{RESTful<http://code.google.com/apis/ajaxlanguage/documentation/#fonje>} interface for 
    Non-JavaScript environments.
    """

    __gtype_name__ = 'GoogleTranslateTMModel'
    #l10n: The name of Google Translate in your language (translated in most languages). See http://translate.google.com/
    display_name = _('Google Translate')
    description = _("Unreviewed machine translations from Google's translation service")
    default_config = {'api_key': ''}

    translate_url = "https://www.googleapis.com/language/translate/v2?key=%(key)s&q=%(message)s&source=%(from)s&target=%(to)s"
    languages_url = "https://www.googleapis.com/language/translate/v2/languages?key=%(key)s"

    # INITIALIZERS #
    def __init__(self, internal_name, controller):
        self.internal_name = internal_name
        super(TMModel, self).__init__(controller)
        self.load_config()
        if not self.config['api_key']:
            self._disable_all("An API key is needed to use the Google Translate plugin")
            return
        self.client = HTTPClient()
        self._languages = set()
        langreq = RESTRequest(self.url_getlanguages % self.config, '')
        self.client.add(langreq)
        langreq.connect(
            'http-success',
            lambda langreq, response: self.got_languages(response)
        )

    # METHODS #
    def query(self, tmcontroller, unit):
        query_str = unit.source
        # Google's Terms of Service says the whole URL must be less than "2K"
        # characters.
        query_str = query_str[:2000 - len(self.translate_url)]
        source_lang = code_translation.get(self.source_lang, self.source_lang).replace('_', '-')
        target_lang = code_translation.get(self.target_lang, self.target_lang).replace('_', '-')
        if source_lang not in self._languages or target_lang not in self._languages:
            logging.debug('language pair not supported: %s => %s' % (source_lang, target_lang))
            return

        if self.cache.has_key(query_str):
            self.emit('match-found', query_str, self.cache[query_str])
        else:
            real_url = self.translate_url % {
                'key':     self.config['api_key'],
                'message': urllib.quote_plus(query_str.encode('utf-8')),
                'from':    source_lang,
                'to':      target_lang,
            }

            req = RESTRequest(real_url, '')
            self.client.add(req)
            # Google's Terms of Service says we need a proper HTTP referrer
            req.curl.setopt(pycurl.REFERER, virtaal_referrer)
            req.connect(
                'http-success',
                lambda req, response: self.got_translation(response, query_str)
            )
            req.connect(
                'http-client-error',
                lambda req, response: self.got_error(response, query_str)
            )
            req.connect(
                'http-server-error',
                lambda req, response: self.got_error(response, query_str)
            )

    def got_translation(self, val, query_str):
        """Handle the response from the web service now that it came in."""
        # In December 2011 version 1 of the API was deprecated, and we had to
        # release code to handle the eminent disappearance of the API. Although
        # version 2 is now supported, the code is a bit more careful (as most
        # code probably should be) and in case of error we make the list of
        # supported languages empty so that no unnecesary network activity is
        # performed if we can't communicate with the available API any more.
        try:
            data = json.loads(val)
            # We try to access the members to validate that the dictionary is
            # formed in the way we expect.
            data['data']
            data['data']['translations']
            text = data['data']['translations'][0]['translatedText']
        except Exception, e:
            self._disable_all("Error with json response: %s" % e)
            return

        target_unescaped = unescape_html_entities(text)
        if not isinstance(target_unescaped, unicode):
            target_unescaped = unicode(target_unescaped, 'utf-8')
        match = {
            'source': query_str,
            'target': target_unescaped,
            #l10n: Try to keep this as short as possible. Feel free to transliterate.
            'tmsource': _('Google')
        }
        self.cache[query_str] = [match]
        self.emit('match-found', query_str, [match])
Ejemplo n.º 3
0
class TMModel(BaseTMModel):
    """This is the translation memory model."""

    __gtype_name__ = "ApertiumTMModel"
    display_name = _("Apertium")
    description = _("Unreviewed machine translations from Apertium")

    url = "http://api.apertium.org/json"
    default_config = {"appid": ""}

    # INITIALISERS #
    def __init__(self, internal_name, controller):
        self.internal_name = internal_name
        self.language_pairs = []
        self.load_config()

        self.client = HTTPClient()
        self.url_getpairs = "%(url)s/listPairs?appId=%(appid)s" % {"url": self.url, "appid": self.config["appid"]}
        self.url_translate = "%(url)s/translate" % {"url": self.url}
        self.appid = self.config["appid"]
        langreq = RESTRequest(self.url_getpairs, "", method="GET", data=urllib.urlencode(""), headers=None)
        self.client.add(langreq)
        langreq.connect("http-success", lambda langreq, response: self.got_language_pairs(response))

        super(TMModel, self).__init__(controller)

    # METHODS #
    def query(self, tmcontroller, unit):
        """Send the query to the web service. The response is handled by means
        of a call-back because it happens asynchronously."""
        pair = (self.source_lang, self.target_lang)
        if pair not in self.language_pairs:
            return

        query_str = unit.source
        if self.cache.has_key(query_str):
            self.emit("match-found", query_str, self.cache[query_str])
        else:
            values = {
                "appId": self.appid,
                "q": query_str,
                "langpair": "%s|%s" % (self.source_lang, self.target_lang),
                "markUnknown": "no",
                "format": "html",
            }
            req = RESTRequest(
                self.url_translate + "?" + urllib.urlencode(values),
                "",
                method="GET",
                data=urllib.urlencode(""),
                headers=None,
            )
            self.client.add(req)
            req.connect("http-success", lambda req, response: self.got_translation(response, query_str))

    def got_language_pairs(self, val):
        """Handle the response from the web service to set up language pairs."""
        data = json.loads(val)
        if data["responseStatus"] != 200:
            logging.debug("Failed to get languages:\n%s", (data["responseDetails"]))
            return

        self.language_pairs = [(pair["sourceLanguage"], pair["targetLanguage"]) for pair in data["responseData"]]

    def got_translation(self, val, query_str):
        """Handle the response from the web service now that it came in."""
        data = json.loads(val)

        if data["responseStatus"] != 200:
            logging.debug("Failed to translate '%s':\n%s", (query_str, data["responseDetails"]))
            return

        target = data["responseData"]["translatedText"]
        target = unescape_html_entities(target)
        if target.endswith("\n") and not query_str.endswith("\n"):
            target = target[:-1]  # chop of \n
        if not isinstance(target, unicode):
            target = unicode(target, "utf-8")
        match = {
            "source": query_str,
            "target": target,
            # l10n: Try to keep this as short as possible. Feel free to transliterate in CJK languages for optimal vertical display.
            "tmsource": _("Apertium"),
        }
        self.cache[query_str] = [match]

        self.emit("match-found", query_str, [match])
class TMModel(BaseTMModel):
    """This is a Google Translate translation memory model.

    The plugin uses the U{Google AJAX Languages API<http://code.google.com/apis/ajaxlanguage/>}
    to query Google's machine translation services.  The implementation makes use of the 
    U{RESTful<http://code.google.com/apis/ajaxlanguage/documentation/#fonje>} interface for 
    Non-JavaScript environments.
    """

    __gtype_name__ = "GoogleTranslateTMModel"
    # l10n: The name of Google Translate in your language (translated in most languages). See http://translate.google.com/
    display_name = _("Google Translate")
    description = _("Unreviewed machine translations from Google's translation service")

    translate_url = (
        "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%(message)s&langpair=%(from)s%%7C%(to)s"
    )

    # INITIALIZERS #
    def __init__(self, internal_name, controller):
        self.internal_name = internal_name
        super(TMModel, self).__init__(controller)
        self.client = HTTPClient()

    # METHODS #
    def query(self, tmcontroller, unit):
        query_str = unit.source
        # Google's Terms of Service says no more than 5000 characters
        query_str = query_str[:5000]
        source_lang = code_translation.get(self.source_lang, self.source_lang).replace("_", "-")
        target_lang = code_translation.get(self.target_lang, self.target_lang).replace("_", "-")
        if source_lang not in _languages or target_lang not in _languages:
            logging.debug("language pair not supported: %s => %s" % (source_lang, target_lang))
            return

        if self.cache.has_key(query_str):
            self.emit("match-found", query_str, self.cache[query_str])
        else:
            real_url = self.translate_url % {
                "message": urllib.quote_plus(query_str.encode("utf-8")),
                "from": source_lang,
                "to": target_lang,
            }

            req = RESTRequest(real_url, "", method="GET", data=urllib.urlencode(""), headers=None)
            self.client.add(req)
            # Google's Terms of Service says we need a proper HTTP referrer
            req.curl.setopt(pycurl.REFERER, virtaal_referrer)
            req.connect("http-success", lambda req, response: self.got_translation(response, query_str))

    def got_translation(self, val, query_str):
        """Handle the response from the web service now that it came in."""
        data = json.loads(val)

        if data["responseStatus"] != 200:
            logging.debug("Failed to translate '%s':\n%s" % (query_str, data["responseDetails"]))
            return

        target_unescaped = unescape_html_entities(data["responseData"]["translatedText"])
        if not isinstance(target_unescaped, unicode):
            target_unescaped = unicode(target_unescaped, "utf-8")
        match = {
            "source": query_str,
            "target": target_unescaped,
            # l10n: Try to keep this as short as possible. Feel free to transliterate.
            "tmsource": _("Google"),
        }
        self.cache[query_str] = [match]
        self.emit("match-found", query_str, [match])
Ejemplo n.º 5
0
class TMModel(BaseTMModel):
    """This is the translation memory model."""

    __gtype_name__ = 'ApertiumTMModel'
    display_name = _('Apertium')
    description = _('Unreviewed machine translations from Apertium')

    url = "http://api.apertium.org/json"
    default_config = {
        "appid" : "",
    }

    # INITIALISERS #
    def __init__(self, internal_name, controller):
        self.internal_name = internal_name
        self.language_pairs = []
        self.load_config()

        self.client = HTTPClient()
        self.url_getpairs = "%(url)s/listPairs?appId=%(appid)s" % {"url": self.url, "appid": self.config["appid"]}
        self.url_translate = "%(url)s/translate" % {"url": self.url}
        self.appid = self.config['appid']
        langreq = RESTRequest(self.url_getpairs, '')
        self.client.add(langreq)
        langreq.connect(
            'http-success',
            lambda langreq, response: self.got_language_pairs(response)
        )

        super(TMModel, self).__init__(controller)


    # METHODS #
    def query(self, tmcontroller, unit):
        """Send the query to the web service. The response is handled by means
        of a call-back because it happens asynchronously."""
        pair = (self.source_lang, self.target_lang)
        if pair not in self.language_pairs:
            return

        query_str = unit.source
        if self.cache.has_key(query_str):
            self.emit('match-found', query_str, self.cache[query_str])
        else:
            values = {
                'appId': self.appid,
                'q': query_str,
                'langpair': "%s|%s" % (self.source_lang, self.target_lang),
                'markUnknown': "no",
                'format': 'html',
            }
            req = RESTRequest(self.url_translate + "?" + urllib.urlencode(values), '')
            self.client.add(req)
            req.connect(
                'http-success',
                lambda req, response: self.got_translation(response, query_str)
            )

    def got_language_pairs(self, val):
        """Handle the response from the web service to set up language pairs."""
        data = json.loads(val)
        if data['responseStatus'] != 200:
            import logging
            logging.debug("Failed to get languages:\n%s", (data['responseDetails']))
            return

        self.language_pairs = [(pair['sourceLanguage'], pair['targetLanguage']) for pair in data['responseData']]

    def got_translation(self, val, query_str):
        """Handle the response from the web service now that it came in."""
        data = json.loads(val)

        if data['responseStatus'] != 200:
            import logging
            logging.debug("Failed to translate '%s':\n%s", (query_str, data['responseDetails']))
            return

        target = data['responseData']['translatedText']
        target = unescape_html_entities(target)
        if target.endswith("\n") and not query_str.endswith("\n"):
            target = target[:-1]# chop of \n
        if not isinstance(target, unicode):
            target = unicode(target, 'utf-8')
        match = {
            'source': query_str,
            'target': target,
            #l10n: Try to keep this as short as possible. Feel free to transliterate in CJK languages for optimal vertical display.
            'tmsource': _('Apertium'),
        }
        self.cache[query_str] = [match]

        self.emit('match-found', query_str, [match])
class TMModel(BaseTMModel):
    """This is the translation memory model."""

    __gtype_name__ = 'MicrosoftTranslatorTMModel'
    display_name = _('Microsoft Translator')
    description = _('Unreviewed machine translations from Microsoft Translator')

    default_config = {
        "url" : "http://api.microsofttranslator.com/V1/Http.svc",
        "appid" : "7286B45B8C4816BDF75DC007C1952DDC11C646C1",
    }

    # INITIALISERS #
    def __init__(self, internal_name, controller):
        self.internal_name = internal_name
        self.languages = []
        self.load_config()

        self.client = HTTPClient()
        self.url_getlanguages = "%(url)s/GetLanguages?appId=%(appid)s" % {"url": self.config['url'], "appid": self.config["appid"]}
        self.url_translate = "%(url)s/Translate" % {"url": self.config['url']}
        self.appid = self.config['appid']
        langreq = RESTRequest(self.url_getlanguages, '', method='GET', data=urllib.urlencode(''), headers=None)
        self.client.add(langreq)
        langreq.connect(
            'http-success',
            lambda langreq, response: self.got_languages(response)
        )

        super(TMModel, self).__init__(controller)


    # METHODS #
    def query(self, tmcontroller, unit):
        """Send the query to the web service. The response is handled by means
        of a call-back because it happens asynchronously."""
        if self.source_lang not in self.languages or self.target_lang not in self.languages:
            return

        query_str = unit.source
        if self.cache.has_key(query_str):
            self.emit('match-found', query_str, self.cache[query_str])
        else:
            values = {
                'appId': self.appid,
                'text': query_str,
                'from': self.source_lang,
                'to': self.target_lang
            }
            req = RESTRequest(self.url_translate + "?" + urllib.urlencode(values), '', method='GET', \
                    data=urllib.urlencode(''), headers=None)
            self.client.add(req)
            req.connect(
                'http-success',
                lambda req, response: self.got_translation(response, query_str)
            )

    def got_languages(self, val):
        """Handle the response from the web service to set up language pairs."""
        val = strip_bom(val.decode('utf-8')).strip()
        self.languages = [lang for lang in val.split('\r\n')]

    def got_translation(self, val, query_str):
        """Handle the response from the web service now that it came in."""
        if not isinstance(val, unicode):
            val = unicode(val, 'utf-8')
        val = strip_bom(val)
        match = {
            'source': query_str,
            'target': val,
            #l10n: Try to keep this as short as possible. Feel free to transliterate in CJK languages for optimal vertical display.
            'tmsource': _('Microsoft'),
        }
        self.cache[query_str] = [match]

        self.emit('match-found', query_str, [match])
Ejemplo n.º 7
0
class TMModel(BaseTMModel):
    """This is a Google Translate translation memory model.

    The plugin uses the U{Google AJAX Languages API<http://code.google.com/apis/ajaxlanguage/>}
    to query Google's machine translation services.  The implementation makes use of the 
    U{RESTful<http://code.google.com/apis/ajaxlanguage/documentation/#fonje>} interface for 
    Non-JavaScript environments.
    """

    __gtype_name__ = 'GoogleTranslateTMModel'
    #l10n: The name of Google Translate in your language (translated in most languages). See http://translate.google.com/
    display_name = _('Google Translate')
    description = _("Unreviewed machine translations from Google's translation service")

    translate_url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%(message)s&langpair=%(from)s%%7C%(to)s"

    # INITIALIZERS #
    def __init__(self, internal_name, controller):
        self.internal_name = internal_name
        super(TMModel, self).__init__(controller)
        self.client = HTTPClient()

    # METHODS #
    def query(self, tmcontroller, unit):
        query_str = unit.source
        # Google's Terms of Service says no more than 5000 characters
        query_str = query_str[:5000]
        source_lang = code_translation.get(self.source_lang, self.source_lang).replace('_', '-')
        target_lang = code_translation.get(self.target_lang, self.target_lang).replace('_', '-')
        if source_lang not in _languages or target_lang not in _languages:
            logging.debug('language pair not supported: %s => %s' % (source_lang, target_lang))
            return

        if self.cache.has_key(query_str):
            self.emit('match-found', query_str, self.cache[query_str])
        else:
            real_url = self.translate_url % {
                'message': urllib.quote_plus(query_str.encode('utf-8')),
                'from':    source_lang,
                'to':      target_lang,
            }

            req = RESTRequest(real_url, '')
            self.client.add(req)
            # Google's Terms of Service says we need a proper HTTP referrer
            req.curl.setopt(pycurl.REFERER, virtaal_referrer)
            req.connect(
                'http-success',
                lambda req, response: self.got_translation(response, query_str)
            )
            req.connect(
                'http-client-error',
                lambda req, response: self.got_error(response, query_str)
            )
            req.connect(
                'http-server-error',
                lambda req, response: self.got_error(response, query_str)
            )

    def got_translation(self, val, query_str):
        """Handle the response from the web service now that it came in."""
        # Since we expect Google to shut down the service in December 2011, we
        # try to be extra careful with error handling, and actually expect
        # problems. If we encounter a problem, we make the list of languages
        # empty so that no other requests would be attempted.
        try:
            data = json.loads(val)
            # We try to access the members to validate that the dictionary is
            # formed in the way we expect.
            data['responseStatus']
            data['responseData']['translatedText']
        except Exception, e:
            self._disable_all("Error with json response: %s" % e)
            return

        if data['responseStatus'] != 200:
            logging.debug("Failed to translate '%s':\n%s" % (query_str, data['responseDetails']))
            self._disable_all("responseStatus not 200")
            return

        target_unescaped = unescape_html_entities(data['responseData']['translatedText'])
        if not isinstance(target_unescaped, unicode):
            target_unescaped = unicode(target_unescaped, 'utf-8')
        match = {
            'source': query_str,
            'target': target_unescaped,
            #l10n: Try to keep this as short as possible. Feel free to transliterate.
            'tmsource': _('Google')
        }
        self.cache[query_str] = [match]
        self.emit('match-found', query_str, [match])
Ejemplo n.º 8
0
class TMModel(BaseTMModel):
    """This is the translation memory model."""

    __gtype_name__ = 'MicrosoftTranslatorTMModel'
    display_name = _('Microsoft Translator')
    description = _(
        'Unreviewed machine translations from Microsoft Translator')

    default_config = {
        "url": "http://api.microsofttranslator.com/V1/Http.svc",
        "appid": "7286B45B8C4816BDF75DC007C1952DDC11C646C1",
    }

    # INITIALISERS #
    def __init__(self, internal_name, controller):
        self.internal_name = internal_name
        self.languages = []
        self.load_config()

        self.client = HTTPClient()
        self.url_getlanguages = "%(url)s/GetLanguages?appId=%(appid)s" % {
            "url": self.config['url'],
            "appid": self.config["appid"]
        }
        self.url_translate = "%(url)s/Translate" % {"url": self.config['url']}
        self.appid = self.config['appid']
        langreq = RESTRequest(self.url_getlanguages,
                              '',
                              method='GET',
                              data=urllib.urlencode(''),
                              headers=None)
        self.client.add(langreq)
        langreq.connect('http-success',
                        lambda langreq, response: self.got_languages(response))

        super(TMModel, self).__init__(controller)

    # METHODS #
    def query(self, tmcontroller, unit):
        """Send the query to the web service. The response is handled by means
        of a call-back because it happens asynchronously."""
        if self.source_lang not in self.languages or self.target_lang not in self.languages:
            return

        query_str = unit.source
        if self.cache.has_key(query_str):
            self.emit('match-found', query_str, self.cache[query_str])
        else:
            values = {
                'appId': self.appid,
                'text': query_str,
                'from': self.source_lang,
                'to': self.target_lang
            }
            req = RESTRequest(self.url_translate + "?" + urllib.urlencode(values), '', method='GET', \
                    data=urllib.urlencode(''), headers=None)
            self.client.add(req)
            req.connect(
                'http-success', lambda req, response: self.got_translation(
                    response, query_str))

    def got_languages(self, val):
        """Handle the response from the web service to set up language pairs."""
        val = strip_bom(val.decode('utf-8')).strip()
        self.languages = [lang for lang in val.split('\r\n')]

    def got_translation(self, val, query_str):
        """Handle the response from the web service now that it came in."""
        if not isinstance(val, unicode):
            val = unicode(val, 'utf-8')
        val = strip_bom(val)
        match = {
            'source': query_str,
            'target': val,
            #l10n: Try to keep this as short as possible. Feel free to transliterate in CJK languages for optimal vertical display.
            'tmsource': _('Microsoft'),
        }
        self.cache[query_str] = [match]

        self.emit('match-found', query_str, [match])
Ejemplo n.º 9
0
class TMModel(BaseTMModel):
    """This is a Google Translate translation memory model.

    The plugin uses the U{Google AJAX Languages API<http://code.google.com/apis/ajaxlanguage/>}
    to query Google's machine translation services.  The implementation makes use of the 
    U{RESTful<http://code.google.com/apis/ajaxlanguage/documentation/#fonje>} interface for 
    Non-JavaScript environments.
    """

    __gtype_name__ = 'GoogleTranslateTMModel'
    #l10n: The name of Google Translate in your language (translated in most languages). See http://translate.google.com/
    display_name = _('Google Translate')
    description = _(
        "Unreviewed machine translations from Google's translation service")

    translate_url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%(message)s&langpair=%(from)s%%7C%(to)s"

    # INITIALIZERS #
    def __init__(self, internal_name, controller):
        self.internal_name = internal_name
        super(TMModel, self).__init__(controller)
        self.client = HTTPClient()

    # METHODS #
    def query(self, tmcontroller, unit):
        query_str = unit.source
        # Google's Terms of Service says no more than 5000 characters
        query_str = query_str[:5000]
        source_lang = code_translation.get(self.source_lang,
                                           self.source_lang).replace('_', '-')
        target_lang = code_translation.get(self.target_lang,
                                           self.target_lang).replace('_', '-')
        if source_lang not in _languages or target_lang not in _languages:
            logging.debug('language pair not supported: %s => %s' %
                          (source_lang, target_lang))
            return

        if self.cache.has_key(query_str):
            self.emit('match-found', query_str, self.cache[query_str])
        else:
            real_url = self.translate_url % {
                'message': urllib.quote_plus(query_str.encode('utf-8')),
                'from': source_lang,
                'to': target_lang,
            }

            req = RESTRequest(real_url,
                              '',
                              method='GET',
                              data=urllib.urlencode(''),
                              headers=None)
            self.client.add(req)
            # Google's Terms of Service says we need a proper HTTP referrer
            req.curl.setopt(pycurl.REFERER, virtaal_referrer)
            req.connect(
                'http-success', lambda req, response: self.got_translation(
                    response, query_str))

    def got_translation(self, val, query_str):
        """Handle the response from the web service now that it came in."""
        data = json.loads(val)

        if data['responseStatus'] != 200:
            logging.debug("Failed to translate '%s':\n%s" %
                          (query_str, data['responseDetails']))
            return

        target_unescaped = unescape_html_entities(
            data['responseData']['translatedText'])
        if not isinstance(target_unescaped, unicode):
            target_unescaped = unicode(target_unescaped, 'utf-8')
        match = {
            'source': query_str,
            'target': target_unescaped,
            #l10n: Try to keep this as short as possible. Feel free to transliterate.
            'tmsource': _('Google')
        }
        self.cache[query_str] = [match]
        self.emit('match-found', query_str, [match])