Skip to content

Commit

Permalink
Adding doc blocks to I18n
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 24, 2014
1 parent c0baae7 commit 96f66bf
Showing 1 changed file with 89 additions and 9 deletions.
98 changes: 89 additions & 9 deletions src/I18n/I18n.php
Expand Up @@ -28,10 +28,28 @@
*/
class I18n {

/**
* The translators collection
*
* @var \Aura\Int\TranslatorLocator
*/
protected static $_collection;

/**
* The name of the default formatter to use for newly created
* translators
*
* @var string
*/
protected static $_defaultFormatter = 'basic';

/**
* Returns the translators collection instance. It can be used
* for getting specific translators based of their name and locale
* or to configure some aspect of future translations that are not yet constructed.
*
* @return \Aura\Intl\TranslatorLocator The translators collection.
*/
public static function translators() {
if (static::$_collection !== null) {
return static::$_collection;
Expand All @@ -52,11 +70,39 @@ public static function translators() {
);
}

public static function translator($package = 'default', $locale = null, callable $loader = null) {
/**
* Returns an instance of a translator that was configured for the name and passed
* locale. If no locale is passed the it takes the value returned by the `defaultLocale()` method.
*
* This method can be used to configure future translators, this is achieved by passing a callable
* as the last argument of this function.
*
* ### Example:
*
* {{{
* I18n::translator('default', 'fr_FR', function() {
* $package = new \Aura\Intl\Package();
* $package->setMessages([
* 'Cake' => 'Gâteau'
* ]);
* return $package;
* });
*
* $translator = I18n::translator('default', 'fr_FR');
* echo $translator->translate('Cake');
* }}}
*
* @param string $name The domain of the translation messages.
* @param string $locale The locale for the translator.
* @param callable $loader A callback function or callable class responsible for
* constructing a translations package instance.
* @return \Aura\Intl\Translator The configured translator.
*/
public static function translator($name = 'default', $locale = null, callable $loader = null) {
if ($loader !== null) {
$packages = static::translators()->getPackages();
$locale = $locale ?: static::defaultLocale();
$packages->set($package, $locale, $loader);
$packages->set($name, $locale, $loader);
return;
}

Expand All @@ -68,9 +114,9 @@ public static function translator($package = 'default', $locale = null, callable
}

try {
$translator = $translators->get($package);
$translator = $translators->get($name);
} catch (LoadException $e) {
$translator = static::_fallbackTranslator($package, $locale);
$translator = static::_fallbackTranslator($name, $locale);
}

if (isset($currentLocale)) {
Expand All @@ -80,6 +126,16 @@ public static function translator($package = 'default', $locale = null, callable
return $translator;
}

/**
* Sets the default locale to use for future translator instances.
* This also affects the `intl.default_locale` php setting.
*
* When called with no arguments it will return the currently configure
* defaultLocale as stored in the `intl.default_locale` php setting.
*
* @param string $locale The name of the locale to set as default.
* @return string|null The name of the default locale.
*/
public static function defaultLocale($locale = null) {
if (!empty($locale)) {
ini_set('intl.default_locale', $locale);
Expand All @@ -97,6 +153,16 @@ public static function defaultLocale($locale = null) {
return $current;
}

/**
* Sets the name of the default messages formatter to use for future
* translator instances. By default the `basic` and `sprintf` formatters
* are available.
*
* If called with no arguments, it will return the currently configured value.
*
* @param string $name The name of the formatter to use.
* @return string The name of the formatter.
*/
public static function defaultFormatter($name = null) {
if ($name === null) {
return static::$_defaultFormatter;
Expand All @@ -105,14 +171,28 @@ public static function defaultFormatter($name = null) {
static::$_defaultFormatter = $name;
}

/**
* Destroys all translator instances and creates a new empty translations
* collection.
*
* @return void
*/
public static function clear() {
static::$_collection = null;
}

protected static function _fallbackTranslator($package, $locale) {
/**
* Returns a new translator instance for the given name and locale
* based of conventions.
*
* @param string $name The translation package name.
* @param string $locale The locale to create the translator for.
* @return \Aura\Intl\Translator
*/
protected static function _fallbackTranslator($name, $locale) {
$chain = new ChainMessagesLoader([
new MessagesFileLoader($package, $locale, 'mo'),
new MessagesFileLoader($package, $locale, 'po')
new MessagesFileLoader($name, $locale, 'mo'),
new MessagesFileLoader($name, $locale, 'po')
]);

if (static::$_defaultFormatter !== 'basic') {
Expand All @@ -124,8 +204,8 @@ protected static function _fallbackTranslator($package, $locale) {
};
}

static::translator($package, $locale, $chain);
return static::translators()->get($package);
static::translator($name, $locale, $chain);
return static::translators()->get($name);
}

}

0 comments on commit 96f66bf

Please sign in to comment.