本文内容主要基于http://groups.google.com/group/android-developers/web/localizing-android-apps-draft
展开。
Android已经设计好了国际化(I18N)和本地化(L10N)的框架,只要编写程序时符合这个框架要求,实现程序的本地化并不困难。
下面以HelloAndroid(http://code.google.com/android/intro/hello-android.html中EclipseSDK自动生成)为例,讨论如何实现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字符串的内容:
<stringname="hello">Hello World,HelloAndroid</string>
3)创建一个新的目录和新的strings.xml文件,用来存储hello字符串的中文翻译:
res/values-zh/strings.xml:
<?xml version="1.0"encoding="utf-8"?>
<resources>
<stringname="hello">你好,Android</string>
<stringname="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.countryCN;stop;sleep 5;start <sdk 1.5>