為Android添加一門新語言的解決辦法
更新時間:2013年06月15日 15:24:50 作者:
本篇文章是對為Android添加一門新語言的解決方法進行了詳細的分析介紹,需要的朋友參考下
雖然Android從2.3開始已經(jīng)支持50種以上的語言,但是不是每種語言都有字體可以顯示。遇到一個新需求,有客戶要求對hindi語言的支持。于是上網(wǎng)找了一些資料,發(fā)現(xiàn)網(wǎng)上介紹的大部分是如何替換默認字體,就是替換./frameworks/base/data/fonts/DroidSansFallback.ttf,但是替換完之后,中文就無法正常顯示。
其實只要有下面幾個步驟,就可以實現(xiàn)新曾加一種語言的顯示支持:
1. 需要有可以顯示hindi語言的字體,我在網(wǎng)上下載了一個:DroidHindi.ttf
2. 需要修改的地方主要有
1) 將下載的字體文件拷貝到:./frameworks/base/data/fonts/
2) 修改./frameworks/base/data/fonts/Android.mk,將DroidHindi.ttf添加到copy_from:
copy_from := \
DroidSans.ttf \
DroidSans-Bold.ttf \
DroidSansArabic.ttf \
DroidSansHebrew.ttf \
DroidSansThai.ttf \
DroidHindi.ttf \
DroidSerif-Regular.ttf \
DroidSerif-Bold.ttf \
DroidSerif-Italic.ttf \
DroidSerif-BoldItalic.ttf \
DroidSansMono.ttf \
Clockopia.ttf
3) Hindi的語言代碼是hi_IN,修改./device/qcom/common/common.mk,將hindi的語言代碼加進去,這樣在你的設置->語言和鍵盤->選擇語言里面就可以看見hindi語言了:
PRODUCT_LOCALES := en_US en_GB es_ES es_US fr_FR zh_CN zh_TW hi_IN it_IT pt_PT ru_RU
4) 修改./external/skia/src/ports/SkFontHost_android.cpp,將DroidHindi.ttf加進去。網(wǎng)上介紹的很多方法都沒有提到這一步,如果沒有這一步的話,添加的語言是不生效的,顯示的是亂碼,因為android無法找到hindi語言可以顯示的字體,還是會選擇默認字體去顯示。修改的地方為:
/* Fonts must be grouped by family, with the first font in a family having the
list of names (even if that list is empty), and the following members having
null for the list. The names list must be NULL-terminated
*/
static const FontInitRec gSystemFonts[] = {
{ "DroidSans.ttf", gSansNames },
{ "DroidSans-Bold.ttf", NULL },
{ "DroidSerif-Regular.ttf", gSerifNames },
{ "DroidSerif-Bold.ttf", NULL },
{ "DroidSerif-Italic.ttf", NULL },
{ "DroidSerif-BoldItalic.ttf", NULL },
{ "DroidSansMono.ttf", gMonoNames },
/* These are optional, and can be ignored if not found in the file system.
These are appended to gFallbackFonts[] as they are seen, so we list
them in the order we want them to be accessed by NextLogicalFont().
*/
{ "DroidSansArabic.ttf", gFBNames },
{ "DroidSansHebrew.ttf", gFBNames },
{ "DroidSansThai.ttf", gFBNames },
{ "DroidHindi.ttf", gFBNames }, // 新添加的語言
{ "MTLmr3m.ttf", gFBNames }, // Motoya Japanese Font
{ "MTLc3m.ttf", gFBNames }, // Motoya Japanese Font
{ "DroidSansJapanese.ttf", gFBNames },
{ "DroidSansFallback.ttf", gFBNames }
};
5) 去./build/target/product/full.mk看看系統(tǒng)選擇的是哪個語言列表,我的是:
$(call inherit-product, build/target/product/languages_small.mk)
那我就去修改./build/target/product/languages_small.mk,若這里顯示的是languages_full.mk,那么就修改./build/target/product/languages_full.mk文件,修改如下:
PRODUCT_LOCALES := en_US en_GB fr_FR hi_IN it_IT de_DE es_ES
3. 剩下的就是重新編譯一下,然后flash到手機或模擬器上就可以了。
設置頁面:
其實只要有下面幾個步驟,就可以實現(xiàn)新曾加一種語言的顯示支持:
1. 需要有可以顯示hindi語言的字體,我在網(wǎng)上下載了一個:DroidHindi.ttf
2. 需要修改的地方主要有
1) 將下載的字體文件拷貝到:./frameworks/base/data/fonts/
2) 修改./frameworks/base/data/fonts/Android.mk,將DroidHindi.ttf添加到copy_from:
復制代碼 代碼如下:
copy_from := \
DroidSans.ttf \
DroidSans-Bold.ttf \
DroidSansArabic.ttf \
DroidSansHebrew.ttf \
DroidSansThai.ttf \
DroidHindi.ttf \
DroidSerif-Regular.ttf \
DroidSerif-Bold.ttf \
DroidSerif-Italic.ttf \
DroidSerif-BoldItalic.ttf \
DroidSansMono.ttf \
Clockopia.ttf
3) Hindi的語言代碼是hi_IN,修改./device/qcom/common/common.mk,將hindi的語言代碼加進去,這樣在你的設置->語言和鍵盤->選擇語言里面就可以看見hindi語言了:
復制代碼 代碼如下:
PRODUCT_LOCALES := en_US en_GB es_ES es_US fr_FR zh_CN zh_TW hi_IN it_IT pt_PT ru_RU
4) 修改./external/skia/src/ports/SkFontHost_android.cpp,將DroidHindi.ttf加進去。網(wǎng)上介紹的很多方法都沒有提到這一步,如果沒有這一步的話,添加的語言是不生效的,顯示的是亂碼,因為android無法找到hindi語言可以顯示的字體,還是會選擇默認字體去顯示。修改的地方為:
復制代碼 代碼如下:
/* Fonts must be grouped by family, with the first font in a family having the
list of names (even if that list is empty), and the following members having
null for the list. The names list must be NULL-terminated
*/
static const FontInitRec gSystemFonts[] = {
{ "DroidSans.ttf", gSansNames },
{ "DroidSans-Bold.ttf", NULL },
{ "DroidSerif-Regular.ttf", gSerifNames },
{ "DroidSerif-Bold.ttf", NULL },
{ "DroidSerif-Italic.ttf", NULL },
{ "DroidSerif-BoldItalic.ttf", NULL },
{ "DroidSansMono.ttf", gMonoNames },
/* These are optional, and can be ignored if not found in the file system.
These are appended to gFallbackFonts[] as they are seen, so we list
them in the order we want them to be accessed by NextLogicalFont().
*/
{ "DroidSansArabic.ttf", gFBNames },
{ "DroidSansHebrew.ttf", gFBNames },
{ "DroidSansThai.ttf", gFBNames },
{ "DroidHindi.ttf", gFBNames }, // 新添加的語言
{ "MTLmr3m.ttf", gFBNames }, // Motoya Japanese Font
{ "MTLc3m.ttf", gFBNames }, // Motoya Japanese Font
{ "DroidSansJapanese.ttf", gFBNames },
{ "DroidSansFallback.ttf", gFBNames }
};
5) 去./build/target/product/full.mk看看系統(tǒng)選擇的是哪個語言列表,我的是:
$(call inherit-product, build/target/product/languages_small.mk)
那我就去修改./build/target/product/languages_small.mk,若這里顯示的是languages_full.mk,那么就修改./build/target/product/languages_full.mk文件,修改如下:
PRODUCT_LOCALES := en_US en_GB fr_FR hi_IN it_IT de_DE es_ES
3. 剩下的就是重新編譯一下,然后flash到手機或模擬器上就可以了。
設置頁面:

打開一個Hindi的網(wǎng)頁,hindi語言可以正常顯示了:

相關(guān)文章
android開發(fā)教程之清除android數(shù)據(jù)緩存示例(清除本地數(shù)據(jù)緩存)
android清除本地數(shù)據(jù)緩存示例,主要功能清除內(nèi)/外緩存,清除數(shù)據(jù)庫,清除sharedPreference,清除files和清除自定義目錄,需要的朋友可以參考下2014-02-02
Android學習筆記--使用剪切板在Activity中傳值示例代碼
相對于getText和setText而言,利用ClipData對象來傳遞數(shù)據(jù),更符合面向?qū)ο蟮乃枷耄宜軅鬟f的數(shù)據(jù)類型也多樣化了2013-06-06
利用smsmanager實現(xiàn)后臺發(fā)送短信示例
這篇文章主要介紹了android利用SmsManager可以實現(xiàn)后臺發(fā)送短信的方法,最近有使用說明,大家可以參考使用2014-01-01
android ImageView 的幾點經(jīng)驗總結(jié)
本篇文章是對android中ImageView的使用技巧進行了幾點經(jīng)驗總結(jié),需要的朋友參考下2013-06-06
Android?自定義View?加?lifecycle?簡單使用詳解
本文介紹了自定義View的基本使用方法,包括onMeasure、onDraw、自定義樣式和lifecycle的使用,通過了解MeasureSpec的作用和lifecycle的控制,可以更好地管理View的生命周期,避免內(nèi)存泄露問題,感興趣的朋友一起看看吧2025-03-03
Flutter UI如何使用Provide實現(xiàn)主題切換詳解
這篇文章主要給大家介紹了關(guān)于Flutter UI如何使用Provide實現(xiàn)主題切換的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Flutter具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-04-04
Android TV開發(fā):實現(xiàn)3D仿Gallery效果的實例代碼
這篇文章主要介紹了Android TV開發(fā):實現(xiàn)3D仿Gallery效果的實例代碼,效果:滾動翻頁+ 頁面點擊+頁碼指示器+焦點控制,主要為了移植到電視上做了按鍵事件和焦點控制。2018-05-05
使用Android WebSocket實現(xiàn)即時通訊功能
即時通訊(Instant Messaging)最重要的毫無疑問就是即時,不能有明顯的延遲,要實現(xiàn)IM的功能其實并不難,目前有很多第三方,比如極光的JMessage,都比較容易實現(xiàn)。本文通過實例代碼給大家分享Android WebSocket實現(xiàn)即時通訊功能,一起看看吧2019-10-10

