安卓多语言支持的代码

4

我需要开发一个可以同时支持英文和日语的应用程序。

我只需要为我的应用程序更改语言,而其他所有手机应用程序都应该使用默认语言。

请帮我提供一些在Android 2.2中使用的代码片段/ API。


你的意思是什么?你希望你的应用程序只支持日语和英语吗?还是你需要在应用中能够切换英语和日语? - Jason Rogers
2个回答

5

本地化是你需要的!

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:

  1. res/values/strings.xml Contains English text for all the strings that the application uses, including text for a string named title.
  2. res/values-fr/strings.xml Contain French text for all the strings, including title.
  3. 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);

但是本地化应该改变手机语言,而我只需要更改特定应用程序的语言。 - ruby
@ruby:所以真正的问题不是本地化您的应用程序,而是仅在应用程序范围内更改语言环境而不是手机范围内? - RoflcoptrException
准确地说,我正在开发一个语音识别应用程序,用户可以选择任何语言,如英语和日语,然后以该语言发出语音命令:例如:如果我在英语中说“Mail”,我的应用程序应该打开我的收件箱,并且如果其他用户说日语,则应接受该请求。 - ruby

0

如果您想要添加一个简单的英文和日文下拉菜单,请使用以下代码。不要忘记在特定的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());

并且对于日本人来说,将字符串 languageToLoad 替换为日语代码 (ja-jp)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接