翻訳に intl を使用した方法を次に示します (php v. 5.3.10 および 5.4.7 でテスト済み)。
intl.php
namespace Example;
class Intl {
private $resource;
public function __construct() {
$bundle_location = "./locales";
$user_locale = \Locale::acceptFromHttp( $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
$this->resource = new \ResourceBundle( $user_locale, $bundle_location );
}
...
display.php
use Example\Intl;
$intl = new Intl();
$r = $intl->resource;
echo $r['string_to_be_translated'];
...
リソースバンドル
locales
ディレクトリには、リソース ファイルがあります。
root.res - ルート言語 (英語)
root {
string_to_be_translated {String to be translated }
}
ja.res - 日本語
ja {
string_to_be_translated {\u5909\u63DB\u3055\u308C\u308B\u6587\u5B57\u5217 }
}
sp.res - スペイン語
sp {
string_to_be_translated {Cadena a ser traducido }
}
(等)