このエントリーをはてなブックマークに追加

2010年2月21日日曜日

XLIFF enables you to replacement characters(1)

[In English]
Do you know XLIFF?

The purpose of XLIFF is to make localization process easier.
XLIFF can be used in XML.

The rudimentary way to use is as follows.

At first, we need to declare to use xliff.


<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">


And second, use xliff like this.

<string name="remaining_time_message">Only <xliff:g id="minutes">%d</xliff:g> minutes left.</string>


At the end replace it.

getResources().getString(R.string.remaining_time_message, 10);


Like this way we can create the dynamic message from xml.
But wait a minute. Yes I know how you feel. You might feel that there is easier way than that.

1.

<string name="remaining_time_message">Only %d minutes left.</string>


2.

String.format(getResources().getString(R.string.remaining_time_message, 10);


Yes, the result of the both of them is exactly the same.

The former has a number of advantages. But I'll answer it later.
So much for today.



[In Japanese]

XLIFFをご存知ですか?
XLIFFとはローカライズ作業を簡略化してくれる仕組みで、XML中で使用する事ができます。

基本的な使い方は以下の通りです。

まず始めに、XLIFFを使用するという事を宣言します。

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">



次に、XML中でXLIFFを使用した記述をします。

<string name="remaining_time_message">Only <xliff:g id="minutes">%d</xliff:g> minutes left.</string>



最後に、XML中のXLIFFで記述された部分を置換します。

getResources().getString(R.string.remaining_time_message, 10);



このようにして動的なメッセージを生成する事ができます。
しかし、もっと簡単な方法があると感じた方もいらっしゃるのではないでしょうか。
そうです、XLIFFを使わずとも、以下のようにするともっと簡単に実現できます。
1.

<string name="remaining_time_message">Only %d minutes left.</string>


2.

String.format(getResources().getString(R.string.remaining_time_message, 10);


両者の違いはなんでしょうか?実はXLIFFを用いた方がメリットが大きいんです。
そのメリットは次回に書くことにします。
お楽しみに。

0 件のコメント:

コメントを投稿