Use Android’s Resource-Switching Mechanism For Language Localization & More

To begin with, let’s see what are resources in an Android project. All kinds of static data, such as text strings, layouts and pictures, are considered as resources. And indeed, they are very important, without them any application is useless. But resources may vary based on many situations and the device’s configurations. Luckily, handling resources is quite easy with Android’s resource-switching mechanism, that is, the device automatically sets and loads he appropriate resources depending on the configurations.

exit_sortieFor example, the default language for an app is English and the developer also wants to make the app available in French. Doing the translation for the app is as easy as placing another strings.xml file in res/values-fr folder. When a device that is configured in French tries to open the app, Android will detect there is a strings.xml for French locale and will use values from that file.

Important Note:

Default resources must always be made available, otherwise the app will crash if it is launched in another locale. Locale or alternative resources can be a subset of the default resources, but not vice versa. As an example, maybe you want to translate all texts in your app to French except for the app’s title. then, you need not include the title in the strings.xml file for French locale and Android will load the default title together with French text for everything else.

When creating a resource directory for alternative resources, it is important that we follow the naming scheme specified on the android developer’s website for providing alternative resources.

Side Note:

Changing locale on an emulator for Android is possible with the Custom Locale application or by using the adb shell command:

setprop persist.sys.language [language code];setprop [country code];stop;sleep 5;start


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>