Beispiel #1
0
    def translate(self, text, from_lang, to_lang):

        # Switch to 3 char codes
        from_lang = iso639_convert(from_lang)
        to_lang = iso639_convert(to_lang)

        # Free translate requires text to be URI-encoded
        quoted = urllib.quote(text.encode('utf8'))

        params = {'from': from_lang, 'to': to_lang, 'text': quoted}

        try:
            resp = self._api_post_request('translate', **params)

            if resp.status_code != 200:
                error = API_ERRORS.get(resp.status_code, "Unknown error")
                raise TranslationException("Failed to translate: %s" % error)

            jsobj = json.loads(resp.text)
            text = jsobj['translation']
            return urllib.unquote(text)

        except (ValueError, requests.exceptions.RequestException) as exc:
            raise TranslationException('Translation request failed: %s' %
                                       str(exc))
Beispiel #2
0
    def translate(self, text, from_lang, to_lang):

        # Switch to 3 char codes
        from_lang = iso639_convert(from_lang)
        to_lang = iso639_convert(to_lang)

        # Free translate requires text to be URI-encoded
        quoted = urllib.quote(text.encode('utf8'))

        params = {'from': from_lang, 'to': to_lang, 'text': quoted}

        try:
            resp = self._api_post_request('translate', **params)

            if resp.status_code != 200:
                error = API_ERRORS.get(resp.status_code, "Unknown error")
                raise TranslationException("Failed to translate: %s" % error)

            jsobj = json.loads(resp.text)
            text = jsobj['translation']
            return urllib.unquote(text)

        except (ValueError, requests.exceptions.RequestException) as exc:
            raise TranslationException('Translation request failed: %s' %
                                       str(exc))
Beispiel #3
0
    def activate(self, config):
        self.config = config

        if not self.config.get('active', False):
            return False

        if config.get('key') is None:
            log.error("Don't have an API key, can't proceed")
            return False

        self.key = config['key']
        self.timeout = self.config.get('timeout', API_TIMEOUT)

        self.auth_header = 'BeGlobal apiKey=%s' % self.key

        try:
            resp = self._api_get_request('languages', quality='Q1')

            if resp.status_code != 200:
                log.error("Request failed: %s", resp.text)
                return False

            jsobj = json.loads(resp.text)
        except (ValueError, requests.exceptions.RequestException):
            return False

        self.language_pairs = []

        for obj in jsobj['languageExpertise']['Q1']:
            from_lang = obj['languagePair']['from']['code'].lower()
            to_lang = obj['languagePair']['to']['code'].lower()

            from_lang = iso639_convert(from_lang)
            to_lang = iso639_convert(to_lang)

            self.language_pairs.append((from_lang, to_lang))

        # Just in case the API returns duplicates for whatever reason
        self.language_pairs = list(set(self.language_pairs))

        if len(self.language_pairs) == 0:
            log.error('Got zero translation pairs, aborting')
            return False

        return True
Beispiel #4
0
    def activate(self, config):
        self.config = config

        if not self.config.get('active', False):
            return False

        if config.get('key') is None:
            log.error("Don't have an API key, can't proceed")
            return False

        self.key = config['key']
        self.timeout = self.config.get('timeout', API_TIMEOUT)

        self.auth_header = 'BeGlobal apiKey=%s' % self.key

        try:
            resp = self._api_get_request('languages', quality='Q1')

            if resp.status_code != 200:
                log.error("Request failed: %s", resp.text)
                return False

            jsobj = json.loads(resp.text)
        except (ValueError, requests.exceptions.RequestException):
            return False

        self.language_pairs = []

        for obj in jsobj['languageExpertise']['Q1']:
            from_lang = obj['languagePair']['from']['code'].lower()
            to_lang = obj['languagePair']['to']['code'].lower()

            from_lang = iso639_convert(from_lang)
            to_lang = iso639_convert(to_lang)

            self.language_pairs.append((from_lang, to_lang))

        # Just in case the API returns duplicates for whatever reason
        self.language_pairs = list(set(self.language_pairs))

        if len(self.language_pairs) == 0:
            log.error('Got zero translation pairs, aborting')
            return False

        return True