Android开发第3-1课:支持不同的语言
2013-05-18 10:51
507 查看
This class teaches you to
Create Locale Directories and String FilesUse the String Resources
You should also read
LocalizationIt’s always a good practice to extract UI strings from your app code and keep themin an external file. Android makes this easy with a resources directory in each Androidproject.
If you created your project using the Android SDKTools (read
Creating anAndroid Project), the tools create a
res/directory in the top level ofthe project. Within this
res/directory are subdirectories for various resourcetypes. There are also a few default files such as
res/values/strings.xml,
which holdsyour string values.
Create Locale Directories and String Files
To add support for more languages, create additionalvaluesdirectories inside
res/that include a hyphen and the ISO country code at the end of thedirectory name. For example,
values-es/is the directory containing simpleresourcess
for the Locales with the language code "es". Android loads the appropriate resourcesaccording to the locale settings of the device at run time.
Once you’ve decided on the languages you will support, create the resource subdirectories andstring 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 thelocale 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:
<?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:
<?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>
Note: You can use the locale qualifier (or anyconfiguration qualifer) on any resource type, such as if you want to providelocalized versions of your bitmap drawable. For more information, seeLocalization.
Use the String Resources
You can reference your string resources in your source code and other XML files using theresource name defined by the<string>element's
nameattribute.
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 thisway.
For example:
// Get a string resource from your app's [code]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);[/code]
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:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />
相关文章推荐
- Android官方开发文档Training系列课程中文版:支持不同的设备之支持不同的语言
- Android开发第3-3课:支持不同的平台版本
- Android 应用开发支持不同的语言国际化操作
- Android开发第3-2课:支持不同的屏幕
- Android官方开发文档Training系列课程中文版:支持不同的设备之支持不同的屏幕
- Android新增C/C++语言支持:应用开发更快
- 第二部分:开发简要指南-第五章 支持不同的Android设备
- Android应用如何支持不同的语言和文化
- 跟Google学习Android开发-起始篇-支持不同的设备(2)
- 如何看待 Kotlin 成为 Android 官方支持开发语言?
- 如何看待 Kotlin 成为 Android 官方支持开发语言?
- 如何让自己开发的android支持多种不同的手机屏幕(supporting Multiple Screens)
- Android 7.0多语言支持开发浅析
- 移动开发:Android官方提供的支持不同屏幕大小的全部方法
- 【Android Developers Training】 11. 支持不同语言
- Android开发笔记(八十三)多语言支持
- (完结)Android官方培训课-支持不同的语言
- Android官方开发文档Training系列课程中文版:多样屏幕之支持不同的屏幕密度
- 跟Google学习Android开发-起始篇-支持不同的设备(3)
- 让android支持不同的语言、屏幕