Supporting Different Languages

支持不同语言

This class teaches you to

这节课教给你

  1. Create Locale Directories and String Files

    创建一个本地目录和String文件夹

  2. Use the String Resources

    使用String资源

You should also read

你还需要阅读

  • Localization Checklist

    本地清单列表

  • Localization with Resources

    本地化资源

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 工具创建你的项目(请阅读Creating an Android Project),SDK工具会在你项目的顶层创建一个 res/ 目录。在这个 res/ 目录下是一些各种各样的资源类型的子目录。这里也有一些默认的文件,比如 res/values/strings.xml,这里面保存的是你的字符串值。

Create Locale Directories and String Files

创建一个本地目录和String文件

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.

为了让你的app支持更多的语言,需要在 res/ 下创建一个额外的 values 目录,在这个目录的名字后面需要跟上一个连字符以及ISO( 国际标准化组织)语言代码。例如,values-es/ 是仅仅包含对于本地语言代码是“es”的资源目录。Android在运行时,会根据设备对于地区的设置来加载适当的资源。想获取跟多信息,请看Providing Alternative Resources。

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.

把对于每种不同地区的string值添加到适当的文件中。

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

在运行的时候,Android系统会使用适当的string资源,这取决于用户的设备当前的地区设置。

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

例如,下面是对于不同语言的几种不同string资源文件:

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

使用String资源

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

你可以通过已经在string资源中已经定义好的资源名字在你的源代码或其他的XML文件中引用你的string资源,这些资源名字通过元素的名字属性来定义。

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

在你的源代码中,你可以用R.string.这个语法来引用一个资源文件。有许多方法都可以通过这种方式接受一个string资源。

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/ whenever the XML attribute accepts a string value.

在其他的XML文件中,每当XML属性接收一个string值时,你就可以用 @string/这个语法来关联到一个string资源。

For example:

例如:

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

Next: Supporting Different Screens

下一节:支持不同的屏幕

这是我自己翻译的,如果您发现其中有重要错误,敬请指出,万分感谢!

Android官方文档翻译 十三 3.1Supporting Different Languages的更多相关文章

  1. android官方文档翻译(不断更新中。。。)

    最近在自学android,抽空把官方文档的guide跟training差不多看了一遍,又对比了一些书籍,感觉还是官方文档讲得比较好,所以自己计划把官方文档翻译一下,方便自己的知识巩固以及复习查找,由于 ...

  2. Android官方文档翻译 十二 3.Supporting Different Devices

    Supporting Different Devices 支持不同设备 Dependencies and prerequisites 依赖关系和先决条件 Android 1.6 or higher A ...

  3. Android官方文档翻译 九 2.2Adding Action Buttons

    Adding Action Buttons 增加动作按钮 This lesson teaches you to 这节课教给你 Specify the Actions in XML 在XML中指定动作 ...

  4. Android官方文档翻译 五 1.3Building a Simple User Interface

    Building a Simple User Interface 创建一个简单的用户界面 This lesson teaches you to 这节课将教给你: Create a Linear Lay ...

  5. Android官方文档翻译 十七 4.1Starting an Activity

    Starting an Activity 开启一个Activity This lesson teaches you to 这节课教给你 Understand the Lifecycle Callbac ...

  6. Android官方文档翻译 十六 4.Managing the Activity Lifecycle

    Managing the Activity Lifecycle 管理activity的生命周期 Dependencies and prerequisites 依赖关系和先决条件 How to crea ...

  7. Android官方文档翻译 十五 3.3Supporting Different Platform Versions

    Supporting Different Platform Versions 支持不同的平台版本 This lesson teaches you to 这节课教给你 Specify Minimum a ...

  8. Android官方文档翻译 十四 3.2Supporting Different Screens

    Supporting Different Screens 支持不同的屏幕 This lesson teaches you to 这节课教给你 Create Different Layouts 创建不同 ...

  9. Android官方文档翻译 四 1.2Running Your App

    Running Your App If you followed the previous lesson to create an Android project, it includes a def ...

随机推荐

  1. Table.RowCount行列计数…Count(Power Query 之 M 语言)

    数据源: 任意五行两列 目标: 计算行数(包括空行) 操作过程: [转换]>[对行进行计数] M公式:  = Table.RowCount( 表 ) 扩展: 对表中列进行计数:= Table.C ...

  2. 工作簿拆分(Excel代码集团)

    一个工作簿中包括N个工作表,将各个工作表拆分成工作簿. 工作表数量不定,表内内容不限,拆分后保存于当前文件夹内. Sub Sample() Dim MySheetsCount As Long For ...

  3. CF748A Santa Claus and a Place in a Class 题解

    Content 圣诞老人坐在一个桌子被摆成 \(m\) 行 \(n\) 列的教室里.每个桌子有两个座位,每个位置从左往右都有编号,依次为 \(1,2,3,...,2\times n\times m\) ...

  4. Java容器源码学习--ArrayList源码分析

    ArrayList实现了List接口,它的底层数据结构是数组,因此获取容器中任意元素值的时间复杂度为O(1),新增或删除元素的时间复杂度为O(N).每一个ArrayList实例都有一个capacity ...

  5. c++之一个方便的日志库

    概述 本文演示环境: win10 + vs2017 日志,我用的很少,通常是用作动态库调试使用. 日志记录下来,基本就没看过,除非模块出现了问题. 使用cmake管理的项目 使用C++封装了C语言读写 ...

  6. 【LeetCode】1029. Two City Scheduling 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 排序 日期 题目地址:https://lee ...

  7. 【九度OJ】题目1196:成绩排序 解题报告

    [九度OJ]题目1196:成绩排序 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1196 题目描述: 用一维数组存储学号和成绩,然后 ...

  8. 5分钟搭建wordpress个人博客网站——宝塔傻瓜式部署,无坑系列,附赠主题和md插件[2021-12-31]

    一.前言 自从买了服务器,小编已经马不停蹄的学了两天服务搭建的知识,问了很多大佬,快速搭建自己的博客网站.有四种方式,我在这里全部分享给大家.自己已经搭建好,欢迎大家过来看一下,给你提供个思路哈! 小 ...

  9. Java高级程序设计笔记 • 【第4章 网络编程】

    全部章节   >>>> 本章目录 4.1 网络基础知识 4.1.1 IP地址 4.1.2 端口号 4.1.3 使用InetAddress 4.1.4 InetAddress 类 ...

  10. Java基础(八)——IO流3_对象流

    一.对象流 1.序列化与反序列化 序列化:将内存中的Java对象保存到磁盘中或通过网络传输出去. 反序列化:将磁盘文件中的对象还原为内存中的一个Java对象. 用途: (1)将对象保存到物理硬盘:比如 ...