blog

Home / DeveloperSection / Blogs / Android support for different Languages

Android support for different Languages

Anonymous User 3850 11-Nov-2014

Android device comes in many shapes and size all over the world. With a vast wide range of device types .In order to be as successful as possible our app need to be compatible with different device configurations. Some of the important variations that we must consider includes different languages, screen sizes, and versions of the Android platform.

Here we are going to know  how to use basic platform features that leverage alternative resources and features so our app provide an optimized user experience on variety of Android-compatible device , using a single application package(APK)

Thumb Rule for Strings:  The best practice is to extract the UI strings from the app code and keep them in an external file. Android makes this easy with a resource directory in each Android Project.

If you created your project using the Android SDK Tools, the tools create a res/ directory in the top level of the project. Within this res/ directory are subdirectories for the various resource types. There are also a few default files such as res/values/strings.xml which holds our string values 


Create Local Directories and String Files:

To add support for more languages, create additional values directories inside res/ that include a hyphen and the ISO-language code at the end of the directory name.  For example, values-es/  is the directory containing simple resources for the locales with the language code. Android loads the appropriate resources according to the locale settings of the device  at the runtime.

First chose the language for which we want support, create the resource sub-directories and string resource file.

MyProject/
        res/
            values/
                      strings.xml
            values-es/
                      strings.xml
            values-fr/
                      strings.xml

 

Add the string values for each locale into the appropriate file.

At runtime, the Android systems uses the appropriate set of string resources based on the locale currently set for the user’s device.

Have look on some of the string resource examples in different languages.

English (default locale) /values/strings.xml:

<?xml version =“1.0” encoding=”utf-8”>

<resources>

      <string name=”title”>My First App </string>

      <string name=”hello_world”>Hello World! </string>

</resources>

 

French,/values-fr/strings.xml :

 

<?xml version =“1.0” encoding=”utf-8”>

<resources>

      <string name=”title”>Ma premiere App </string>

      <string name=”hello_world”>Bonjur le monde! </string>

</resources>

 

Spanish,/values-fes/strings.xml :

 

<?xml version =“1.0” encoding=”utf-8”>

<resources>

      <string name=”title”>Mi primera App </string>

      <string name=”hello_world”> Hola Monda! </string>

</resources>

  

Use the String Resources

We can refer our string resource in our source code and other XML files using the resource name defined by the <string> element’s name attribute.

 

In our source code we can refer to a string resource with syntax R.string.<string_name>. There are a variety of methods that accept a string resource this way

 

// Get a string resource from app’s Resources

String hello = getResource().getString(R.string.hello_world);

 

// Supply a string resource to a method that requires a string

TextView textView = new TextView(this);

textView.setText(R.string.hello_world);

 

We can also refer string in XML files, with the syntax @string/<string_name> whenever the XML attributes accepts a string value.

 

 

<TextView

      android: layout_width=”wrap_content”

      android: layout_height=”wrap_content”

      android: text=”@string/hello_world” />

 

This post is referred from Google docs Supporting Different Languages


Updated 11-Nov-2014
I am a content writter !

Leave Comment

Comments

Liked By