Code for multilingual support in android
I need to develop an application which works in English and Japanese both开发者_如何学Go.
I need language change for only my application and rest all other phone applications should be in default language.
Please help me with some code snippet / apis to use in android 2.2
Localization is what you need!
A large part of localizing an application is providing alternative text for different languages. In some cases you will also provide alternative graphics, sounds, layouts, and other locale-specific resources.
An application can specify many res// directories, each with different qualifiers. To create an alternative resource for a different locale, you use a qualifier that specifies a language or a language-region combination. (The name of a resource directory must conform to the naming scheme described in Providing Alternative Resources, or else it will not compile.)
Example:
Suppose that your application's default language is English. Suppose also that you want to localize all the text in your application to French, and most of the text in your application (everything except the application's title) to Japanese. In this case, you could create three alternative strings.xml files, each stored in a locale-specific resource directory:
- res/values/strings.xml Contains English text for all the strings that the application uses, including text for a string named title.
- res/values-fr/strings.xml Contain French text for all the strings, including title.
- res/values-ja/strings.xml Contain Japanese text for all the strings except title.
If your Java code refers to R.string.title, here is what will happen at runtime:
* If the device is set to any language other than French, Android
will load title from the res/values/strings.xml file. * If the device is set to French, Android will load title from the res/values-fr/strings.xml file.
Notice that if the device is set to Japanese, Android will look for title in the res/values-ja/strings.xml file. But because no such string is included in that file, Android will fall back to the default, and will load title in English from the res/values/strings.xml file.
To change the localization of your application, you can use the following code snippet:
Resources res = getResources();
Configuration newConfig = new Configuration(res.getConfiguration());
// newConfig.locale = Locale.TRADITIONAL_CHINESE;
newConfig.locale = Locale.ENGLISH;
res.updateConfiguration(newConfig, null);
Just put a simple drop-down for English and Japanese if you select English use the following code.And Don't forget to add the different strings with the particular values folder for eg. values-es
String languageToLoad = "es";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
And for Japnese replace the string languageToLoad with Japnese code as (ja-jp).
精彩评论