I18n handles translation of Text and time format strings.
string
'en_US'
Default locale
Cake\I18n\TranslatorRegistry|null
The translators collection
string|null
The environment default locale
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.
Returns the currently configured default formatter.
Returns the default locale.
Will return the currently configure locale as stored in the intl.default_locale
PHP setting.
Returns an instance of a translator that was configured for the name and locale.
Sets the name of the default messages formatter to use for future translator instances. By default, the default
and sprintf
formatters are available.
Sets the default locale to use for future translator instances. This also affects the intl.default_locale
PHP setting.
Sets a translator.
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.
Set if the domain fallback is used.
clear(): void
Destroys all translator instances and creates a new empty translations collection.
void
config(string $name, callable $loader): 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.
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.
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; });
string
$name The name of the translator to create a loader for
callable
$loader A callable object that should return a Package instance to be used for assembling a new translator.
void
getDefaultFormatter(): string
Returns the currently configured default formatter.
string
getDefaultLocale(): string
Returns the default locale.
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.
string
getLocale(): string
Will return the currently configure locale as stored in the intl.default_locale
PHP setting.
string
getTranslator(string $name = 'default', string|null $locale = null): Cake\I18n\Translator
Returns an instance of a translator that was configured for the name and locale.
If no locale is passed then it takes the value returned by the getLocale()
method.
string
$name optional The domain of the translation messages.
string|null
$locale optional The locale for the translator.
Cake\I18n\Translator
Cake\I18n\Exception\I18nException
setDefaultFormatter(string $name): void
Sets the name of the default messages formatter to use for future translator instances. By default, the default
and sprintf
formatters are available.
string
$name The name of the formatter to use.
void
setLocale(string $locale): void
Sets the default locale to use for future translator instances. This also affects the intl.default_locale
PHP setting.
string
$locale The name of the locale to set as default.
void
setTranslator(string $name, callable $loader, string|null $locale = null): void
Sets a translator.
Configures future translators, this is achieved by passing a callable as the last argument of this function.
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' );
string
$name The domain of the translation messages.
callable
$loader A callback function or callable class responsible for constructing a translations package instance.
string|null
$locale optional The locale for the translator.
void
translators(): Cake\I18n\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.
Cake\I18n\TranslatorRegistry
useFallback(bool $enable = true): void
Set if the domain fallback is used.
bool
$enable optional flag to enable or disable fallback
void
The translators collection
Cake\I18n\TranslatorRegistry|null
The environment default locale
string|null
© 2005–present 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/4.4/class-Cake.I18n.I18n.html