Skip to content

IBM-Cloud/gp-ios-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Globalization Pipeline Client SDK for iOS

What is this?

This is a iOS framework 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 framework into your Swift or Obj-C project, your application can dynamically request translations of your application content from the IBM Globalization Pipeline.

Note that this framework implements HTTP Basic authentication, which means that a “reader” credential must be used. Other credential classes will be rejected by the server.

Build Status

See this blog post for a tutorial on using this SDK.

Getting started

Set up the Globalization Pipeline service

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.

Use the included test

  1. create a Globalization Pipeline instance using the Quick Start Guide documentation.

  2. Create a bundle named uistrings and upload the file uistrings_en.json as the English source content. Choose the target language(s) you wish to translate to.

  3. Create a new Reader user with access to the uistrings bundle. Save the url, userId, password, and instanceId parameters

  4. Copy the ReaderCredentials-SAMPLE.swift source file to ReaderCredentials.swift (in the same directory)

  5. Modify the TestFramework/TestSDK/TestSDK/ReaderCredentials.swift source file to include these credentials.

  6. Now, you should be able to build and run the application from XCode. The translated language (from the Settings) will be used for display.

Sample Output

Using with your own project

Manual

  1. Clone or download the SDK repository locally
  2. Drag the GPSDK/GPSDK.xcodeproj file into your own XCode workspace
  3. Change the Scheme to GPSDK and build it in your own XCode workspace
  4. Change the Scheme back to your application's Scheme. Verify that build works.

Carthage

  1. Install Carthage
  2. add/create a Cartfile containing the following
     github "IBM-Bluemix/gp-ios-client"
  1. Run carthage update
  2. You should now be able to add Carthage/Build/iOS/GPSDK.framework to your own project’s linked frameworks.
  3. See the Carthage documentation for more detail. In particular, you will need to add or update a copy-frameworks step with the following input file:
     $(SRCROOT)/Carthage/Build/iOS/GPSDK.framework

Sample Code

Note: the first four parameters to initService() are your Globalization Pipeline reader credentials, see the Quick Start Guide

let service = GPService()
        
do {
	try service.initService(
			url:          "(Your URL)", 
			userId:       "(your userId)", 
			password:     "(your password)", 
			instanceId:   "(your instance ID)", 

			bundleId: "uistrings", 
			languageId:nil, 
			alwaysLoadFromServer: false, 
			expireAfter: 0)
} catch GPService.GPError.languageNotSupported {
        print("This language is not supported...")
} catch GPService.GPError.requestServerError(let errorDescription) {
        print("Request server error: " + errorDescription)
} catch GPService.GPError.HTTPError(let statusCode) {
        print("Request server error: HTTP \(statusCode)")
} catch {
}

label1.text = service.localizedString("Key1", nil)

label2.text = service.localizedString("Key2", "Hello world")

API reference

  • GPService

    • initService

      • ######Declaration func initService(url:String!, instanceId:String!, bundleId: String!, userId:String!, password:String!, languageId:String?, alwaysLoadFromServer:Bool!, expireAfter:Int!) throws

      • ######Descrption Initialize the arguments information and load the useful information in the initial stage. If the information from the server has been loaded successfully, no exception will be thrown. Else, the function will throw corresponding exceptions to the outer function. About the errors, please refer to GPService.GPError.

      • ######Parameters

        • languageId: This parameter can be set as nil. If it set as nil, system default language will be used.
        • alwaysLoadFromServer: If this parameter is set as true, always load result from GP server. Else, if the translation result has been cached, it will load the result from the cache. If no cache has been hit, it will load the translation result from GP server if the cache is not expired.
        • expireAffer: Calculated by hours. This argument will be used to judge if the cache is expired. If the cache is existed more than assigned expired time, it will request the result directly from the GP server.
    • localizedString

      • ######Declaration func localizedString(_ key:String, _ comment:String?) -> String

      • ######Descrption Load the value of the corresponding key in the bundle.

      • ######Parameters

        • key: The key of your bundle.
        • comment: Comment for the key.
    • localizations

      • ######Declaration func localizations(_ bundleId:String) throws -> [String]?

      • ######Descrption Get GP supported translation languages for each bundle.

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