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:
First, you need to set up the Assistant and obtain the necessary credentials. You can find instructions for setting up the Assistant here.
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:
# Add this to your top level gradle fileallprojects {repositories {…maven { url "http://maven.slanglabs.in:8080/artifactory/gradle-release" }}}# Add this to your app's gradle filedependencies {…implementation 'in.slanglabs.assistants:slang-retail-assistant:4.0.1'}
// Swift Package Manger// --------------------Add https://github.com/SlangLabs/slang-retail-assistant-ios as a Swift Package// CocoaPods// ------------// Add the following to your Podfilesource 'https://github.com/SlangLabs/cocoapod-specs.git'target 'TARGET_NAME' douse_frameworks!pod 'SlangRetailAssistant', '~> 4.0.0'end
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
Before the translation service can be used, the Assistant needs to be initialized by passing the credentials obtained during the first step.
// Your application classprotected 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);}
import slang_retail_assistantSlangRetailAssistant.initialize(with: "<AssistantId>",apiKey: "<APIKey>",environment: .staging // Change this to .production once you've published// the Assistant to production environment)
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.
SlangRetailAssistant.translateText(srcText, srcLocale, tgtLocale, new AssistantTranslateAction () {@Overridevoid onTranslationSuccess(String translatedText, String srcText, Locale srcLocale, Locale tgtLocale) {// handle successful translation here}@Overridevoid onTranslationFailure(String srcText, Locale srcLocale, Locale tgtLocale, AssistantTranslationError translationError) {// handle translation failure here}});
SlangRetailAssistant.translateText(for: srcText, sourceLocale: srcLocale, targetLocale: tgtLocale) {(translatedText, translationError) inif translationError != nil {// handle translation error herereturn}if let translatedText = translatedText {// handle successful translation here}}
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
SlangRetailAssistant.translateText("टमाटर",new Locale("hi", "IN"),new Locale("en","IN"),new AssistantTranslateAction() {@Overridepublic void onTranslationSuccess(String translatedText, String srcText,Locale srcLocale, Locale tgtLocale) {Log.d(TAG, "translationSuccess: src: %s, tgt: %s", srcText, translatedText);targetText.setText(translatedText);}@Overridepublic void onTranslationFailure(String srcText, Locale srcLocale,Locale tgtLocale,AssistantTranslationError translationError) {Log.d(TAG, "translationError: %s", translationError.getDescription());targetText.setText(translationError.getDescription());}});
let locale: Locale = Locale.init(identifier: "hi-IN")SlangRetailAssistant.translateText(for: "टमाटर",sourceLocale: locale,targetLocale: Locale(identifier: "en-IN")) {(translatedText, translationError) inif translationError != nil {print(translationError)return}if let translatedText = translatedText {print(translatedText)}}