Skip to content

IBM-Cloud/gp-python-client

Repository files navigation

gp-python-client

gp-python-client is the official Python client for IBM Globalization Pipeline.

IBM Globalization Pipeline is a DevOps integrated application translation management service that you can use to rapidly translate and release cloud and mobile applications to your global customers. Access IBM Globalization Pipeline capabilities through its dashboard, RESTful API, or integrate it seamlessly into your application's Delivery Pipeline.

This package expands on the gettext module (avaliable as part of the Python standard library) and provides functionality for Python applications to use the Globalization Pipeline service.

Getting started

To get started, you should familiarize yourself with the service itself. A good place to begin is by reading the Quick Start Guide and the official Getting Started with IBM Globalization documentation.

The documentation explains how to find the service on IBM Cloud; authentication mechanisms supported; create a new service instance; create a new bundle; access the translated messages.

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 app 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

API

The API documentation for the package can be found here. If the link is broken, you can generate the documentation yourself.

Examples

Example 1 - Bluemix app:

This sample code will allow you to get started using the Globalization Pipeline service in your Bluemix app. The example assumes the Bluemix app has been connected to a Globalization Pipeline service instance and has a bundle named myBundle, and that the bundle contains a source string with key 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. The example assumes a Globalization Pipeline service instance exists with a bundle named myBundle, and that the bundle contains a source string with key exit.

You will need the service instance credentials.

>>> from gpclient import GPClient, GPServiceAccount, GPTranslations
>>> import locale
>>>
>>> acc = GPServiceAccount(url=url, instanceId=instId,
    userId=userId, password=passwd) # Using Globalization Pipeline authentication
# Using IAM 
# acc = GPServiceAccount(url=url, instanceId=instanceId,
#                                apiKey=apiKey)
>>> 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
>>>

You can also provide the service credentials through a JSON file as shown in the snippet below

>>> from gpclient import GPClient, GPServiceAccount
>>>
>>> acc = GPServiceAccount(credentialsJson="./local_credentials.json")
>>> client = GPClient(acc)

Obtaining language/locale codes

This package requires that valid (BCP47 compliant) 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

For the above example, the language code is en_US - where en is the language subtag, and US is the region subtag.

Running Tests

Refer to test/README.md.

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/.

Community

Contributing

See CONTRIBUTING.md.

License

Apache 2.0. See LICENSE.txt.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.