Andriod 2.2 語言定製


本文是主要對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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章