SUSI.AI has many skills. Some of which are displaying web search of a certain query, provide a list of relevant information of a topic, displaying a map of the certain position and simple text message of any query. Previously SUSI.AI reply all query in English language but now one important feature is added in SUSI.AI and that feature is, reply query of the user in the language that user wants. But to get the reply in different language user has to send language code of that language along with query to SUSI Server. In this blog post, I will show you how it is implemented in SUSI Android app.
Different language supported in SUSI Android
Currently, different languages added in option in SUSI Android and their language code are:
Language | Language Code |
English | en |
German | de |
Spanish | es |
French | fr |
Italian | it |
Default | Default language of the device. |
Layout design
I added an option for choosing the different language in settings. When the user clicks on Language option a dialog box pops up. I used listpreference to show the list of different languages.
<ListPreference
android:title=“@string/Language” android:key=“Lang_Select” android:entries=“@array/languagentries” android:entryValues=“@array/languagentry”> </ListPreference> |
“title” is used to show the title of setting, “entries” is used to show the list of entry to the user and “entryValue” is the value corresponding to each entry. I used listpreference because it has own UI so we don‘t have to develop our own UI for it and also it stores the string into the SharedPreferences so we don’t need to manage the values in SharedPreference. SharedPreference needed to set value in Language in settings so that once user close app and again open it setting will show same value otherwise it will show default value. We used an array of string to show the list of languages.
<string-array name=“languagentries”>
<item>Default</item> <item>English</item> <item>German</item> <item>Spanish</item> <item>French</item> <item>Italian</item> </string-array> |
SetLanguage implementation
To set language user must choose Language option in setting.
On clicking Language option a dialog box pop up with the list of different languages. When the user chooses a language then we save corresponding language code in preference with key “prefLanguage” so that we can use it later to send it to the server along with the query. It also uses to send language to the server to store user language on the server, so that user can use the same language in the different client.
querylanguage.setOnPreferenceChangeListener { _, newValue ->
PrefManager.putString(Constant.LANGUAGE, newValue.toString()) if(!settingsPresenter.getAnonymity()) { settingsPresenter.sendSetting(Constant.LANGUAGE, newValue.toString(), 1) } } |
newValue.toString() is the value i.e language code of corresponding language.
Now when we query anything from SUSI.AI we send language code along with query to the server. Default language is default language of the device. Before sending language to the server we check language is default language or any specific language.
val language = if (PrefManager.getString(Constant.LANGUAGE, Constant.DEFAULT).equals(Constant.DEFAULT))
Locale.getDefault().language else PrefManager.getString(Constant.LANGUAGE, Constant.DEFAULT) |
And after that, we send the corresponding language along with query to the server.
clientBuilder.susiApi.getSusiResponse(timezoneOffset, longitude, latitude, source, language, query) |
Reference
- Main site link of listpreference: https://developer.android.com/reference/android/preference/ListPreference.html
- Tutorial on how to use preference: https://www.androidhive.info/2017/07/android-implementing-preferences-settings-screen/
- Github link of SUSI Server: https://github.com/fossasia/susi_server
- Tutorial on how to use listpreference: http://www.edumobile.org/android/listpreference-example/
- Tutorial on sharedpreference: http://www.journaldev.com/9412/android-shared-preferences-example-tutorial
- Tutorial on Kotlin language: https://code.tutsplus.com/tutorials/an-introduction-to-kotlin–cms-24051