Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 多語系筆記:xliff 標籤與 plurals 單複數處理

做多國語系時,字串裡需要插入動態數值,或遇到「1 台車 / 3 台車」這種單複數差異,Android 提供了 <xliff:g> 與 <plurals> 兩套機制。

xliff:g 標籤

  • id:識別名稱,可自由命名。
  • example:舉例說明,可省略,但建議寫上,方便翻譯人員理解。
  • %n$ms:輸出字串,n 是第幾個參數,m 可控制輸出前補的空格數。
  • %n$md:輸出整數,m 設為 0m 時會在前方補 m 個 0。
  • %n$mf:輸出浮點數,m 控制小數位數,例如 m=2.2 時格式為 00.00。

標籤本身用來把可變動的片段包起來,例如 <xliff:g id="name">%s</xliff:g>,翻譯時就不會誤動這些佔位符。

plurals:處理單複數

當同一個詞在單數與複數下拼法不同時使用:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <plurals name="numberOfCars">
        <item quantity="one">%d car</item>
        <item quantity="other">%d cars</item>
    </plurals>
</resources>

quantity 決定採用哪一種拼法。程式碼中透過 getQuantityString 取值,第二個參數傳入實際數量,系統會自動挑選對應的字串:

// Get the number of cars
int count = getNumberOfCars();
Resources res = getResources();
// public String getQuantityString (int id, int quantity, Object... formatArgs)
String cars = res.getQuantityString(R.plurals.numberOfCars, count, count);
Copyright © Linux教程網 All Rights Reserved