Python client for Globalization Pipeline on IBM Bluemix
Project description
What is this?
gp-python-client is the Python client for Globalization Pipeline on IBM Bluemix.
IBM® Globalization provides machine translation and editing capabilities that enable you to rapidly create, maintain, and revise translations for your Bluemix™ application UI. Use the intuitive Bluemix dashboard or full featured RESTful API to seamlessly translate your English source strings in up to eight other languages while continuing to develop, build, test, and deploy within your Bluemix DevOps environment.
This package expands on the gettext module (avalible in the Python standard library) and provides functionality for Python applications to use the Globalization Pipeline service for translations.
Getting started
To get started, you should first become familiar with the service itself. A good place to begin is by reading the Getting Started with IBM Globalization documentation.
The documentation explains how to create a new service instance, create a new bundle, upload your source text and much more.
If you’re impatient or just need a quick refresher…
Create new Globalization Pipeline service instance:
Create new bundle:
Demo
This demo Bluemix app uses the Globalization Pipeline with the Python client to display a short welcome message that is translated in several languages. The source code for the demo can be found in the demo dir.
Installation
To install gp-python-client, simply run:
$ pip install gp-python-client
Or if you’re oldschool, run:
$ easy_install gp-python-client
Examples
Example 1 - Bluemix app:
This sample code will allow you to get started using the Globalization Pipeline service in your Bluemix app. This example assumes the Bluemix app has been binded with a service instance of Globalization Pipeline and has a bundle named myBundle, and that the bundle contains a source string whose key is greet.
>>> from gpclient import GPClient, GPServiceAccount, GPTranslations >>> import locale >>> >>> acc = GPServiceAccount() >>> client = GPClient(acc) >>> >>> languages=[locale.getdefaultlocale()[0]] # languages=['fr_CA'] >>> >>> t = client.translation(bundleId='myBundle', languages=languages) >>> _ = t.gettext # create alias for method >>> >>> print _('greet') # 'greet' key's French translated value will be used Bonjour >>>
Example 2 - Non-Bluemix app:
This sample code will allow you to get started using the Globalization Pipeline service in a standalone Python app that is not hosted on Bluemix. This example assumes there exists a service instance of Globalization Pipeline that has a bundle named myBundle, and that the bundle contains a source string whose key is exit.
You can find the Globalization Pipeline instance’s url, instanceId, userId, and password in the instance dashboard. It is recommended that you create a new reader account and use it’s credentials in your applications.
>>> from gpclient import GPClient, GPServiceAccount, GPTranslations >>> import locale >>> >>> acc = GPServiceAccount(url=url, instanceId=instId, userId=userId, password=passwd) >>> client = GPClient(acc) >>> >>> languages=[locale.getdefaultlocale()[0]] # languages=['es-mx'] >>> >>> t = client.translation(bundleId='myBundle', languages=languages) >>> _ = t.gettext # create alias for method >>> >>> print _('exit') # 'exit' key's Spanish translated value will be used Adiós >>>
Obtaining language/locale codes
This package requires that valid language/locale codes be provided when asked; for example, when calling GPClient.translation() (see Examples). From these codes, the language, region, and script subtags will be extracted.
Some example codes are:
zh-Hans pt-BR ja en_US
There are several ways to get the code for the working locale. One way is to use the locale module (avaliable as part of the Python standard library).
>>> import locale >>> myLocale = locale.getdefaultlocale() >>> print myLocale ('en_US', 'UTF-8') >>> code = myLocale[0] >>> print code en_US
From this example, the language code is en_US - where en is the language subtag, and US is the region subtag.
Generating documentation
Documentation can be generated using Sphinx - you must first install it:
$ pip install sphinx
Then, to auto generate the documentation, run:
$ cd $BASEDIR/docs $ make clean $ make html
To navigate the documentation, open $BASEDIR/docs/_build/html/index.html.
Creating distribution package
First update CHANGES.txt and setup.py if necessary (e.g. update version number), then create the preferred distribution package.
Wheel distribution (Recommended)
$ pip install wheel $ python setup.py bdist_wheel
Source distribution
$ python setup.py sdist
Note: Source distribution contains tests as well.
Build distribution
$ python setup.py bdist
The new distribution files should be located under $BASEDIR/dist/.
Running Tests
Refer to test/README.md.
End Notes
You are most welcome to submit issues, or fork the repository.
gp-python-client is published under the Apache License Version 2.0.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for gp_python_client-1.0.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d15d11f33302f1250860d442b27f0dcabae7ca2970df93b2448908e89a3e7bd |
|
MD5 | 6bb7d279af86a6e9f52d70323965ca87 |
|
BLAKE2b-256 | e67f7e4015bab934de0f394956ee5d9ce7f1da213cd14f4c34515c0f98a42e53 |