Android Native - How to load alternate String resources

dimitrilc 2 Tallied Votes 182 Views Share

Introduction

If your Android app is available to a global audience, you might have wondered how to load localized Strings into your app. This tutorial will teach you how to do just that.

There are quite a few steps involved, but once you understand the concepts, it will make loading other alternate resources easier for you as well, such as drawables or fonts.

Our end goal is to have the app display “Hola Mundo!”, which is the Spanish equivalent of “Hello World!” when the Android device language setting changes to Spanish.

Goals

At the end of the tutorial, you would have learned:

  1. How to load alternate String resources on Android native.

Prerequisite Knowledge

  1. Basic Android development knowledge.
  2. Basic knowledge of the Java class Locale.

Tools Required

  1. Android Studio.

Project Setup

To follow along with the tutorial, perform the steps below:

  1. Create a new Android project with the default Empty Activity.
  2. Our project so far is pretty barebone. Android Studio created an empty TextView for us with “Hello World” as its content.

helloworld.png

  1. It is still a hard-coded String, so let us go ahead and use a String resource under res/values/strings.xml instead.

  2. In strings.xml, add a new <string>

    <string name="hello_world">Hello World</string>
  3. In res/layout/activity.xml, modify the TextView attribute android:text to reference the String resource that we just created previously.

     android:text="@string/hello_world"

Resource Directory and Configuration Qualifier

The first thing that we need to do when adding alternate resources is to create a Resource Directory with a Configuration Qualifier.

To create a Resource Directory, perform the steps below:

  1. Right-click on res -> New -> Android Resource Directory. You will then be presented with the New Resource Directory wizard like the screenshot below.
    New_Resource_Directory.png
  2. Keep the Directory name as “values” for now.
  3. Keep the Resource type as “values”.
  4. Under the Available qualifiers box, Android Studio provides a list of qualifiers. Each of the items in this list is officially called a Configuration Qualifier Name. Android will automatically pick the resource to use if the current Android device settings match the qualifier name. The list is also sorted from highest to lowest precedence for the qualifiers themselves. For example, if there are two resources with one matching the Country Code (retrieved from the device SIM card) and the other resource matching Locale, e.g en-rUS, then the resource matching the Country Code will be loaded because Country Code has higher precedence than Locale.
  5. Because we want to provide alternate String resources in a different Locale, select Locale and then hit the bitwise-signed-right-shift (>>) button.
  6. The window will change a little bit. The list of languages is too long to find our language, so you can highlight any language to put the focus into that box and start typing to find the language you need.
  7. After selecting your language, you might have noticed that the Region list has also been narrowed down to match the common regions for the selected language. Now you can easily select the region you want.
    7.png
  8. Now, if you pay attention to the Directory name field, the name has already been automatically changed for you as well. Android Studio automatically added the Locale configuration qualifier into the resource directory name.
  9. If you are familiar with the Java class Locale, then the “r” might seem odd to you. It is just a requirement on Android to prefix the region with “r”.
  10. Click OK to create the resource directory.

You can also create a Resource Directory without this wizard, just make sure that the language is a two-letter ISO 639-1 language code. The region code is optional. If the region code is added, it must be prefixed with the “r” and uses a two letter ISO 3166-1-alpha-2 region code.

To see the newly created directory, you need to switch the Project view from Android to Project.

project.png

Create the alternate String resource

To create a String resource file for the custom Locale,

  1. Right click on values-es-rMX -> New -> Values Resource File
  2. In the “Enter a new file name” box, type in strings.
  3. Select OK.

We can now see that the new strings.xml file has been created. Android Studio even shows the flag of the region, which will make project navigation a little bit easier.

values.png

You can now switch back to the Android view (we switched to Project view earlier).

Inside the xml file values-es-rMX/strings.xml, add a new string to it, which will translate “Hello World!” to “Hola Mundo!

<string name="hello_world">Hola Mundo!</string>

If we switch back to the default strings.xml file under values/strings.xml, we will see red warning that says:

"app_name" is not translated in "es" (Spanish)

The code will still compile, so you can ignore that warning. There are ways to disable such warnings for the whole project or just a single instance, but that topic is out of scope for this tutorial.

For the alternate String to display, we need to change the device Locale as well.

  1. On the test device, go to Settings > System > Languages & Input > Languages.
  2. Search for Español > Add it to Languages.
  3. Move it on top of English.
    espanol.png
  4. In Android Studio, re-run the app.

We will now see our app displaying “Hola Mundo!”.

hola_mundo.png

Solution Code

values/strings.xml

<resources>
   <string name="app_name">Daniweb Android Alternate String Resource</string>
   <string name="hello_world">Hello World!</string>
</resources>

values-es-rMX/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="hello_world">Hola Mundo!</string>
</resources>

Summary

Congratulations, you have learned how to load alternate String resources on Android. The full project code can be found here: https://github.com/dmitrilc/DaniwebAndroidAlternateStringResource/tree/master

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.