Change language Programmatically Using Kotlin

Tram Ho

By default, Android will attempt to load resources based on the System Language located on the user’s phone. Therefore, if a Tamil language user, Kavi, with my Android device set to Tamil, opens my Android app on the phone, I will see a localized app (auto translate) to your language.

But what if another user wants to use the Tamil language for his Android app on Android whose default language is set to English?

To resolve this issue, we will have to update according to our Android app’s language program to override the default language set in the user’s system. Here we will ask the user to select the language and switch only the language within the app.

Let’s start with creating a new resource file for the Tamil language using the Android Studio resource file creation wizard. First, right click on the res folder and choose “New -> Android resource file”: It will display a message like the filename of this type is string.xml, and choose the spend of resource type as “Locale”

Here you will select the language,

It will generate XML file for you then you can put the sample string as below. Below is the default string.xml for the English language:

Similarly, you will create a new file string.xml for Tamil language:

That’s it, we have configured the language with as many string files as we want according to the language we want.

Now, let’s create a utility class ContextUtils to hold our language update functions. Put this in the utils package on android app, like so:

In the above code,

  • Filed (field) configuration.locale has been deprecated from API level 24 (Nougat) onwards. This was introduced by the Android API developers to make programmers switch to using getters and setters instead of directly accessing variables.
  • This is the preferred way of setting up the language (instead of using a direct accessor or setLocale (java.util.Locale)) starting from API 24.
  • Before API 24, developers could directly access configuration.locale field to change it at will.

We can then use this (func) method to apply language changes.

Next, Create BaseActivity extend from AppCompatActivity and it must be inherited by other activities.

We will use the attachBaseContext override method to update the language configuration to ACTIVITY so that it will reflect on all other extended activities.

Question : What is the use of AttachBaseContext?

Anwser : The attachBaseContext method of the ContextWrapper class ensures that the context is only attached once. ContextThemeWrapper applies a theme from Application or Activity defined in the android: theme tag in the AndroidManifest.xml file. Since both Application and Service do not need a theme, they inherit it directly from ContextWrapper. During activity creation, Application and Service are instantiated, a new ContextImpl object is created each time and it executes functions in Context .

Example: LanguageChooseActivityMainActvity

Here we store inside SharedPreference a string containing language codes like tamil → “ta”, English → “en”, Hindi → “hi”, etc.

This code is used to specify Language by Language (“code-language”)

Once applied in the base activity, wherever the string strings are used will be automatically translated.

Below is an example of an activity extending from BaseActivity ().

Just extend BaseActivity is enough and you’ll see the magic.

Thanks for reading!


Share the news now

Source : Viblo