Python API

Code author: Sang Han <jjangsangy@gmail.com>

Translator Co-Routine

translate.translator(source, target, phrase, version='0.0 test', charset='utf-8')[source]

Returns the url encoded string that will be pushed to the translation server for parsing.

List of acceptable language codes for source and target languages can be found as a JSON file in the etc directory.

Some source languages are limited in scope of the possible target languages that are available.

>>> from translate import translator
>>> translator('en', 'zh-TW', 'Hello World!')
    '你好世界!'
Parameters:
  • source (String) – Language code for translation source
  • target (String) – Language code that source will be translate into
  • phrase (String) – Text body string that will be url encoded and translated
Returns:

Request Interface

Return type:

Dictionary

translate.coroutine(func)[source]

Initializes coroutine essentially priming it to the yield statement. Used as a decorator over functions that generate coroutines.

# Basic coroutine producer/consumer pattern
from translate import coroutine

@coroutine
def coroutine_foo(bar):
    try:
        while True:
            baz = (yield)
            bar.send(baz)

    except GeneratorExit:
        bar.close()
Parameters:func (Function) – Unprimed Generator
Returns:Initialized Coroutine
Return type:Function

HTTP Delegation

translate.push_url(interface)[source]

Decorates a function returning the url of translation API. Creates and maintains HTTP connection state

Returns a dict response object from the server containing the translated text and metadata of the request body

Parameters:interface (Function) – Callable Request Interface

Stream Caching

translate.source(target, inputstream=<_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>)[source]

Coroutine starting point. Produces text stream and forwards to consumers

Parameters:
  • target (Coroutine) – Target coroutine consumer
  • inputstream (BufferedTextIO Object) – Input Source
translate.spool(iterable, maxlen=1250)[source]

Consumes text streams and spools them together for more io efficient processes.

Parameters:
  • iterable (Coroutine) – Sends text stream for further processing
  • maxlen (Integer) – Maximum query string size
translate.set_task(translator, translit=False)[source]

Task Setter Coroutine

End point destination coroutine of a purely consumer type. Delegates Text IO to the write_stream function.

Parameters:
  • translation_function (Function) – Translator
  • translit (Boolean) – Transliteration Switch
translate.write_stream(script, output='trans')[source]
Parameters:
  • script (Iterable) – Translated Text
  • output (String) – Output Type (either ‘trans’ or ‘translit’)
translate.accumulator(init, update)[source]

Generic accumulator function.

# Simplest Form
>>> a = 'this' + ' '
>>> b = 'that'
>>> c = functools.reduce(accumulator, a, b)
>>> c
'this that'

# The type of the initial value determines output type.
>>> a = 5
>>> b = Hello
>>> c = functools.reduce(accumulator, a, b)
>>> c
10
Parameters:
  • init – Initial Value
  • update – Value to accumulate
Returns:

Combined Values

Multitasking

translate.set_task(translator, translit=False)[source]

Task Setter Coroutine

End point destination coroutine of a purely consumer type. Delegates Text IO to the write_stream function.

Parameters:
  • translation_function (Function) – Translator
  • translit (Boolean) – Transliteration Switch

Writer

translate.write_stream(script, output='trans')[source]
Parameters:
  • script (Iterable) – Translated Text
  • output (String) – Output Type (either ‘trans’ or ‘translit’)

Language Code Generation

translate.translation_table(language, filepath='supported_translations.json')[source]

Opens up file located under the etc directory containing language codes and prints them out.

Parameters:file (str) – Path to location of json file
Returns:language codes
Return type:dict
translate.print_table(language)[source]

Generates a formatted table of language codes

translate
Access to library is done primarily through the application called translate which will be installed and accessed through a directory under users $PATH envar.