Skip to main content

Python client for Globalization Pipeline on IBM Cloud

Project description

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.

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

gp-python-client-1.1.2.tar.gz (35.5 kB view details)

Uploaded Source

Built Distribution

gp_python_client-1.1.2-py2.py3-none-any.whl (19.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file gp-python-client-1.1.2.tar.gz.

File metadata

  • Download URL: gp-python-client-1.1.2.tar.gz
  • Upload date:
  • Size: 35.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.5

File hashes

Hashes for gp-python-client-1.1.2.tar.gz
Algorithm Hash digest
SHA256 dbfea5d2b37afe784b34cdb3868899e98a81b70f27809ccf996b3c81f14baa26
MD5 0b0b28ebb0bf68a042ed2b6eb286139c
BLAKE2b-256 5a57d32c62c4a2311dac1b1d7ef2342275ab282d89d54a7fef4b7a5e6ef0283f

See more details on using hashes here.

File details

Details for the file gp_python_client-1.1.2-py2.py3-none-any.whl.

File metadata

  • Download URL: gp_python_client-1.1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 19.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.5

File hashes

Hashes for gp_python_client-1.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f2a3185df672e097bc91d1cb12b2b0a0f8caed36791963d092ff1efe859d74d1
MD5 26756bdf331906c7af40a064991dfae3
BLAKE2b-256 b33602956110b0aa09e5b0e1084e6604aed43f83dd376ed5288f42b28f02cc16

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page