I18n handles translation of Text and time format strings.
string
'en_US'
string
Destroys all translator instances and creates a new empty translations collection.
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.
Sets the name of the default messages formatter to use for future translator instances. By default the default
and sprintf
formatters are available.
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.
Sets the default locale to use for future translator instances. This also affects the intl.default_locale
PHP setting.
Returns an instance of a translator that was configured for the name and passed locale. If no locale is passed then it takes the value returned by the locale()
method.
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.
clear( )
Destroys all translator instances and creates a new empty translations collection.
config( string $name , callable $loader )
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.
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 Aura\Intl\Package
object.
use Cake\I18n\MessagesFileLoader; I18n::config('my_domain', function ($name, $locale) { // Load src/Locale/$locale/filename.po $fileLoader = new MessagesFileLoader('filename', $locale, 'po'); return $fileLoader(); });
You can also assemble the package object yourself:
use Aura\Intl\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; });
$name
$loader
A callable object that should return a Package instance to be used for assembling a new translator.
defaultFormatter( string|null $name null )
Sets the name of the default messages formatter to use for future translator instances. By default the default
and sprintf
formatters are available.
If called with no arguments, it will return the currently configured value.
$name
optional null defaultLocale( )
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.
locale( string|null $locale null )
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 locale as stored in the intl.default_locale
PHP setting.
$locale
optional null translator( string $name 'default' , string|null $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 then it takes the value returned by the locale()
method.
This method can be used to configure future translators, this is achieved by passing a callable as the last argument of this function.
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');
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 src/Locale/custom
folder, you would do:
I18n::translator( 'default', 'fr_FR', new MessagesFileLoader('my_translations', 'custom', 'po'); );
$name
optional 'default' $locale
optional null $loader
optional null A callback function or callable class responsible for constructing a translations package instance.
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.
Cake\I18n\TranslatorRegistry
useFallback( boolean $enable true )
Set if the domain fallback is used.
$enable
optional true protected static Cake\I18n\TranslatorRegistry
|null
The translators collection
null
© 2005–2017 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.4/class-Cake.I18n.I18n.html