tohokuaikiのチラシの裏

技術的ネタとか。

Confluenceのプラグイン作成における国際化

設置するファイル

言語ファイルは、I18N Architecture - Confluence Development - Atlassian Developer Documentationにあるように各ロケールごとに作成する。
サンプルとして、

/com/atlassian/confluence/core/ConfluenceActionSupport_.properties
/com/atlassian/confluence/core/ConfluenceActionSupport_.properties
/com/atlassian/confluence/core/ConfluenceActionSupport_.properties

が挙げられているが、variantというのは方言であまり無いので、日本語だと2行目のを使って
/com/atlassian/confluence/core/ConfluenceActionSupport_ja_JP.properties
ファイルを用意する。プラグインの場合は、

src/main/resources/i18n/

に設置する。

ただし、マルチバイトの場合はUTF-8のものをUnicodeしなければいけないので注意。たとえば、「名前」は「\u540d\u524d」にしなければいけない。ツールとしてプロパティエディタなどが便利。

実際に使う場合

Velocityテンプレート
$i18n.getText("admin.name.label")
Java
#beanが設定してくれている場合は、行き成りgetTextで
i18NBeanFactory.getI18NBean().getText("admin.name.label")
# 自前のClassとかではGeneralUtilを使う
com.atlassian.confluence.util.GeneralUtil.getI18n().getText("admin.name.label")
JavaScript
AJS.I18n.getText("admin.name.label")

引数を使いたい場合

たとえば、Confluenceのソース
./confluence-project/confluence-core/confluence/src/etc/java/com/atlassian/confluence/core/ConfluenceActionSupport.properties
には、

unknown.name=Unknown User ({0})

というのがある。この場合、

i18NBeanFactory.getI18NBean().getText("unknown.name", new Object[] {userName});
# 複数引数の場合
i18NBeanFactory.getI18NBean().getText("unknown.name", new Object[] {userName, additionalError});

のようにして、引数を採ってやる。