Android官方入门文档[9]支持不同的语言
Supporting Different Languages
支持不同的语言
This class teaches you to
1.Create Locale Directories and String Files
2.Use the String Resources
You should also read
•Localization Checklist
•Localization with Resources
该课程教你
1.创建区域设置目录和文件的字符串
2.使用字符串资源
你也应该阅读
•本地化清单
•与资源本地化
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串并保存在一个外部文件。 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工具创建项目(读创建Android项目),该工具创建一个RES/目录下的项目的顶层。在这个res/目录的子目录是各种资源类型。也有一些默认的文件,如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 language 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. For more information, see Providing Alternative Resources.
为了支持更多的语言,创建内部RES/,其中包括一个连字符,并在目录名称末尾的ISO语言代码附加值目录。例如,值-ES/是包含简单resourcess的区域设置的语言代码“ES”的目录。机器人根据设备在运行时的语言环境设置加载相应的资源。欲了解更多信息,请参阅提供替代资源。
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:
<?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:
<?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.
注意:您可以在任何资源类型使用的语言环境预选赛(或任何配置预选赛),如果你想为您的位图绘制的本地化版本,如。欲了解更多信息,请参见本地化。
Use the String Resources
使用String资源
--------------------------------------------------------------------------------
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.
您可以参考您的字符串资源的源代码,并使用由<字符串>元素的name属性定义的资源名称的其他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文件,你可以参考一个字符串资源的语法@string/<string_name>每当XML属性接受一个字符串值。
For example:
例如:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
Next: Supporting Different Screens
下一页:支持不同的屏幕
本文翻译自:https://developer.android.com/training/basics/supporting-devices/languages.html