Software localization
Android Studio Tutorial on Internationalizing Android Apps
Want to unlock the global potential of your Android app? This basic Android Studio tutorial will give you an overview of how to get started with Android localization and adapt your app for an international user base.
What is Internationalization in Android Studio?
Before we move on to the code, we must understand what the internationalization of an Android app entails.
In an Android OS-based mobile device, users have a locale set, based on their location and their preferences. Whenever an application starts, that application can decide to respect the preference set by the user and show all the text in the app based on their set language.
Internationalizing one's application is a great way to also increase one's user base, as product users don’t feel limited due to the presence of a foreign language that they might not even understand.
Knowing the project
For this tutorial, we will be making an Android application using the Android Studio IDE. The Android Studio IDE will get the user locale, set on the device, and automatically also set the language of the application to be the same. Of course, we will be demonstrating this with only a limited number of languages in this demo, three to be exact: English, Japanese and French.
We will try to keep the project simple and try to build two functionalities that are complementary to each other when it comes to creating an excellent and localized user experience for the end user:
- When a user opens our app, the app will automatically get the device's set locale and set the app's language to be the same locale.
- Inside the application, we will allow the user to change the locale for the application as well. This will change the application's language too.
Setting up the project
Let's make a new project in Android Studio as below:
We named our application "AndroidInternationalization" and it will be supporting three languages.
Managing Dependencies
In Android, dependencies are managed using the Gradle build tool. When we make a project using Android Studio, it automatically adds the dependencies it needs to set up a project.
For the sake of keeping this guide simple - you might also have different versions of dependencies and SDK versions - here is our app level build.gradle file for this project:
apply plugin: 'com.android.application' android { compileSdkVersion 26 defaultConfig { applicationId "com.phraseapp.androidinternationalization" minSdkVersion 16 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' }
We also have a root level dependency build.gradle file and here are the contents of the file:
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
This what it looks like when using Android Studio Version 3.0.1, which is what we used for this tutorial.
Making multiple String resource files
To give our localization process a kick start for the three supported languages, we will create some new folders and files in the res folder in our project structure. The file structure now looks as follows:
See how there are three, similarly named folders for values:
- the values directory is for default language, i.e. English
- the values-fr directory is for the French language
- the values-ja directory is for the Japanese language
We have also added the same file (strings.xml) in all the three folders.
Trying the application
For now, we will run our application multiple times and will only change the device's locale so that app can show changes in the locale:
Of course, the default locale shown above reflects the English language being used. Let's change this now to Japanese:
Finally, let's try the French locale:
This was the simplest demonstration of internationalization support of our app. The best thing about this was that we actually didn't have to do any configuration for this in our app or on our device. The application just picked the locale from the device and used that language in the app as well.
One thing to note here is if the Android cannot find a language locale's file or just a particular String in the locale-specific file, it will switch either completely or just for that specific String to the default English Strings file. This enables us to show the user something rather than nothing at all!
Translating between text
If you want to perform some dynamic localization in your project and not through the Strings resource file, you have two options. We will explain these options along with the use cases:
- Returning language specific data from the server: In this case, you know what data you are returning from the Server. In the request object from the device to the server, you can include meta-data about the user's language. Now, it is the responsibility of the server to return the response in a correct language. This is very much flexible but only applicable when your server know what data to return,
- Using a runtime API for the translation purposes: It is also possible to use Translation APIs which translate the text at the go. This approach can be used in apps like conversational apps where Users can chat with each other. When a user sends a message in, say French and user at the other end understands German, we can use the API to convert the text to German and then pass it to the receiving end.
Conclusion
This Android Studio tutorial guided you through the internationalization process of an Android-based app. We saw how easy it is to integrate Internationalization in Android.
Internationalization is a great way to increase users on a product so there are no limits in terms of how users use your product.
However, writing code to localize your app is one task, but working with translations comes with a whole new set of challenges. Fortunately, Phrase can make your life as a developer easier! Feel free to learn more about Phrase, referring to the Phrase Localization Platform.
If you're interested in the topic of i18n in the Backend using Spring Boot, make sure to check out our other article which also takes a closer look at spring boot localization.