It’s always a good practice to extract UI strings from your app code and keep them in an external file. Android makes this easy with a resources directory in each Android project.

将UI字符串从你的app代码的提取出来并保存在一个外部的文件里总是一个良好的做法。Android在每个Android项目里通过一个资源目录使得这一工作变得很容易。

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.

如果你使用Android SDK Tools来创建你的项目(阅读​Creating an Android Project​),那么它将在项目顶层目录中创建一个res/ directory目录。res/ directory 目录包含了不同类型资源的子目录。而且还有一些默认文件,如​ ​​res/values/strings.xml​​(保存了你的字符串值)。


Create Locale Directories and String Files —— 创建区域目录和字符串文件


To add support for more languages, create additional ​​values​​​ directories inside​​res/​​​ that include a hyphen and the ISO country code at the end of the directory name. For example,​​values-es/​​ is the directory containing simple resourcess for the Locales with the language code "es". Android loads the appropriate resources according to the locale settings of the device at run time.

为了添加对更多语言的支持,在res/ 下创建额外的values目录,它们的目录名字的末尾包含了一个连字符和ISO国家代码。例如,values-es/ 目录包含了国家码为“es”的区域的简单资源。Android在运行适合会根据区域设置来加载合适的资源。

Once you’ve decided on the languages you will support, create the resource subdirectories and string resource files. For example:

一旦你决定你将要支持的语言,就创建资源子目录和字符串资源文件。例如:

MyProject/
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml

Add the string values for each locale into the appropriate file.

向合适的文件里为每个区域添加字符串值。

At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.

在运行时刻,Android系统基于用户设备的本地当前设置,使用合适的字符串资源。

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

例如,下面的例子中包含了对于不同语言的一些不同的字符串资源。

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

英语(默认区域),/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">My Application</string>
<string name="hello_world">Hello World!</string>
</resources>

Spanish, ​​/values-es/strings.xml​​:

西班牙语,​​/values-es/strings.xml​​:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Mi Aplicación</string>
<string name="hello_world">Hola Mundo!</string>
</resources>

French, ​​/values-fr/strings.xml​​:

法语,/values-fr/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Mon Application</string>
<string name="hello_world">Bonjour le monde !</string>
</resources>

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

注意:你可以在任何资源类型中使用区域限定符(或者其他配置限定符),例如如果你想要提供本地化版本的可绘制化位图。更多的信息详见​Localization​


Use the String Resources —— 使用字符串资源


You can reference your string resources in your source code and other XML files using the resource name defined by the​​<string>​​​ element's​ ​​name​​ attribute.

你可以使用资源名称(以​​<string>元素名称属性​​定义)在你的源代码或者其他XML文件里引用你的字符串资源。

In your source code, you can refer to a string resource with the syntax ​​R.string.<string_name> ​​. There are a variety of methods that accept a string resource this way.

在你的源代码里,你可以通过语法 ​​R.string.<string_name>​​来使用一个字符串资源。有很多方法以这样的方法接受一个字符串。

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 string resource with the syntax ​​@string/<string_name>​​ whenever the XML attribute accepts a string value.

在其他的XML文件里,在任何时刻XML属性接受一个字符创值时,你可以通过语法 ​​@string/<string_name>来使用一个字符串资源。​

For example:

例如:

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />