Page tree
Skip to end of metadata
Go to start of metadata

中文标题【支持不同的语言和文化】

Apps include resources that can be specific to a particular culture. For example, an app can include culture-specific strings that are translated to the language of the current locale. It's a good practice to keep culture-specific resources separated from the rest of your app. Android resolves language- and culture-specific resources based on the system locale setting. You can provide support for different locales by using the resources directory in your Android project.

本页中的内容:

相关页面:

You can specify resources tailored to the culture of the people who use your app. You can provide any resource type that is appropriate for the language and culture of your users. For example, the following screenshot shows an app displaying string and drawable resources in the device's default (en_US) locale and the Spanish (es_ES) locale.

The app shows a different text
and icon depending on the current locale

Figure 1. App using different resources depending on the current locale

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

创建本地目录和资源文件


To add support for more locales, create additional directories inside res/. Each directory's name should adhere to the following format:

<resource type>-b+<language code>[+<country code>]

For example, values-b+es/ contains string resources for locales with the language code es. Similarly, mipmap-b+es+ES/ contains icons for locales with the es language code and the ES country code. Android loads the appropriate resources according to the locale settings of the device at runtime. For more information, see Providing Alternative Resources.

After you’ve decided on the locales to support, create the resource subdirectories and files. For example:

MyProject/
    res/
       values/
           strings.xml
       values-b+es/
           strings.xml
       mipmap/
           country_flag.png
       mipmap-b+es+ES/
           country_flag.png

For example, the following are some different resource files for different languages:

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

<resources>
   
<string name="hello_world">Hello World!</string>
</resources>

Spanish strings (es locale), /values-es/strings.xml:

<resources>
   
<string name="hello_world">¡Hola Mundo!</string>
</resources>

United States' flag icon (default locale), /mipmap/country_flag.png:

The icon of flag of the
United States

Figure 2. Icon used for the default (en_US) locale

Spain's flag icon (es_ES locale), /mipmap-b+es+ES/country_flag.png:

The icon of flag of
Spain

Figure 3. Icon used for the es_ES locale

Note: You can use the locale qualifier (or any configuration qualifier) on any resource type, such as if you want to provide localized versions of your bitmap drawable. For more information, see Localization.

在你的 App 中使用资源


You can reference the resources in your source code and other XML files using each resource's name attribute.

In your source code, you can refer to a resource using the syntax R.<resource type>.<resource name>. There are a variety of methods that accept a resource this way.

For example:

// Get a string resource from your app's Resources
String hello = getResources().getString(R.string.hello_world);

// Or supply a string resource to a method that requires a string
TextView textView = new TextView(this);
textView
.setText(R.string.hello_world);

In other XML files, you can refer to a resource with the syntax @<resource type>/<resource name> whenever the XML attribute accepts a compatible value.

For example:

<ImageView
   
android:layout_width="wrap_content"
   
android:layout_height="wrap_content"
   
android:src="@mipmap/country_flag" />
  • No labels