android課程升級(jí)版08國(guó)際化和本地化_第1頁(yè)
android課程升級(jí)版08國(guó)際化和本地化_第2頁(yè)
android課程升級(jí)版08國(guó)際化和本地化_第3頁(yè)
android課程升級(jí)版08國(guó)際化和本地化_第4頁(yè)
android課程升級(jí)版08國(guó)際化和本地化_第5頁(yè)
已閱讀5頁(yè),還剩21頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

Android國(guó)際化和本地化

2023/3/17版權(quán)聲明華清遠(yuǎn)見(jiàn)教育集團(tuán);未經(jīng)華清遠(yuǎn)見(jiàn)明確許可,不得為任何目的以任何形式復(fù)制或傳播此文檔的任何部分;本文檔包含的信息如有更改,恕不另行通知;華清遠(yuǎn)見(jiàn)教育集團(tuán)保留所有權(quán)利。

2目錄了解Android應(yīng)用國(guó)際化的意義掌握Locale類的用法掌握使用NumerFormat格式化數(shù)字、貨幣掌握使用DateFormat、SimpleDateFormat格式化日期和時(shí)間掌握Android中資源文件的定義和使用掌握消息的格式化如何讓世界各地的人都使用我的軟件?國(guó)際化你的程序I18N/Internationalization我的程序怎么知道它運(yùn)行在哪個(gè)國(guó)家?本地化你的程序L10N/Localization國(guó)際化和本地化如何國(guó)際化?確定地點(diǎn)

Locale類封裝了關(guān)于國(guó)家和語(yǔ)言的信息國(guó)家地區(qū)編碼采用ISO3166-1標(biāo)準(zhǔn),用2位大寫(xiě)字母表示,如CN、US等語(yǔ)言編碼采用ISO639-1標(biāo)準(zhǔn),用2位小寫(xiě)字母表示,如zh、en等Locale示例Localelocale=getResources().getConfiguration().locale;Stringcountry=locale.getCountry();Stringlanguage=locale.getLanguage();強(qiáng)制指定地域通過(guò)Locale來(lái)強(qiáng)制指定地域:

Resourcesresources=getResources();

Configurationconfig=resources.getConfiguration();

config.locale=Locale.SIMPLIFIED_CHINESE;//簡(jiǎn)體中文

DisplayMetricsdm=res.getDisplayMetrics();

resources.updateConfiguration(config,dm);

7格式化輸出格式化輸出數(shù)字相關(guān)的格式化——NumberFormat日期/時(shí)間格式化——DateFormat字符信息格式化——MessageFormat

8數(shù)字相關(guān)格式化NumberFormat類數(shù)字(Numeric):getNumberInstance()/getNumberInstance(Localelcl)貨幣(Currency):getCurrencyInstance()/getCurrencyInstance(Localelcl)百分比(Percent):getPercentInstance()/getPercentInstance(Localelcl)示例 doublemoney=123000.879; //設(shè)定三個(gè)國(guó)家的locale LocalecnLocale=newLocale("zh","CN"); LocalejpLocale=newLocale("ja","JP"); LocaleusLocale=newLocale("en","US"); //得到三個(gè)對(duì)應(yīng)的NumberFormat對(duì)象

NumberFormatcnnf=NumberFormat.getCurrencyInstance(cnLocale); NumberFormatjpnf=NumberFormat.getCurrencyInstance(jpLocale); NumberFormatusnf=NumberFormat.getCurrencyInstance(usLocale); //將上面的double數(shù)值格式化輸出

tv1.setText("ChinacurrencyFormat:"+cnnf.format(money)); tv2.setText("JapancurrencyFormat:"+jpnf.format(money)); tv3.setText(“UScurrencyFormat:"+usnf.format(money));

ChinacurrencyFormat:¥123,000.88JapancurrencyFormat:¥123,001UScurrencyFormat:$123,000.88日期/時(shí)間格式化DateFormatDateFormatgetDateInstance()DateFormatgetDateInstance(intdateStyle,Localelcl)DateFormatgetTimeFormat()DateFormatgetTimeFormat(inttimeStyle,Localelcl)DateFormatgetDateTimeFormat()DateFormatgetDateTimeFormat(intdateStyle,inttimeStyle,Localelcl)dateStyle/timeStyle可以取如下的值:DateFormat.DEFAULT——默認(rèn)格式DateFormat.LONG——長(zhǎng)格式DateFormat.MEDIUM——中等格式DateFormat.SHORT——段格式DateFormat.FULL——完整格式使用SimpleDateFormatSimpleDateFormat簡(jiǎn)化了日期和時(shí)間的格式化它主要用于本地化SimpleDateFormat案例SimpleDateFormatmyFmt0=newSimpleDateFormat("yyyy年MM月dd日HH時(shí)mm分ss秒");SimpleDateFormatmyFmt1=newSimpleDateFormat("yy/MM/ddHH:mm");SimpleDateFormatmyFmt2=newSimpleDateFormat("yyyy-MM-ddhh:mm:ssa");Stringpattern="現(xiàn)在是一年中的第D天,"

+"一年中第w個(gè)星期,一月中第W個(gè)星期,E,z時(shí)區(qū)";SimpleDateFormatmyFmt3=newSimpleDateFormat(pattern);LocaleusLocale=newLocale("en","US");SimpleDateFormatmyFmt4=newSimpleDateFormat("'On'MM-dd-yyyyHH'o''clock,...'",usLocale);

13資源文件Android應(yīng)用中國(guó)資源包日本德國(guó)res/values-cn/strings.xmlres/drawable-zh/res/drawable-zh-rCN...res/values-ja/strings.xmlres/drawable-ja/res/drawable-ja-rJP/...res/values-de/strings.xmlRes/drawable-de/res/drawable-de-rDe/...資源文件指定值:res/values/strings.xml (默認(rèn),當(dāng)找不到對(duì)應(yīng)Locale的資源時(shí),使用這個(gè)資源)res/values-zh/strings.xml res/values-zh-rCN/strings.xml res/values-ja/strings.xml res/values-ja-rJP/strings.xml圖片資源res/drawable/(默認(rèn),當(dāng)找不到對(duì)應(yīng)Locale的圖片資源時(shí),使用這個(gè)文件夾中的資源)res/drawable-zh/ res/drawable-zh-rCN/res/drawable-ja/ res/drawable-ja-rJP/

15資源文件案例

16資源文件案例<?xmlversion="1.0"encoding="utf-8"?><resources><stringname="hello">你好世界!</string><stringname="app_name">你好世界!</string></resources>

17res/values-zh/strings.xml<?xmlversion="1.0"encoding="utf-8"?><resources><stringname="hello">こんにちは,世界</string><stringname="app_name">こんにちは,世界</string></resources>res/values-ja/strings.xml資源文件案例<?xmlversion="1.0"encoding="utf-8"?><resources><stringname="hello">HelloWorld!</string><stringname="app_name">HelloWorld!</string></resources>

18res/values/strings.xmlpublicclassResourcesActivityextendsActivity{ publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); }}ResourcesActivity.java資源文件案例

19消息的格式化使用MessageFormat來(lái)對(duì)消息進(jìn)行格式化將文本消息寫(xiě)成一個(gè)字符串,在其中不確定的部分使用占位符,在一個(gè)字符串中最多可以使用10個(gè)占位符({0}~{9}),每個(gè)占位符可以多次使用;建立一個(gè)MessageFormat對(duì)象,將格式字符串作為構(gòu)造器的參數(shù);使用MessageFormat的setLocale()方法設(shè)置它的Locale屬性;建立一個(gè)用于取代占位符的對(duì)象的數(shù)組,讓占位符中的數(shù)字和數(shù)組的索引匹配;調(diào)用MessageFormat的format()方法,將對(duì)象數(shù)組作為參數(shù)。示例 Stringpattern="{0},你好!今天是{1}"; MessageFormatmf=newMessageFormat(""); Localelocale=Locale.getDefault(); mf.setLocale(locale); mf.applyPattern(pattern); Datenow=newDate(); Object[]msgParams={args[0],now}; tv.setText(mf.format(msgParams));定義包含兩個(gè)占位符的模式套用模式取代占位符的對(duì)象數(shù)組格式化輸出示例可以將內(nèi)容定義到strings.xml中,且根據(jù)不同的locale,其內(nèi)容也有所區(qū)別:

22<?xmlversion="1.0"encoding="utf-8"?><resources><stringname="app_name">こんにちは,世界</string><stringname="hello">こんにちは{0},今日はです{1}</string></resources

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論