goslate 1.1.2 documentation

Goslate: Free Google Translate API

Contents

Goslate: Free Google Translate API

goslate provides you free python API to google translation service by querying google translation website.

It is:

Usage

>>> import goslate
>>> gs = goslate.Goslate()
>>> print gs.translate('hello world', 'de')
hallo welt

For batch translation, language detection, proxy support etc., please check API reference

Install

goslate support both Python2 and Python3. You could install it via:

$ pip install goslate

or just download latest goslate.py directly and use

futures is optional but recommended to install for best performance.

CLI

goslate.py is also a command line tool

  • Translate stdin input into Chinese

    $ echo "hello world" | goslate.py -t zh-CN
    
  • Translate 2 text files into Chinese, output to UTF-8 file

    $ goslate.py -t zh-CN -o utf-8 source/1.txt "source 2.txt" > output.txt
    

use --help for detail usage

$ goslate.py -h

Reference

Goslate: Free Google Translate API

exception goslate.Error

Error type

class goslate.Goslate(opener=None, retry_times=4, executor=<concurrent.futures.thread.ThreadPoolExecutor object at 0x10b157510>, timeout=4, debug=False)

All goslate API lives in this class

You have to first create an instance of Goslate to use this API

Parameters:
  • opener – The url opener to be used for HTTP/HTTPS query. If not provide, a default opener will be used. For proxy support you should provide an opener with ProxyHandler
  • retry_times (int) – how many times to retry when connection reset error occured. Default to 4
  • executor (futures.ThreadPoolExecutor) –

    the multi thread executor for handling batch input, default to a global futures.ThreadPoolExecutor instance with 120 max thead workers if futures is avalible. Set to None to disable multi thread support

    Note

    multi thread worker relys on futures, if it is not avalible, goslate will work under single thread mode

  • timeout (int/float) – HTTP request timeout in seconds
  • debug (bool) – Turn on/off the debug output
Example :
>>> import goslate
>>>
>>> # Create a Goslate instance to use first
>>> gs = goslate.Goslate()
>>> 
>>> # You could get all supported language list through get_languages
>>> languages = gs.get_languages()
>>> print(languages['en'])
English
>>>
>>> # Tranlate the languages' name into Chinese
>>> language_names = languages.values()
>>> language_names_in_chinese = gs.translate(language_names, 'zh')
>>> 
>>> # verify each Chinese name is really in Chinese using detect
>>> language_codes = gs.detect(language_names_in_chinese)
>>> for code in language_codes:
...     assert 'zh-CN' == code
...
>>>
detect(text)

Detect language of the input text

Note

  • Input all source strings at once. Goslate will detect concurrently for maximize speed.
  • futures is required for best performance.
  • It returns generator on batch input in order to better fit pipeline architecture.
Parameters:text (UTF-8 str; unicode; sequence of string) – The source text(s) whose language you want to identify. Batch detection is supported via sequence input
Returns:the language code(s)
  • unicode: on single string input
  • generator of unicode: on batch input of string sequence
Raises :Error if parameter type or value is not valid

Example:

>>> gs = Goslate()
>>> print(gs.detect('hello world'))
en
>>> for i in gs.detect([u'hello', 'Hallo']):
...     print(i)
...
en
de
get_languages()

Discover supported languages

It returns iso639-1 language codes for supported languages for translation. Some language codes also include a country code, like zh-CN or zh-TW.

Note

It only queries Google once for the first time and use cached result afterwards

Returns:a dict of all supported language code and language name mapping {'language-code', 'Language name'}
Example :
>>> languages = Goslate().get_languages()
>>> assert 'zh' in languages
>>> print(languages['zh'])
Chinese
translate(text, target_language, source_language=u'')

Translate text from source language to target language

Note

  • Input all source strings at once. Goslate will batch and fetch concurrently for maximize speed.
  • futures is required for best performance.
  • It returns generator on batch input in order to better fit pipeline architecture
Parameters:
  • text (UTF-8 str; unicode; string sequence (list, tuple, iterator, generator)) – The source text(s) to be translated. Batch translation is supported via sequence input
  • target_language (str; unicode) – The language to translate the source text into. The value should be one of the language codes listed in get_languages()
  • source_language (str; unicode) – The language of the source text. The value should be one of the language codes listed in get_languages(). If a language is not specified, the system will attempt to identify the source language automatically.
Returns:

the translated text(s)

  • unicode: on single string input
  • generator of unicode: on batch input of string sequence

Raises :
  • Error (‘invalid target language’) if target language is not set
  • Error (‘input too large’) if input a single large word without any punctuation or space in between
Example :
>>> gs = Goslate()
>>> print(gs.translate('Hello World', 'de'))
Hallo Welt
>>> 
>>> for i in gs.translate(['thank', u'you'], 'de'):
...     print(i)
...
danke
Sie

Contents