Search…
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
1
# Add this to your top level gradle file
2
3
allprojects {
4
repositories {
5
6
maven { url "http://maven.slanglabs.in:8080/artifactory/gradle-release" }
7
}
8
}
9
10
# Add this to your app's gradle file
11
12
dependencies {
13
14
implementation 'in.slanglabs.assistants:slang-retail-assistant:4.0.1'
15
}
Copied!
1
// Swift Package Manger
2
// --------------------
3
4
Add https://github.com/SlangLabs/slang-retail-assistant-ios as a Swift Package
5
6
// CocoaPods
7
// ------------
8
9
// Add the following to your Podfile
10
11
source 'https://github.com/SlangLabs/cocoapod-specs.git'
12
13
target 'TARGET_NAME' do
14
use_frameworks!
15
16
pod 'SlangRetailAssistant', '~> 4.0.0'
17
end
Copied!

3. Integrate

Once the dependencies are configured correctly, 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
1
// Your application class
2
3
protected void onCreate(Bundle savedInstance) {
4
...
5
AssistantConfiguration configuration = new AssistantConfiguration.Builder()
6
.setAPIKey(<API Key>)
7
.setAssistantId(<AssistantId>)
8
.setEnvironment(STAGING) // Change this to PRODUCTION once you've published the Assistant to production environment
9
.build();
10
SlangRetailAssistant.initialize(this, configuration);
11
}
Copied!
1
import slang_retail_assistant
2
3
SlangRetailAssistant.initialize(
4
with: "<AssistantId>",
5
apiKey: "<APIKey>",
6
environment: .staging // Change this to .production once you've published
7
// the Assistant to production environment
8
)
Copied!

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
1
SlangRetailAssistant.translateText(srcText, srcLocale, tgtLocale, new AssistantTranslateAction () {
2
@Override
3
void onTranslationSuccess(String translatedText, String srcText, Locale srcLocale, Locale tgtLocale) {
4
// handle successful translation here
5
}
6
7
@Override
8
void onTranslationFailure(String srcText, Locale srcLocale, Locale tgtLocale, AssistantTranslationError translationError) {
9
// handle translation failure here
10
}
11
});
12
Copied!
1
SlangRetailAssistant.translateText(for: srcText, sourceLocale: srcLocale, targetLocale: tgtLocale) {
2
(translatedText, translationError) in
3
if translationError != nil {
4
// handle translation error here
5
return
6
}
7
if let translatedText = translatedText {
8
// handle successful translation here
9
}
10
}
Copied!

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 succeeds, 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
1
SlangRetailAssistant.translateText(
2
"टमाटर",
3
new Locale("hi", "IN"),
4
new Locale("en","IN"),
5
new AssistantTranslateAction() {
6
@Override
7
public void onTranslationSuccess(String translatedText, String srcText,
8
Locale srcLocale, Locale tgtLocale) {
9
Log.d(TAG, "translationSuccess: src: %s, tgt: %s", srcText, translatedText);
10
targetText.setText(translatedText);
11
}
12
13
@Override
14
public void onTranslationFailure(String srcText, Locale srcLocale,
15
Locale tgtLocale,
16
AssistantTranslationError translationError) {
17
Log.d(TAG, "translationError: %s", translationError.getDescription());
18
targetText.setText(translationError.getDescription());
19
}
20
}
21
);
22
Copied!
1
let locale: Locale = Locale.init(identifier: "hi-IN")
2
SlangRetailAssistant.translateText(
3
for: "टमाटर",
4
sourceLocale: locale,
5
targetLocale: Locale(identifier: "en-IN")) {
6
(translatedText, translationError) in
7
if translationError != nil {
8
print(translationError)
9
return
10
}
11
if let translatedText = translatedText {
12
print(translatedText)
13
}
14
}
Copied!
Last modified 5mo ago