Python client for Globalization Pipeline on IBM Bluemix
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.
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:
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.
To install gp-python-client, simply run:
$ pip install gp-python-client
Or if you’re oldschool, run:
$ easy_install gp-python-client
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()] # 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()] # 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 >>> 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.
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
$ python setup.py sdist
Note: Source distribution contains tests as well.
$ python setup.py bdist
The new distribution files should be located under $BASEDIR/dist/.
Refer to test/README.md.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size & hash SHA256 hash help||File type||Python version||Upload date|
|gp_python_client-1.0.0-py2.py3-none-any.whl (17.1 kB) Copy SHA256 hash SHA256||Wheel||py2.py3|
|gp-python-client-1.0.0.tar.gz (28.5 kB) Copy SHA256 hash SHA256||Source||None|