[翻译]Android教程-保存数据-支持多种语言

创建 Locale 路径和 String 文件

为了添加对更多语言的支持,就要在res/里面另外再创建包含一个其路径名称的末尾带上连字符后面,再跟上ISO语言编码的 values 路径 . 例如,values-es/ 是包含带有语言编码“es”的本地方言简单资源的路径 . Android 会根据设备在运行时的方言设置来加载相近的资源 . 更多信息,键 提供可选资源.

一旦你已经决定了你所要支持的语言,那就要创建资源子路径和字符串资源文件了. 例如 :

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

将每一个方言的字符串值都添加到相近的文件中.

在运行时,Android系统会基于用户设备的当前方言设置来使用相近的字符串资源集合 .

例如,下面是一些用于不同语言的字符串资源文件 .

英语(default locale), /values/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>


西班牙语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>

注意:你可以在任何资源类型上使用到这个方言限定符 (或者任何配置限定符), 比如你想要提供位图的方言化版本. 更多的信息,见 本地化.

使用 String 资源

你可以使用由<String>元素名称属性定义的资源名称来引用你的资源代码和XML文件中的字符串资源。


在你的源代码中,你可以使用 R.string.<字符串_名称>来引用字符串资源. 有各种方法可以用这种方式接受一个字符串资源 .

例如 :

// 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);

在其它的XML文件中, 无论何时XML属性药接收一个字符串值你都可以使用语法 @string/<string_name> 来应用一个字符串资源 .

例如 :

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

来源:

http://developer.android.com/training/basics/supporting-devices/languages.html

你可能感兴趣的:(android,国际化,多语言,adt)