斯大林当政多少年:Android?技术专题系列之五?--?本地化

来源:百度文库 编辑:偶看新闻 时间:2024/03/28 17:26:30

Android 技术专题系列之五 -- 本地化

本文内容主要基于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字符串的内容:
 Hello World,HelloAndroid

3)创建一个新的目录和新的strings.xml文件,用来存储hello字符串的中文翻译:
res/values-zh/strings.xml:



   你好,Android
   你好,Android


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

#setprop persist.sys.language zh;setprop persist.sys.countryCN;stop;sleep 5;start