How to add multiple language support in my android app — Static Text👨‍💻

Divyanshutw
6 min readJan 27, 2021

We live in a world with diverse languages. India only has 22 primary languages. That’s why we see that most of the global apps and the local Indian apps have multi language support. There are two types of texts that we show in our app depending on their origin:

📱Static : It includes all the text that is static, i.e., the text is written inside the app as a string. This includes all the text in the xml files, class files, etc.

🌐Dynamic : It includes all the text that is dynamically fetched from a remote database through firebase, some API, etc.

Both of the strings are dealt in a different manner. In this part, we’ll talk about just the static text. There are different ways to do this. The one you’ll need depends on you requirements but I’ll cover them all.

⚠️THIS ARTICLE IS BEGINNER FRIENDLY

CASE 1 :- If you want your app to change language depending on the user’s default language, then this case is for you. This task is pretty simple and includes following steps:

  1. Place all the texts in the strings.xml file.
  2. Add locale(s) in the translation editor.
  3. Convert strings.xml into an excel file.
  4. Translate all the text fields using google translate.
  5. Convert the excel file back into strings.xml file.

Now lets see each of these in detail :

Step 1️⃣ : Place all the texts in the strings.xml file : One should always keep all the texts in the strings.xml file. You can find it here in your app.

Android studio offers a simple way to do it. Just click alt+enter(windows/linux) or option+enter(mac) on a string whether inside a xml file or any other file. You’ll get an option saying “Extract string resource”. This will extract string resource for that string. After making a resource, you can access the string using “@string/string_resource_id” in xml file and using “getString(R.string.string_resource_id)” in kotlin class files with context.

Doing this is always a good practice and is an important aspect for making scalable apps.

Step 2️⃣ : Add locale(s) in the translation editor : Go to strings.xml and there you’ll find a notification at the top at the right end of which you’ll find an option saying “Open editor”. Click on it. You may also open Translation editor by right clicking on strings.xml from the android panel and selecting the “Open Translation Editor” in the bottom.

Now click on the button as shown and choose the locale or the language you want to make available.

Step 3️⃣ : Convert strings.xml into an excel file : You can do this using this website. There might be some discrepancies if you have large number of keys, so its recommended to have a look on the excel file once.

Step 4️⃣ : Translate all the text fields using google translate : Now make a copy of this excel file and delete the column containing the text in English. Copy all the texts from the original excel file in English and translate it into desired language using this. Copy the converted texts and paste into the empty column in the new excel file.

Note : For untranslatable strings, you may copy the same text as in original excel file and paste it back in the new one as it is without translating. You may also do this in the Translation Editor by checking the Untranslatable check box.

Step 5️⃣ :Convert the new excel file back into the xml file using this. Take care of using the proper xml tags.

When you added a locale, another strings.xml file was created by android studio in the same folder in which the original strings.xml was there as shown here.

Go to this new strings.xml file and paste the content of your strings.xml that you created from the excel file after translation.

Just that……And when you change the language from your phone’s settings, the app language will also change.🥳

CASE 2 :- You might have seen that when you install an app, it asks you to set a language for your app. If you want to implement this, then this case is for you.

Firstly, you’ll have to do everything as in CASE 1 and then follow these steps :

  1. Add a screen to ask the user to set language.
  2. Store the current language in the SharedPreferences.
  3. Change the locale of your app.
  4. Add the ChangeLanguage class to your app.
  5. Restart the activity to see the changes.

Now lets see each of these in detail :

Step 1️⃣ : Add a screen to ask the user to set language : This is totally on your choice on how you do it. You can use a ListView, RadioGroup or anything else you like. I have used RadioGroup here:

Step 2️⃣ : Store the current language in the SharedPreferences : Make a SharedPreferences and store the current language inside it.

var preferences: SharedPreferences? = null
var editor: SharedPreferences.Editor? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

preferences = getSharedPreferences("LANGUAGE", Context.MODE_PRIVATE)
editor= preferences!!.edit()

val currentLanguage=preferences!!.getString("language", "English")
if(currentLanguage=="English")
findViewById<RadioButton>(R.id.radioButton_english).isChecked=true
else
findViewById<RadioButton>(R.id.radioButton_hindi).isChecked=true

findViewById<Button>(R.id.button_ok).setOnClickListener {
val selectedId=findViewById<RadioGroup>(R.id.radioGroup).checkedRadioButtonId
when(selectedId){
R.id.radioButton_english -> {
setLanguage("English", "en")
}
R.id.radioButton_hindi -> {
setLanguage("Hindi", "hi")
}
}
}

findViewById<Button>(R.id.button_test).setOnClickListener {
startActivity(Intent(this,TextActivity::class.java))
}
}

Step 3️⃣ : Change the locale of your app : You can change locale programmatically as follows :

private fun setLanguage(language: String, languageCode: String) {
editor?.putString("language", language)
editor?.commit()
val locale = Locale(languageCode)
Locale.setDefault(locale)
val config = Configuration()
config.locale = locale
resources.updateConfiguration(config, resources.displayMetrics)

val intent = Intent(this, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
startActivity(intent)
Toast.makeText(this, "Language changed", Toast.LENGTH_LONG).show()
finish()
}

Step 4️⃣ : Add the ChangeLanguage class to your app : This will update the locale of your app. But the problem is that the app won’t remember what locale was set once you move to a different page or close the activity.

To solve this problem, make a new class ChangeLanguage.kt and add the following code in it :

import android.content.Context
import android.content.SharedPreferences
import android.content.res.Configuration
import java.util.*

class ChangeLanguage(private val context: Context) {
var preferences: SharedPreferences? = null
var editor: SharedPreferences.Editor? = null
fun setLanguage() {
preferences = context.getSharedPreferences("LANGUAGE", Context.MODE_PRIVATE)
editor= preferences!!.edit()

var language="English"
var languageCode="en"
var currentLanguage="English"
if(preferences!!.contains("language"))
{
currentLanguage=preferences!!.getString("language", "English")!!
if(currentLanguage=="English")
{}
else
{language="Hindi";languageCode="hi"}
}

editor?.putString("language", language)
editor?.commit()

val locale = Locale(languageCode)
Locale.setDefault(locale)
val config = Configuration()
config.locale = locale
context.resources.updateConfiguration(config, context.resources.displayMetrics)
}

fun getLanguage():String
{
val preferences: SharedPreferences=context.getSharedPreferences("LANGUAGE", Context.MODE_PRIVATE)
var language="english"
if(preferences.contains("language"))
language=preferences.getString("language","English")!!.toLowerCase()
return language
}
}

Now you’ll have to call this class before inflating any activity or fragment.

ChangeLanguage(this).setLanguage()
setContentView(R.layout.activity_text)

Note that you’ll have to call the class before setting the layout in case of activity.

Step 5️⃣ : After doing all this you won’t see the changes in your current activity if you don’t restart it.

Just that and congratulations you have successfully added multi language support to your app with static text. 🥳

For full code, see this repositoryhttps://github.com/divyanshutw/Multi-Language-Static

Githubhttps://github.com/divyanshutw

LinkedInhttps://www.linkedin.com/in/divyanshu-tiwari-7a7318173

I’ll explain how to deal with dynamic text in my next article. So stay tuned. 🙃🙃

--

--

Divyanshutw

👨‍💻 Android Developer||Java is ❤||Fond of Problem Solving||SSB Recommended