GPClient

class gpclient.gpclient.GPClient(serviceAccount, auth='HMAC', cacheTimeout=10)

Handles interaction with the Globalization Pipeline (GP) service instance. serviceAccount must be of type GPServiceAccount and will be used to obtain the necessary credentials required for contacting the Globalization Pipeline service instance.

The caching feature may be used to cache translated values locally in order to reduce the number of calls made to the GP service. The cacheTimeout can have the following values (in minutes):

  • cacheTimeout = 0, do not cache
  • cacheTimeout = -1, cache forever
  • cacheTimeout > 0, store cache value for specified number of minutes

The default cacheTimeout value is 10 minutes

The type of authentication to use for requests can also be specified. Currently, the following are supported:

  • HMAC authentication: auth=GPClient.HMAC_AUTH
  • HTTP Basic Access authentication: auth=GPClient.BASIC_AUTH

The default auth value is GPClient.HMAC_AUTH. Note, at this time, only Reader-type accounts are allowed to use Basic authentication.

BASIC_AUTH = 'basic'
HMAC_AUTH = 'HMAC'
get_avaliable_languages(bundleId)

Returns a list of avaliable languages in the bundle

gp_translation(bundleId, languages)

Returns an instance of GPTranslations to be used for obtaining translations.

bundleId is the name of the bundle to use. languages is the list of languages to use, with subsequent ones being fallbacks. For example, to fallback to Spanish if French translated values are not found, languages=['fr', 'es'].

translation(bundleId, languages, priority='gp', domain=None, localedir=None, class_=None, codeset=None)

Returns the Translations instance to be used for obtaining translations.

bundleId is the name of the bundle to use. languages is the list of languages to use, with subsequent ones being fallbacks. Additionally, based on the value of priority, local translated values can be given precedence over Globalization Pipeline translated values.

For example, to fallback to Spanish if French translated values are not found, languages=['fr', 'es']. And if priority=gp, the fallback chain will be as follows:

  • use gp French translated value, if not found:
  • use local French translated value, if not found:
  • use gp Spanish translated value, if not found:
  • use local Spanish translated value, if not found:
  • use source value, if not found:
  • use provided key

In order to search for local translated values, the optional parameters must be provided according to gettext.translation

GPServiceAccount

class gpclient.gpserviceaccount.GPServiceAccount(url=None, instanceId=None, userId=None, password=None, serviceInstanceName=None)

Holds authentication details for connecting to the Globalization Pipeline (GP) service instance.

There are three options for creating a GPServiceAccount instance:

1. Provide the url, instanceId, userId and password as parameters

2. Use the user defined environment variables, GPServiceAccount.GP_URL_ENV_VAR, GPServiceAccount.GP_INSTANCE_ID_ENV_VAR, GPServiceAccount.GP_USER_ID_ENV_VAR, and GPServiceAccount.GP_PASSWORD_ENV_VAR (no params required)

3. Search the VCAP_SERVICES environment variable for all avaliable GP service instances. If a service instance name is provided, it will be given precedence. (optional serviceInstanceName param)

GP_INSTANCE_ID_ENV_VAR = 'GP_INSTANCE_ID'
GP_PASSWORD_ENV_VAR = 'GP_PASSWORD'
GP_URL_ENV_VAR = 'GP_URL'
GP_USER_ID_ENV_VAR = 'GP_USER_ID'
get_instance_id()

Return the instanceId being used by this GPServiceAccount

get_password()

Return the password being used by this GPServiceAccount

get_url()

Return the url being used by this GPServiceAccount

get_user_id()

Return the userId being used by this GPServiceAccount

GPTranslations

class gpclient.gptranslations.GPTranslations(client, bundleId, languageId, cacheTimeout, fp=None)

Bases: gettext.NullTranslations

GPTranslations extends gettext.NullTranslations and performs a similar function to gettext.GNUTranslations ; however, instead of using the translations in local mo files, it uses those provided by Globalization Pipeline (GP).

NOTE: It is recommended that the GPTranslations constructor not be used directly - instead GPClient.translation or GPClient.gp_translation should be used, which will create and return a GPTranslations instance.

gettext(message)

Contacts the GP service instance to find the translated value for the provided message key.

If translated value is not available, and a fallback has been set, the fallback will be used. If there is no fallback, the source language value is returned. And if the message key is not found, the message key itself will be returned.

The caching feature may be used to cache translated values locally in order to reduce the number of calls made to the GP service.

  • cacheTimeout = 0, do not cache
  • cacheTimeout = -1, cache forever
  • cacheTimeout > 0, store cache value for specified number of minutes

Table Of Contents

Previous topic

gp-python-client

This Page