Python API

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

Translator Co-Routine

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

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)

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)

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'>)

Coroutine starting point. Produces text stream and forwards to consumers

Parameters:target (Coroutine) – Target coroutine consumer
translate.spool(iterable, maxlen=1250)

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)

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 – Transliteration Switch
Type:

Bool

translate.write_stream(script, output='trans')
Parameters:script (Iterable) – Translated Text
Return None:
translate.accumulator(init, update)

Generic accumulator function.

Parameters:
  • init – Initial Value
  • update – Value to accumulate
Returns:

Combined Values

Multitasking

translate.set_task(translator, translit=False)

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 – Transliteration Switch
Type:

Bool

Writer

translate.write_stream(script, output='trans')
Parameters:script (Iterable) – Translated Text
Return None:

Language Code Generation

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

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)

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.