Android 技術專題系列之五 -- 本地化

原文地址:http://www.apkbus.com/forum.php?mod=viewthread&tid=2713&highlight=Android%2B%E6%8A%80%E6%9C%AF%E4%B8%93%E9%A2%98

 

本文內容主要基於http://groups.google.com/group/a ... -android-apps-draft
展開。

Android已經設計好了國際化(I18N)和本地化(L10N)的框架,只要編寫程序時符合這個框架要求,實現程序的本地化並不困難。

下面以HelloAndroid (http://code.google.com/android/intro/hello-android.html中Eclipse SDK自動生成)爲例,討論如何實現HelloAndroid的中文化。
1)文件src/com/android/hello/R.java中列出了hello 字符串的id:
public final class R {
...
public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
    }
}

2)文件res/values/strings.xml中列出了hello字符串的內容:
<string name="hello">Hello World, HelloAndroid</string>

3)創建一個新的目錄和新的strings.xml文件,用來存儲hello字符串的中文翻譯:
res/values-zh/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">你好,Android</string>
    <string name="app_name">你好,Android</string>
</resources>

4) 修改文件 src/com/android/hello/HelloAndroid.java,
將tv.setText("Hello, Android");改成 tv.setText(R.string.hello);
5)編譯helloAndroid項目,設置虛擬機locale爲zh-CN後重新啓動emulator。在從eclipse中運行helloAndroid項目,這時,可見中文顯示。
$./adb shell
#echo zh-CN > /data/locale;stop;sleep 5; start <sdk 1.0>

#setprop persist.sys.language zh;setprop persist.sys.country CN;stop;sleep 5;start <sdk 1.5>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章