• Documentation
  • Integrations
  • Python

Python

Whether you are developing an app or just using Python to script some tasks, python-transifex is a library that can help when it comes to send content to Transifex. This is a neat piece of code that wrappers Transifex's API and offers an easy interphase to create projects, sent and pull files directly from your python code. Below you can see a list of the main functionalities it has.

Usage

Authentication

To connect to transifex:

In [1]: from transifex.api import TransifexAPI

# Replace `username` and `password` here with your own username and password
In [2]: t = TransifexAPI('username', 'password', 'http://transifex.com')

In [3]: t.ping()
Out[3]: True

Projects

Create a new public project

Public projects require a repository_url. This can be any valid URL. Private projects do not require this, but you must have a Transifex plan which allows private projects.

In [4]: t.new_project('helloworld5', repository_url='http://gmail.com')

Check if a project already exists

In [5]: t.project_exists('helloworld5')
Out[5]: True

In [6]: t.project_exists('helloworld44345')
Out[6]: False

Resources

A resource is a set of strings which need to be translated into one or more other languages.

Create a resource

In [7]: t.new_resource('helloworld5', '/src/python-transifex/pofile.po', resource_slug='anotherpofile')

List resources

In [8]: t.list_resources('helloworld5')
Out[8]:
[{u'categories': None,
  u'i18n_type': u'PO',
  u'name': u'anotherpofile',
  u'priority': u'1',
  u'slug': u'anotherpofile',
  u'source_language_code': u'en_GB'}]

Delete a resource

In [9]: t.delete_resource('helloworld5', 'anotherpofile')

List the languages this resource is translated into

# First, recreate the resource on the Tranisfex server
in [10]: t.new_resource('helloworld5', '/src/python-transifex/pofile.po')

In [11]: t.list_languages('helloworld5', 'pofilepo')
Out[11]: [u'en_GB']

Uploading translations to Transifex

If you have up to date translations in your codebase, you should update them to Transifex so that the translators don't have to translate everything from scratch.

In [12]: t.new_translation('helloworld5', 'pofilepo', 'pt-br','/src/python-transifex/pofile.po')
Out[12]:
{u'redirect': u'/projects/p/helloworld5/resource/pofilepo/',
 u'strings_added': 0,
 u'strings_delete': 0,
 u'strings_updated': 0}

Downloading translations from Transifex

To download the translations and store them in a local file, run the following:

In [13]: t.get_translation('helloworld5', 'pofilepo', 'pt-br', '/src/python-transifex/pofile_ptbr.po')