Supporting different languages and cultures in Android

Tuesday, March 14, 2017 Unknown 0 Comments



Android app supports different languages and cultures. App includes resources that can be specific to a particular culture or locale. For example App can include locale specific string that can be translated to the language of current locale. It is always a good practice to keep culture specific resources.
     You can provide support for different locales by using resource directory. When culture specific resources are used, then resources corresponding to the current locale are loaded on run time.
Create locale specific resource files
create additional resource sub folders in res folder.
Each sub directory should have the following format
<resource type>-b+<language code>[+<country code>]
For example:
MyProject/
    res/
       values/
           strings.xml
       values-b+es/
           strings.xml
       mipmap/
           ic_country_flag.png
       mipmap-b+es+ES/
           ic_country_flag.png
For English locale(English is default locale)
values-es\strings.xml
<resources>
<string name="hello_world">Hello World</string>
</resources>
For Spainish locale
values-es\strings.xml
<resources>
<string name="hello_world">¡Hola Mundo</string>
</resources>
Use this resource in code
TextView txt_hello_world=(TextView)findViewById(R.id.txt_hello_world);
txt_hello_world.setText(getString(R.string.hello_world));
This string resource will be loaded on run time from the strings.xml file corresponding to current locale.
Same thing can be done for any type of resource like drawables etc.
For example locale specific mipmap directories contain locale specific country flag.
It will be used in layout files like this
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_country_flag" />
When called from different locale then ic_country_flag.png is loaded from the mipmap folder corresponding to the current locale.

0 comments: