Android 应用开发支持不同的语言国际化操作

本文介绍了Android应用如何支持多种语言,包括创建区域目录和字符串文件,详细讲解了如何根据ISO语言代码创建values目录,并在不同语言的资源文件中添加字符串。在运行时,系统会自动加载匹配用户设备语言的资源。同时,文章提到了如何在源代码和XML文件中引用这些字符串资源。最后,列举了一些常见语言的资源文件夹命名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载请注明来源: http://blog.youkuaiyun.com/kjunchen/article/details/51312995

Android 应用开发支持不同的语言

创建区域目录和字符串文件

要支持更多的语言,在 res/ 下创建额外的 values 目录以一个连字符 ‘-’ 和 ISO 语言代码结尾命名。例如, values-es 目录包含了一些简单的资源,区域语言代码为 “es” 。Android设备在运行时,会根据语言环境的设置加载相应的资源。

一旦你决定支持某种语言,需要创建资源子目录和字符串资源文件。如:

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

为每一种语言添加字符串值到相应的文件中。

在运行时,Android系统会根据用户设备当前的语言设置加载相应的字符串资源文件。

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

英语(默认语言), /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-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>

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

使用字符串资源

我们可以在源代码和其他的XML文件中通过 < string > 元素中的 name 属性来引用字符串资源。

在源代码中可以使用 R.string.< string_name > 语法来引用资源。大多数方法都接受这种方式引用字符串资源。

例如:

// 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" />

Android 多语言文件夹

常见语言文件夹如下:

  • values-zh 中文
    • values-zh-rCN 中文(中国)
    • values-zh-rHK 中文(香港)
  • values-en 英文
    • values-en-rUS 英文(美国)
    • values-en-rGB 英文(英国)
  • values-de 德文
  • values-fr 法文
  • values-ja 日文
  • values-ko 韩文
  • values-es 西班牙文
  • values-it 意大利文
  • values-ar 阿拉伯文

如有问题欢迎加qq群讨论学习:365532949
HomePage:http://junkchen.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值