Localization in Android App - Supporting Different Languages
In this blog, we will explore Android Localization, an important part of Android development that prepares your app for interaction with people around the world.
Localization is the process of displaying the text in the phone's default language which could be different based on the country/region and It makes your app more friendly with the local users.
Step 1: Create a New Project
We will start by creating a new Project from scratch, using the "Empty Views Activity" template.
Step 2: View UI Elements
In your activity_main.xml file, we are displaying a string Hello World!
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:textSize="26sp"
android:layout_margin="16dp"
android:id="@+id/hello_world"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The Problem: The "Hello World" string is hardcoded into Textview. This is a bad practice because it makes the app harder to maintain and adapt for multiple languages.
Step 3: Add String Resources
To support localization we need to remove the hardcoded string from XML and provide the string dynamically at runtime.
Inside the res/values directory, you will find a strings.xml file that holds the text strings for the app and makes the app strings dynamic.
<resources>
<string name="app_name" translatable="false">MobileApplicationDevelopment_Labwork
</string>
<string name="hello_world">Hello World</string>
</resources>
Here we have added the hello_world key to your Hello World text, always use the meaning full name to make your app maintainable.
Use the newly created key in the Textview to make your app strings dynamic and remove the hardcoded android:text="Hello World!" and replace it with android:text="@string/hello_world".
<TextView
android:textSize="26sp"
android:layout_margin="16dp"
android:id="@+id/hello_world"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Step 4: Add Localization for Another Language
Let's add support for another language, such as Arabic. Android studio makes localization really easy.
Steps:
Recommended by LinkedIn
Convert Hello World in Arabic and add the translated Hello World to this newly created file against the same hello_world key
This key acts as a placeholder that will be replaced with the actual translation based on the device's default language.
Now, your app supports two languages: English & Arabic.
Test app localization, change your phone language to Arabic, and run the app. You should see "مرحبا بالعالم" rather than Hello World!
Step 5: Change the App's Locale Programmatically
If you need to change the language independently of the device's default language you can do it programmatically. By default, the android loads the strings.xml based on the system language. However, you can override this behavior programmatically to allow users to switch languages within the app.
Create a Utility Class
Create a new Java File ContextUtils.java
public class ContextUtils extends ContextWrapper {
public ContextUtils(Context base) {
super(base);
}
public static ContextUtils updateLocale(Context context, Locale newLocale) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
LocaleList localeList = new LocaleList(newLocale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
Context updatedContext = context.createConfigurationContext(configuration);
return new ContextUtils(updatedContext);
}
}
Use the Utility class in MainActivity
update MainActivity to use the ContextUtils class:
public class MainActivity extends AppCompatActivity {
@Override
protected void attachBaseContext(Context newBase) {
Log.e("TAG", "attachBaseContext: ");
ContextUtils.updateLocale(newBase, Locale.forLanguageTag("ar"));
super.attachBaseContext(newBase);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("TAG", "onCreate: ");
}
}
attachBaseContext method executes before the onCreate Method where we can do this configuration before our activity is created. We are overriding the behavior programmatically and independently of the device's default language.
Step 6: Test Localization
To test localization, run the app on an Android device with the system language set to English, and You should see the Arabic "مرحبا بالعالم" instead of Hello world!
Conclusion:
Android localization is important for making your app interactable to users worldwide. checkout the GitHub repository https://github.com/ifahimkhan/MobileApplicationDevelopment_Labwork/tree/1.0/localization
If you found this blog helpful, please share it with your friends.