让 App 支持不同的语言(中文,法文)

Android 开发

让 App 本土化的第一件事就是语言,知道如何让 App 支持各种语言会很有帮助,而且可以减少字符串硬编码

因为文档解释得很详细了,所以下面就跟着 文档 走一轮,有一些地方做出小小的标识和改动

创建

要怎样做?按照这个 文档 里说的

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 resources 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/ 创建另外的 values 文件夹,并在文件夹 (values) 名称末尾加上连字符 ISO 语言代码。例如,values-es/ 目录包含的资源会用于语言代码为“es”的语言区域 (这个是西班牙语)Android 根据运行时设备的语言区域设置加载相应语言的资源**。如需了解详细信息,请参阅提供备用资源

意思是在 res/ 目录下创建 名称末尾 不同的 values ,其中目录末尾 ISO 语言代码 则对应着相应语言,譬如:values-fr 则代表着手机系统语言为 法语 时读取的目录

一旦您决定了为哪些语言提供支持,便可创建资源子目录和字符串资源文件。例如 我要支持 中文法文, 英文(默认):

  1. 创建目录, 对 res 右键-> New -> Android resource directory:

    《让 App 支持不同的语言(中文,法文)》 create dir

  2. 在相应目录下创建 strings.xml 文件,右键目录-> New -> Values res…ce file

    《让 App 支持不同的语言(中文,法文)》 创建 strings.xml

最后res文件夹里的结构大致上如下:

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

将各个语言区域的字符串值添加到相应文件中。

在运行时,Android 系统会根据当前为用户设备设置的语言区域使用相应的字符串资源集的

例如,以下是一些不同语言下的不同 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>

中文,/values-zh/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">我的应用</string>
    <string name="hello_world">你好 世界!</string>
</resources>

法语,/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>

注:您可以在任何资源类型上使用语言区域限定符(或任何配置限定符),例如,您可以提供本地化版本的可绘制位图。 如需了解详细信息,请参阅本地化

使用定义在 strings.xml 里的字符串

继续照着 文档

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> 元素name 属性 定义的资源名称在您的源代码(java 代码) 和 其他 XML 文件(布局文件) 中引用您的 字符串资源(定义在 strings.xml 里的字符串)

字符串资源(string resources) 可以理解为 定义在 strings.xml 里的字符串

在您的源代码中,可以使用语法 R.string.<string_name> 引用字符串资源(定义在 strings.xml 里的字符串), 有许多方法都接受以这种方式引用字符串。

例如,这里取出名字为 “hello_world” 的字符串资源,假设手机语言是中文的,那么取出的字符串,按照上面定义好的,就为: 你好 世界!


// Get a string resource from your app's Resources
//变量 hello 的值为 "你好 世界!"
String hello = getResources().getString(R.string.hello_world);

// Or supply a string resource to a method that requires a string
// textView 会显示 "你好 世界!"
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" />

更多

    原文作者:莫威權在B612等着那温柔的风
    原文地址: https://www.jianshu.com/p/530e033fca33
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞