Skip to content

Localizing Resources

Joel Goncalves edited this page Dec 23, 2020 · 1 revision

Localizing Resources

Android allows us to include multiple sets of resources such as strings and audio to be used based on device configuration. Therefore, when we change locale the app will automatically switch to the appropriate resources.

All the resources reside in specially named sub-directories within the projects res/ directory (EclipseSoundscapes-Android/app/src/main/res).

Providing Resources

The res/ directory contains default resources which are used when locale-specific ones have not been provided. To prevent errors and ensure the app is consistent across locale changes, a default should always be provided in the appropriate directory or file (see below).

To create an alternative resource for a different locale, a directory within res/ must be created and named using a qualifier that specifies a language in the form <resources_name>-<qualifier> (see example below).

Strings

Default

The directory and file for the app’s default text is res/values/strings.xml. Currently all the texts are specified in English.

Alternative

To localize all the text in the app to another language we add a strings.xml file in a new directory within res/ named with the format values-<qualifier>, where the qualifier is a two-letter ISO 639-1 language code.

In example, if we wanted to localize to Spanish then we would create a values-es directory in res/ and add strings.xml. The full path would then be res/values-es/strings.xml.

Note

The strings.xml is an XML file that contains key-value pairs. In order to successfully override the default text value for a language, the same key must be used and the value updated accordingly. In example, the text “About” is specified in the default strings file (res/values/strings.xml) as:
<string name="about">About</string>
In order to provide the Spanish alternative, we then specify the following entry in the res/values/es/strings.xml file:
<string name="about">Acerca de</string>

Audio

Default

The default directory for all the audio files is res/raw/. All the supported media formats can be found here.

Alternative

To localize an audio file for a particular language, create a new directory within res/ with a name in the format raw-<qualifier>, where the qualifier is a two-letter ISO 639-1 language code.

In example, if we wanted to provide the Spanish alternative to the default audio files then we would create a directory named raw-es and add the audio file. The full path would then be res/raw-es/corona_full.mp3.

Note

The audio files provided for a specific locale must have the same file name as it’s default counterpart.

Other Resources

Localization also extends to other resources such as images and layouts. To provide an alternative based on locale similar steps can be followed as shown above. The essential detail is that these resources are sub-directories within the res/ directory that must be named with a particular format.

Additional information regarding localization can be found in the Android docs here.

Clone this wiki locally