krishnaparjanya / slang-retail-assistant-ios

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Slang Retail Assistant for iOS

This repository contains releases for the Slang Retail Assistant for iOS SDK. These releases can be installed using Swift Package Manager or CocoaPods as you prefer.

Swift Package Manager

You can add Slang Retail Assistant for iOS by adding the https://github.com/SlangLabs/slang-retail-assistant-ios repository as a Swift Package.

CocoaPods

It's easy to install the Retail Assistant framework if you manage your dependencies using CocoaPods. Simply 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

Then run pod install --verbose to install the dependencies to your project.

Usage Instructions

  • Import SlangRetailAssistant
import slang_retail_assistant
  • Initialize the SlangRetailAssistant
SlangRetailAssistant.initialize(
  with: "<AssistantId>",
  apiKey: "<APIKey>",
  environment: .staging //Change this to .production once you've published the Assistant to production environment
)
  • Perform the translation operation via the translate API
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)
    }
}

TranslateAPI Declaration :

func translateText(for text: String, 
sourceLocale: Locale, targetLocale: Locale,
completionHandler: @escaping (String?, TranslationError?) -> Void)

TranslateAPI Parameters :

sourceText
The source string that needs to be translated.

sourceLocale
Locale specification of the source string.

targetLocale
The target Locale specification for the translation request.

completionHandler
The completion handler to call when the translate request is complete.

The handler takes the following parameters :

translatedText
The translated text of type string. It will be nil if the translation request has failed.

translationError
A TranslationError object that indicates why the request failed, or nil if the request was successful.

TranslationError :

The translation error object is special type of error object which indicates what is the type of error and its description.

The error object contains the following :

errorDescription
A localized message describing what error occurred.

type
A TranslationErrorType enum that indicates the type of failure.

TranslationErrorType :

The TranslationErrorType is enum which describes the type of error.

The enum contains the following cases :

localeNotSupported
This case signifies that either the source or destination locale were incorrect.
Note :

  • Destination locale language code needs to be en.
  • Source locale language code should not be en.

networkError(_ error: SlangNetworkError)
This case signifies that there was a network error, But additionally it provides another error object SlangNetworkError which specifies what exactly the network was.

SlangNetworkError :

The network error object is special type of error object which indicates what is the type of network error and its description.

The error object contains the following :

errorDescription
A localized message describing what error occurred.

type
A SlangNetworkErrorType enum that indicates the type of failure.

SlangNetworkErrorType :

The SlangNetworkErrorType is enum which describes the type of network error.

The enum contains the following cases :

unauthorized
This case indicates that the current assistant is unauthorized.

serializationFailed
This indicates that an internal searlization of the request data has failed

deserializationFailed
This indicates that an internal deserialization of the response data has failed

networkError
This indicates that there has been an internal network error

Typical Error Parsing Construct

let locale: Locale = Locale.init(identifier: "hi-IN")
SlangRetailAssistant.translateText(
  for: "टमाटर",
  sourceLocale: locale,
  targetLocale: Locale(identifier: "en-IN")) {
  (translatedText, translationError)  in
    if error != nil {
      switch error?.type {
        case .localeNotSupported :
        //localeNotSupported error reported
        break
        case .networkError(let error) :
        //networkError error thrown
        switch error.type {
          case .serializationFailed:
          //serializationFailed networkError error reported
          break
          case .deserializationFailed:
          /deserializationFailed networkError error reported
          break
          case .unauthorized:
          //unauthorized networkError error reported
          break
          case .networkError:
          //internalError networkError error reported
          break
        }
        break
        case .none:
          break
      }
      print(error)
      return
    }
    if let translatedText = translatedText {
        print(translatedText)
    }
}

License

Copyright (c) 2017-2021 Slang Labs Private Limited. All rights reserved.

About


Languages

Language:Swift 100.0%