我需要开发一个可以同时支持英文和日语的应用程序。
我只需要为我的应用程序更改语言,而其他所有手机应用程序都应该使用默认语言。
请帮我提供一些在Android 2.2中使用的代码片段/ API。
我需要开发一个可以同时支持英文和日语的应用程序。
我只需要为我的应用程序更改语言,而其他所有手机应用程序都应该使用默认语言。
请帮我提供一些在Android 2.2中使用的代码片段/ API。
本地化是你需要的!
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.
要更改应用程序的本地化,您可以使用以下代码片段:
Resources res = getResources();
Configuration newConfig = new Configuration(res.getConfiguration());
// newConfig.locale = Locale.TRADITIONAL_CHINESE;
newConfig.locale = Locale.ENGLISH;
res.updateConfiguration(newConfig, null);
如果您想要添加一个简单的英文和日文下拉菜单,请使用以下代码。不要忘记在特定的values文件夹中添加不同的字符串和值,例如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());