4 Tips to Support Language Selection in Android App


Sometimes you may need to support different languages in your application that is independent of the Android system language. The Android framework makes supporting multiple languages simple, but it is dependent on the Android system language. To overcome the limitation of the Android framework language support, you can modify your app’s location information so that the Android framework will use different language resources.

 

 

My goal in this post is to provide you with some ways that you can support dynamic language selection in your app. For reference, I am using Android Studio 3.x, Koltin and Java for the code snippets throughout the post.


 

How Language Selection Works in an App With Android System

When your app launches, the Android framework automatically uses the resources that correspond to the system language. This includes any themes, colors, strings, layouts, menus, and etc. For the entire time that your app is running, the same resources will be used even if the system language does change. You will need to restart your app or reload the activity for the resource change to take effect.

 

I have a post that goes into detail about using the Android resource management framework to prepare Android app for localization. I strongly suggest you look over the information before continuing.

 

Understand Locale and What It Means for Your App

A Locale object represents a specific geographical, political, or cultural region. The Locale contains language information that the Android framework uses to determine what resource to load when your app starts. The moment your app starts the Locale is set to the system language as default, which means resources that match the system languages will be loaded by the Android framework. If there is no match, then the Android framework will use the default resources, which is usually English.

 

fun getSystemDefaultLanguage(): String {
        val system_locale = Locale.getDefault()
        val system_default_language = system_locale.language
       
        return system_default_language
}

 

Modifying Locale to Support Dynamic Language Switching

It is possible to overwrite the Locale language for your application at the start. However, the new language will not take effect until your app restarts or the running Activity restarts. There are many different ways to work around this problem. The one I personally use is to do the Locale language change in a splash screen activity that always starts first thing in my app. That way all the other activities that follow will have the proper language resources.

 

val app_config = Configuration()
val current_language = "en" 	// replace this with actual chosen language; here for example purpose
val chosen_lang_locale = Locale(current_language)
app_config.setLocale(chosen_lang_locale)

this.resources.updateConfiguration(this, null)	// 'this' is the app context; if using outside of activity make sure to get the application context

 

private void
restartSelf() {
	finish();
	Intent self_intent = new Intent(getApplicationContext(), ActivityToRestart.class);
	startActivity(self_intent);
}

 

Maintaining Locale to Support Selected Language

All the modifications that you do to your app’s Locale are lost the moment it closes. Therefore, that means you need a way to keep track of the language in your app. I strongly recommend that you use the shared preference system to keep track of the selected language. The shared preference system allows you to keep track of your app’s setting across every activity. The Android framework manages it so you know it is reliable to use.

 

With the addition of the shared preference system to your app, you can load the chosen language and update the app’s Locale to that language at the start of the app every time.

 

val shared_preference = PreferenceManager.getDefaultSharedPreferences(app_context)
var preference_editor = shared_preference.edit()
preference_editor.putString(CHOSEN_LANG_OPTION_KEY, chosen_language)
preference_editor.apply()

 

val chosen_language = PreferenceManager.getDefaultSharedPreferences(app_context).getString(LANG_OPTION_KEY, "")

 

I hope you found this post helpful. If you found this post helpful, share it with others so they can benefit too.

 

Have you ever found a need to implement in-app language switching? If so, what approach have you taken?

 

To stay in touch, follow me on Twitter, leave a comment, or send me an email at steven@brightdevelopers.com.


About Steven To

Steven To is a software developer that specializes in mobile development with a background in computer engineering. Beyond his passion for software development, he also has an interest in Virtual Reality, Augmented Reality, Artificial Intelligence, Personal Development, and Personal Finance. If he is not writing software, then he is out learning something new.