I\'m looking for a way to to do i18n and l10n.
I\'ve used gettext
before and it was good: I would simply create .mo files in different languages and ever
Here's how I used intl for translations (tested on php v. 5.3.10 and 5.4.7):
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 );
}
...
use Example\Intl;
$intl = new Intl();
$r = $intl->resource;
echo $r['string_to_be_translated'];
...
In the locales
directory, I have my resource files:
root.res - root language (English)
root {
string_to_be_translated {String to be translated }
}
ja.res - Japanese
ja {
string_to_be_translated {\u5909\u63DB\u3055\u308C\u308B\u6587\u5B57\u5217 }
}
sp.res - Spanish
sp {
string_to_be_translated {Cadena a ser traducido }
}
(etc)
You'd use gettext. Intl (like ICU underneath it) is for l10n, not loading translations.
That advise wasn't very truthful. The intl functions can be used in conjunction to gettext, not as replacement.
MessageFormatter is what people have in mind when they associate INTL with text translations. The examples suggest so. But in reality it's just a sprintf
on steroids. It injects numbers into existing strings. (I'm not even sure how the locale support is of any use there, as it just serves as internal switch.)