Fr3nch13/CakePHP Utilities

I18n
in package

I18n handles translation of Text and time format strings.

Table of Contents

Constants

DEFAULT_LOCALE  = 'en_US'
Default locale

Properties

$_collection  : TranslatorRegistry|null
The translators collection
$_defaultLocale  : string|null
The environment default locale

Methods

clear()  : void
Destroys all translator instances and creates a new empty translations collection.
config()  : void
Registers a callable object that can be used for creating new translator instances for the same translations domain. Loaders will be invoked whenever a translator object is requested for a domain that has not been configured or loaded already.
getDefaultFormatter()  : string
Returns the currently configured default formatter.
getDefaultLocale()  : string
Returns the default locale.
getLocale()  : string
Will return the currently configure locale as stored in the `intl.default_locale` PHP setting.
getTranslator()  : Translator
Returns an instance of a translator that was configured for the name and locale.
setDefaultFormatter()  : void
Sets the name of the default messages formatter to use for future translator instances. By default, the `default` and `sprintf` formatters are available.
setLocale()  : void
Sets the default locale to use for future translator instances.
setTranslator()  : void
Sets a translator.
translators()  : TranslatorRegistry
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.
useFallback()  : void
Set if the domain fallback is used.

Constants

DEFAULT_LOCALE

Default locale

public string DEFAULT_LOCALE = 'en_US'

Properties

$_defaultLocale

The environment default locale

protected static string|null $_defaultLocale

Methods

clear()

Destroys all translator instances and creates a new empty translations collection.

public static clear() : void

config()

Registers a callable object that can be used for creating new translator instances for the same translations domain. Loaders will be invoked whenever a translator object is requested for a domain that has not been configured or loaded already.

public static config(string $name, callable $loader) : void

Registering loaders is useful when you need to lazily use translations in multiple different locales for the same domain, and don't want to use the built-in translation service based of gettext files.

Loader objects will receive two arguments: The domain name that needs to be built, and the locale that is requested. These objects can assemble the messages from any source, but must return an Cake\I18n\Package object.

Example:

 use Cake\I18n\MessagesFileLoader;
 I18n::config('my_domain', function ($name, $locale) {
     // Load resources/locales/$locale/filename.po
     $fileLoader = new MessagesFileLoader('filename', $locale, 'po');
     return $fileLoader();
 });

You can also assemble the package object yourself:

 use Cake\I18n\Package;
 I18n::config('my_domain', function ($name, $locale) {
     $package = new Package('default');
     $messages = (...); // Fetch messages for locale from external service.
     $package->setMessages($message);
     $package->setFallback('default');
     return $package;
 });
Parameters
$name : string

The name of the translator to create a loader for

$loader : callable

A callable object that should return a Package instance to be used for assembling a new translator.

getDefaultFormatter()

Returns the currently configured default formatter.

public static getDefaultFormatter() : string
Return values
string

The name of the formatter.

getDefaultLocale()

Returns the default locale.

public static getDefaultLocale() : string

This returns the default locale before any modifications, i.e. the value as stored in the intl.default_locale PHP setting before any manipulation by this class.

Return values
string

getLocale()

Will return the currently configure locale as stored in the `intl.default_locale` PHP setting.

public static getLocale() : string
Return values
string

The name of the default locale.

getTranslator()

Returns an instance of a translator that was configured for the name and locale.

public static getTranslator([string $name = 'default' ][, string|null $locale = null ]) : Translator

If no locale is passed then it takes the value returned by the getLocale() method.

Parameters
$name : string = 'default'

The domain of the translation messages.

$locale : string|null = null

The locale for the translator.

Tags
throws
I18nException
Return values
Translator

The configured translator.

setDefaultFormatter()

Sets the name of the default messages formatter to use for future translator instances. By default, the `default` and `sprintf` formatters are available.

public static setDefaultFormatter(string $name) : void
Parameters
$name : string

The name of the formatter to use.

setLocale()

Sets the default locale to use for future translator instances.

public static setLocale(string $locale) : void

This also affects the intl.default_locale PHP setting.

Parameters
$locale : string

The name of the locale to set as default.

setTranslator()

Sets a translator.

public static setTranslator(string $name, callable $loader[, string|null $locale = null ]) : void

Configures future translators, this is achieved by passing a callable as the last argument of this function.

Example:

 I18n::setTranslator('default', function () {
     $package = new \Cake\I18n\Package();
     $package->setMessages([
         'Cake' => 'Gâteau'
     ]);
     return $package;
 }, 'fr_FR');

 $translator = I18n::getTranslator('default', 'fr_FR');
 echo $translator->translate('Cake');

You can also use the Cake\I18n\MessagesFileLoader class to load a specific file from a folder. For example for loading a my_translations.po file from the resources/locales/custom folder, you would do:

I18n::setTranslator(
 'default',
 new MessagesFileLoader('my_translations', 'custom', 'po'),
 'fr_FR'
);
Parameters
$name : string

The domain of the translation messages.

$loader : callable

A callback function or callable class responsible for constructing a translations package instance.

$locale : string|null = null

The locale for the translator.

translators()

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.

public static translators() : TranslatorRegistry
Return values
TranslatorRegistry

The translator collection.

useFallback()

Set if the domain fallback is used.

public static useFallback([bool $enable = true ]) : void
Parameters
$enable : bool = true

flag to enable or disable fallback


        
On this page

Search results