This is a Cordova plugin and sample code for the Globalization Pipeline Bluemix service. The IBM Globalization Pipeline makes it easy for you to provide your global customers with Bluemix applications translated into the languages in which they work.
Adding this plugin into your cordova-based project, your application can dynamically request translations of your application content from the IBM Globalization Pipeline.
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 Bluemix, create a new service instance, create a new bundle, and access the translated messages.
Switch workspace to working cordova project
Add this plugin and dependency plugin into working project
cordova plugin add cordova-plugin-file
cordova plugin add cordova-plugin-igp
Initialize a client
var args = {
uri: 'https://gp-dev-rest.bluemix.net/translate/rest',
username: '1234',
password: '1234',
expireAfter: 3600 //time unit is second, 3600 means 1 hour
}
var client = igp.getClient(args);
cordova-plugin-igp
is added, a global object called igp
is exposed to main program.expireAfter
is the cache expiration duration.Client
object is used to communicate with IBM Globalization Pipeline deployed on IBM BlueMix to get resource data.Get translation resource data from IBM Globalization Pipeline
var args1 = { instanceId: '001', bundleId: 'test2',
langId: 'de', srcPath: 'www/res/12345.txt'};
function successCB(result){
//get the string format of object result and show it in main page.
document.getElementById("loadTranslation").innerHTML = JSON.stringify(result);
}
function failureCB(err){
console.log(err.message)
}
client.getTranslation(args1, successCB, failureCB);
APIs which take two callbacks use this pattern:
obj.function( { /*params*/ } , function successCallBack(err), function failureCallBack(result))
err
: if truthy, indicates an error has occured.result
: result object returned if function executes successfully.(optional)All language identifiers are IETF BCP47 codes.
Author: Jian Jun Wang
Reviewer: Steven Atkin, Terry Tong
var args = {
uri: 'https://gp-dev-rest.bluemix.net/translate/rest',
username: '1234',
password: '1234',
expireAfter: 3600
}
var client = igp.getClient(args);
igp
is a global object available to use once this plugin is included into a cordova project.
Description: This function returns an array containing all target language(s).
Params
object
- optional. Default source language is en
err
objectDescription: Do we have access to the server?
Params
err
objectDescription
Params
object
{instanceId: ..., bundleId: ..., langId: ..., srcPath: ...}ResouceData
objecterr
objectsrcPath
, the absolute path of mobile systemDescription: Plug-in checks if the resource data in plugin database expires. (The expiration duration is configured by the developer.). If cache expires, Plug-in will access the IBM Globalization Pipeline on bluemix to synchronize the resource data in the local storage with the server.
Params
object
{instanceId: ..., bundleId: ..., langId: ...}ResouceData
objecterr
objectDescription: Plug-in loads resource data from its local storage directly.
Params
object
{instanceId: ..., bundleId: ..., langId: ...}ResouceData
objecterr
objectDescription: This class contains the resource data from IBM Globalization Pipeline.
See CONTRIBUTING.md.
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.