1. 程式人生 > >Android中XLIFF的應用

Android中XLIFF的應用

google的demo,發現部分資源字串在/res/values/string.xml中包含有類似xliff的節點,剛才查了一下,Xliff是XML Localization Interchange File Format 的縮寫,中文名為XML本地化資料交換格式。

對於在Android的資源字串中,可能會有類似下面的 <xliff:g id="FILE_NAME">%1$s</xliff:g> 寫法,這裡,id我們可以隨便定義,後面的%1$s的1%表示這是第一個可替換量,s表示字串

詳細的介紹如下:

屬性id可以隨便命名 屬性example表示舉例說明,可以省略 %n$ms:代表輸出的是字串,n代表是第幾個引數,設定m的值可以在輸出之前放置空格 

%n$md:代表輸出的是整數,n代表是第幾個引數,設定m的值可以在輸出之前放置空格,也可以設為0m,在輸出之前放置m個0 %n$mf:代表輸出的是浮點數,n代表是第幾個引數,設定m的值可以控制小數位數,如m=2.2時,輸出格式為00.00

例如一個String中有多個需要替換的變數,可以在xml中定義如下變數: 

   <string name="info">
your name is <xliff:g id="NAME">%1$s</xliff:g>, and your age is
<xliff:g id="AGE">%2$s</xliff:g>
   </string>

程式中動態載入:

TextView tv = (TextView) findViewById(R.id.textView);

        String info = getResources().getString(R.string.info,"jnhoodlum","22");

        tv.setText(info);

最後要注意一點:在String裡要增加XLIFF的 xmlns:

<resources xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">