Performing Text Translation

A guide to using Slang's text translation service

The Slang Retail Assistant provides a text translation service that can be used by apps for translating textual phrases from Indian languages (Hindi and Kannada supported at this time) to English. This is primarily expected to be used by retail apps whose catalog is based on English, but would like to allow their users to search for items in Indian languages.

The steps required to integrate the Assistant's text translation service are:

1. Set up the Assistant

First, you need to set up the Assistant and obtain the necessary credentials. You can find instructions for setting up the Assistant here.

2. Configure the build system

Next, you need to configure the app's build system to ensure it includes a dependency to Slang's Retail Assistant SDK. Here's how to do it:

Android
iOS
Android
# Add this to your top level gradle file
allprojects {
repositories {
maven { url "http://maven.slanglabs.in:8080/artifactory/gradle-release" }
}
}
# Add this to your app's gradle file
dependencies {
implementation 'in.slanglabs.assistants:slang-retail-assistant:4.0.1'
}
iOS
// Swift Package Manger
// --------------------
Add https://github.com/SlangLabs/slang-retail-assistant-ios as a Swift Package
// CocoaPods
// ------------
// Add the following to your Podfile
source 'https://github.com/SlangLabs/cocoapod-specs.git'
target 'TARGET_NAME' do
use_frameworks!
pod 'SlangRetailAssistant', '~> 4.0.0'
end

3. Integrate

Once the dependencies are configured correct, you can move on to integrating the Assistant with your app in order to use the translation service. We look at how to perform the integration and calls to the translation service below

3.1 Initialize

Before the translation service can be used, the Assistant needs to be initialized by passing the credentials obtained during the first step.

Android
iOS
Android
// Your application class
protected void onCreate(Bundle savedInstance) {
...
AssistantConfiguration configuration = new AssistantConfiguration.Builder()
.setAPIKey(<API Key>)
.setAssistantId(<AssistantId>)
.setEnvironment(STAGING) // Change this to PRODUCTION once you've published the Assistant to production environment
.build();
SlangRetailAssistant.initialize(this, configuration);
}
iOS
import slang_retail_assistant
SlangRetailAssistant.initialize(
with: "<AssistantId>",
apiKey: "<APIKey>",
environment: .staging // Change this to .production once you've published
// the Assistant to production environment
)

3.2 Call the Translate API

After the Assistant has been initialized successfully, you can jump right into calling the translation API by providing the correct input text to be translated and consuming the results of the translation call.

Android
iOS
Android
SlangRetailAssistant.translateText(srcText, srcLocale, tgtLocale, new AssistantTranslateAction () {
@Override
void onTranslationSuccess(String translatedText, String srcText, Locale srcLocale, Locale tgtLocale) {
// handle successful translation here
}
@Override
void onTranslationFailure(String srcText, Locale srcLocale, Locale tgtLocale, AssistantTranslationError translationError) {
// handle translation failure here
}
});
iOS
SlangRetailAssistant.translateText(for: srcText, sourceLocale: srcLocale, targetLocale: tgtLocale) {
(translatedText, translationError) in
if translationError != nil {
// handle translation error here
return
}
if let translatedText = translatedText {
// handle successful translation here
}
}

Parameters

  • srcText: The source text that needs to be translated

  • srcLocale: The locale corresponding to the source text

  • tgtLocale: The locale to which the source text needs to be translated into

  • action/completion handler: The call-back that will be called by the Assistant when the translation either succeeds or fails. When the translation succeds, a parameter called translatedText is passed to the handler, which contains the target/translated text. When the translation fails, a parameter called translationError is passed to the handler, which describes the cause for the error

Sample Usage

Android
iOS
Android
SlangRetailAssistant.translateText(
"टमाटर",
new Locale("hi", "IN"),
new Locale("en","IN"),
new AssistantTranslateAction() {
@Override
public void onTranslationSuccess(String translatedText, String srcText,
Locale srcLocale, Locale tgtLocale) {
Log.d(TAG, "translationSuccess: src: %s, tgt: %s", srcText, translatedText);
targetText.setText(translatedText);
}
@Override
public void onTranslationFailure(String srcText, Locale srcLocale,
Locale tgtLocale,
AssistantTranslationError translationError) {
Log.d(TAG, "translationError: %s", translationError.getDescription());
targetText.setText(translationError.getDescription());
}
}
);
iOS
let locale: Locale = Locale.init(identifier: "hi-IN")
SlangRetailAssistant.translateText(
for: "टमाटर",
sourceLocale: locale,
targetLocale: Locale(identifier: "en-IN")) {
(translatedText, translationError) in
if translationError != nil {
print(translationError)
return
}
if let translatedText = translatedText {
print(translatedText)
}
}