Android 多语言开发


第一部分 多语言定制的机制


1、ICU4C简介

ICU4C(ICU for C, http://site.icu-project.org/ ) 是ICU在C/C++平台下的版本, ICU(International Component for Unicode)是基于"IBM公共许可证"的,与开源组织合作研究的, 用于支持软件国际化的开源项目。ICU4C提供了C/C++平台强大的国际化开发能力,软件开发者几乎可以使用ICU4C解决任何国际化的问题,根据各地的风俗和语言习惯,实现对数字、货币、时间、日期、和消息的格式化、解析,对字符串进行大小写转换、整理、搜索和排序等功能,必须一提的是,ICU4C提供了强大的BIDI算法,对阿拉伯语等BIDI语言提供了完善的支持。

ICU首先是由Taligent公司开发的,Taligent公司现在被合并为IBM?公司全球化认证中心的Unicode研究组,然后ICU由IBM和开源组织合作继续开发,开源组织给与了ICU极大的帮助。
开始ICU只有Java平台的版本,后来这个平台下的ICU类被吸纳入SUN公司开发的JDK1.1,并在JDK以后的版本中不断改进。C++和C平台下的ICU是由JAVA平台下的ICU移植过来的,移植过的版本被称为ICU4C,来支持这C/C++两个平台下的国际化应用。 ICU4J和ICU4C区别不大,但由于ICU4C是开源的,并且紧密跟进Unicode标准,ICU4C支持的Unicode标准总是最新的;同时,因为JAVA平台的ICU4J的发布需要和JDK绑定,ICU4C支持Unicode标准改变的速度要比ICU4J快的多。

2、 ANDROID语言包

Android 使用的语言包就是ICU4C,位置:external/icu4c。Android支持的语言有: Locale CANADA
Locale constant for en_CA.
Locale CANADA_FRENCH
Locale constant for fr_CA.
Locale CHINA
Locale constant for zh_CN.
Locale CHINESE
Locale constant for zh.
Locale ENGLISH
Locale constant for en.
Locale FRANCE
Locale constant for fr_FR.
Locale FRENCH
Locale constant for fr.
Locale GERMAN
Locale constant for de.
Locale GERMANY
Locale constant for de_DE.
Locale ITALIAN
Locale constant for it.
Locale ITALY
Locale constant for it_IT.
Locale JAPAN
Locale constant for ja_JP.
Locale JAPANESE
Locale constant for ja.
Locale KOREA
Locale constant for ko_KR.
Locale KOREAN
Locale constant for ko.
Locale PRC
Locale constant for zh_CN.
Locale SIMPLIFIED_CHINESE
Locale constant for zh_CN.
Locale TAIWAN
Locale constant for zh_TW.
Locale TRADITIONAL_CHINESE
Locale constant for zh_TW.
Locale UK
Locale constant for en_GB.
Locale US
Locale constant for en_US.

3、定制语言

定制语言在PRODUCT_LOCALES字段里添加需要语言,如: PRODUCT_LOCALES := en_US zh_CN,则系统里只有英语和汉语两种语言。然后语言的选择处理是在external/icu4c/stubdata/Android.mk里进行的,如下:
config := $(word 1, $(if $(findstring ar,$(PRODUCT_LOCALES)),large)
$(if $(findstring da,$(PRODUCT_LOCALES)),large)
$(if $(findstring el,$(PRODUCT_LOCALES)),large)
$(if $(findstring fi,$(PRODUCT_LOCALES)),large)
$(if $(findstring he,$(PRODUCT_LOCALES)),large)
$(if $(findstring hr,$(PRODUCT_LOCALES)),large)
$(if $(findstring hu,$(PRODUCT_LOCALES)),large)
$(if $(findstring id,$(PRODUCT_LOCALES)),large)
$(if $(findstring ko,$(PRODUCT_LOCALES)),large)
$(if $(findstring nb,$(PRODUCT_LOCALES)),large)
$(if $(findstring pt,$(PRODUCT_LOCALES)),large)
$(if $(findstring ro,$(PRODUCT_LOCALES)),large)
$(if $(findstring ru,$(PRODUCT_LOCALES)),large)
$(if $(findstring sk,$(PRODUCT_LOCALES)),large)
$(if $(findstring sr,$(PRODUCT_LOCALES)),large)
$(if $(findstring sv,$(PRODUCT_LOCALES)),large)
$(if $(findstring th,$(PRODUCT_LOCALES)),large)
$(if $(findstring tr,$(PRODUCT_LOCALES)),large)
$(if $(findstring uk,$(PRODUCT_LOCALES)),large)
$(if $(findstring zh,$(PRODUCT_LOCALES)),large)
$(if $(findstring ja,$(PRODUCT_LOCALES)),us-japan)
$(if $(findstring it,$(PRODUCT_LOCALES)),us-euro)
$(if $(findstring pl,$(PRODUCT_LOCALES)),us-euro)
$(if $(findstring cs,$(PRODUCT_LOCALES)),default)
$(if $(findstring de,$(PRODUCT_LOCALES)),default)
$(if $(findstring fr,$(PRODUCT_LOCALES)),default)
$(if $(findstring nl,$(PRODUCT_LOCALES)),default)
us)

4、默认语言
默认语言的选择实现是在build/core/Makefile里,从PRODUCT_LOCALES里选择第一个语言作为默认语言,如下:
define default-locale $(subst _, , $(firstword $(1)))
endef
# Selects the first locale in the list given as the argument
# and returns the language (or the region)
define default-locale-language $(word 2, 2, $(call default-locale, $(1)))
endef
define default-locale-region $(word 3, 3, $(call default-locale, $(1)))
Endef ... PRODUCT_DEFAULT_LANGUAGE="$(call default-locale-language,$(PRODUCT_LOCALES))"
PRODUCT_DEFAULT_REGION="$(call default-locale-region,$(PRODUCT_LOCALES))"
然后通过build/tool/buildinfo.sh文件将如下段写到文件build.prop,如下:
echo "ro.product.locale.language=$PRODUCT_DEFAULT_LANGUAGE"
echo "ro.product.locale.region=$PRODUCT_DEFAULT_REGION" 。

因此,要改变默认语言用下面两种方法中的一种就行了:

4.1、在PRODUCT_LOCALES字段里,将要选择的语言放在第一位,如: PRODUCT_LOCALES := en_US zh_CN 默认语言是英语;
4.2、在persist.sys.language 和persist.sys.country 里指定语言,如下: PRODUCT_PROPERTY_OVERRIDES :=
persist.sys.language=zh
persist.sys.country=CN build.prop文件的处理是在system/core/init/property_service.c。


第二部分 多语言定制的方法


1、多语言定制的实现步骤

1)进入build/target/product目录,在languages_full.mk或languages_small.mk文件中,修改PRODUCT_LOCALES的值,来定制语言,比如PRODUCT_LOCALES := en_US zh_CN zh_TW en_GB fr_FR it_IT de_DE es_ES;

2)相同目录下,修改full.mk文件的
$(call inherit-product, build/target/product/languages_small|full.mk)语句来切换所使用的文件;

3)重新编译即可。

2、设置默认语言的实现步骤
1)进入build/target/product目录,修改文件core.mk的PRODUCT_PROPERTY_OVERRIDES 值,例如,欲修改为默认中文,则增加
“ persist.sys.language=zh persist.sys.country=CN”,增加后的语句如PRODUCT_PROPERTY_OVERRIDES :=

ro.config.notification_sound=OnTheHunt.ogg

ro.config.alarm_alert=Alarm_Classic.ogg persist.sys.language=zh persist.sys.country=CN
2)重新编译即可。

3、与多语言定制相关的字段及其所在的文件

PREVIOUS_BUILD_CONFIG out/target/product/dream/previous_build_config.mk

NO_FALLBACK_FONT的定义 device/htc/dream-sapphire/BoardConfigCommon.mk

NO_FALLBACK_FONT的调用 frameworks/base/data/fonts/Android.mk

extra_locales CUSTOM_LOCALES nodpi mdpi hdpi build/core/product_config.mk

PRODUCT_PROPERTY_OVERRIDES build/target/product
build.prop out/target/product/generic/system

猜你喜欢

转载自quanminchaoren.iteye.com/blog/1634316