多设备官方教程(7)如何支持多种语言

Supporting Different Languages

  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.

  If you created your project using the Android SDK Tools (readCreating 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.

Create Locale Directories and String Files


To add support for more languages, create additional valuesdirectories 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.

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.

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

English (default locale), /values/strings.xml:

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3     <string name="title">My Application</string>
4     <string name="hello_world">Hello World!</string>
5 </resources>

 

Spanish, /values-es/strings.xml:

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3     <string name="title">Mi Aplicación</string>
4     <string name="hello_world">Hola Mundo!</string>
5 </resources>

 

French, /values-fr/strings.xml:

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3     <string name="title">Mon Application</string>
4     <string name="hello_world">Bonjour le monde !</string>
5 </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


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.

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.

For example:

1 // Get a string resource from your app's Resources
2 String hello = getResources().getString(R.string.hello_world);
3 
4 // Or supply a string resource to a method that requires a string
5 TextView textView = new TextView(this);
6 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.

For example:

1 <TextView
2     android:layout_width="wrap_content"
3     android:layout_height="wrap_content"
4     android:text="@string/hello_world" />

 

posted @   f9q  阅读(204)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
点击右上角即可分享
微信分享提示