【Android】Android国际化
-----------------------
? ? ? ?國(guó)際化的英文單詞是Internationalization,因?yàn)檫@個(gè)單詞太長(zhǎng)了,有時(shí)也簡(jiǎn)稱為I18N,其中的I是這個(gè)單詞的第一個(gè)字符,18表示中間省略的字母?jìng)€(gè)數(shù),而N代表這個(gè)單詞的最后一個(gè)字母。所以,I18N也就是國(guó)際化的意思。Android程序國(guó)際化,也就是程序可以根據(jù)系統(tǒng)所使用的語(yǔ)言,將界面中的文字翻譯成與之對(duì)應(yīng)的語(yǔ)言。這樣,可以讓程序更加通用。Android可以通過(guò)資源文件非常方便的實(shí)現(xiàn)程序的國(guó)際化。
在編寫Android項(xiàng)目時(shí),通常都是將程序中要使用的字符串資源放置在res/values目錄下的strings.xml文件中,為了給這些字符串資源實(shí)現(xiàn)國(guó)際化,可以在Android項(xiàng)目的res目錄下,創(chuàng)建對(duì)應(yīng)于各個(gè)語(yǔ)言的資源文件夾(例如,為了讓程序兼容簡(jiǎn)體中文、繁體中文和美式英文,可以分別創(chuàng)建名稱為values-zh-rCN、values-zh-rTW和values-en-rUS的文件夾),然后在每個(gè)文件夾中創(chuàng)建一個(gè)對(duì)應(yīng)的strings.xml文件,并在該文件中定義對(duì)應(yīng)語(yǔ)言的字符串即可。這樣,當(dāng)程序運(yùn)行時(shí),就會(huì)自動(dòng)根據(jù)操作系統(tǒng)所使用的語(yǔ)言來(lái)顯示對(duì)應(yīng)的字符串信息了。
官方參考API:http://developer.android.com/training/basics/supporting-devices/languages.html
【Supporting Different Languages】步驟
(a)步驟一 ?Create Locale Directories and String Files
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
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:
<?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>
(b)步驟二 ?Use the String Resources
【示例】
// 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);
或者:
<TextView
? ? android:layout_width="wrap_content"
? ? android:layout_height="wrap_content"
? ? android:text="@string/hello_world" />
Internationalization (國(guó)際化)簡(jiǎn)稱 i18n,因?yàn)樵趇和n之間還有18個(gè)字符,localization(本地化 ),簡(jiǎn)稱L10n。
一般用 語(yǔ)言_地區(qū)的形式表示一種語(yǔ)言,如? zh_CN, zh_TW.
各國(guó)語(yǔ)言縮寫??http://www.loc.gov/standards/iso639-2/php/code_list.php
國(guó)家和地區(qū)簡(jiǎn)寫?http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html
在Android工程的res目錄下,通過(guò)定義特殊的文件夾名稱就可以實(shí)現(xiàn)多語(yǔ)言支持。比如我們的程序兼容 簡(jiǎn)體中文、日文、英文、法文和德文,在values文件夾中建立默認(rèn)strings.xml,再建立 values-zh-rCN(zh表示中文rCN表示簡(jiǎn)體,類似還有美式英語(yǔ),奧式英語(yǔ))、values-ja、values、values-fr和values-de文件夾。(可以用開(kāi)發(fā)工具建:見(jiàn)http://www.cnblogs.com/wuyunan/archive/2009/09/16/1567960.html)
在每個(gè)文件夾里放置一個(gè)strings.xml,strings.xml里是各種語(yǔ)言字符串。如果涉及到參數(shù)配置類xml文件夾也要改成xml-zh、xml-ja、xml、xml-fr和xml-de。這樣在android的系統(tǒng)中進(jìn)行語(yǔ)言切換,所開(kāi)發(fā)的程序也會(huì)跟著切換語(yǔ)言。
總結(jié)
以上是生活随笔為你收集整理的【Android】Android国际化的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【Android】dip、dp、sp、p
- 下一篇: 【Linux】Linux中at命令详解