CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • ChainMessagesLoader
  • Date
  • FrozenDate
  • FrozenTime
  • I18n
  • MessagesFileLoader
  • Number
  • PluralRules
  • Time
  • TranslatorRegistry

Traits

  • DateFormatTrait

Class I18n

I18n handles translation of Text and time format strings.

Namespace: Cake\I18n
Location: I18n/I18n.php

Constants summary

  • string
    DEFAULT_LOCALE ¶
    'en_US'

Properties summary

  • $_collection protected static
    Cake\I18n\TranslatorRegistry|null
    The translators collection
  • $_defaultLocale protected static
    string
    The environment default locale

Method Summary

  • clear() public static

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

  • config() public static

    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.

  • defaultFormatter() public static deprecated

    Sets the name of the default messages formatter to use for future translator instances.

  • defaultLocale() public static deprecated

    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.

  • getDefaultFormatter() public static
    Returns the currently configured default formatter.
  • getDefaultLocale() public static
    Returns the default locale.
  • getLocale() public static

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

  • getTranslator() public static
    Returns an instance of a translator that was configured for the name and locale.
  • locale() public static deprecated

    Sets the default locale to use for future translator instances. This also affects the intl.default_locale PHP setting.

  • setDefaultFormatter() public static

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

  • setLocale() public static

    Sets the default locale to use for future translator instances. This also affects the intl.default_locale PHP setting.

  • setTranslator() public static
    Sets a translator.
  • translator() public static deprecated

    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 getLocale() method.

  • translators() public static

    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() public static
    Set if the domain fallback is used.

Method Detail

clear() public static ¶

clear( )

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

config() public static ¶

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.

Example:

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;
 });
Parameters
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.

defaultFormatter() public static deprecated ¶

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.

Deprecated
3.5 Use getDefaultFormatter() and setDefaultFormatter().
Parameters
string|null $name optional null
The name of the formatter to use.
Returns
string
The name of the formatter.

defaultLocale() public static deprecated ¶

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.

Deprecated
3.5 Use getDefaultLocale()
Returns
string

getDefaultFormatter() public static ¶

getDefaultFormatter( )

Returns the currently configured default formatter.

Returns
string
The name of the formatter.

getDefaultLocale() public static ¶

getDefaultLocale( )

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.

Returns
string

getLocale() public static ¶

getLocale( )

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

Returns
string
The name of the default locale.

getTranslator() public static ¶

getTranslator( string $name = 'default' , string|null $locale = null )

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.

Parameters
string $name optional 'default'
The domain of the translation messages.
string|null $locale optional null
The locale for the translator.
Returns
Aura\Intl\TranslatorInterface
The configured translator.
Throws
Aura\Intl\Exception

locale() public static deprecated ¶

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.

Deprecated
3.5 Use setLocale() and getLocale().
Parameters
string|null $locale optional null
The name of the locale to set as default.
Returns
string|null
The name of the default locale.

setDefaultFormatter() public static ¶

setDefaultFormatter( string $name )

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

Parameters
string $name
The name of the formatter to use.

setLocale() public static ¶

setLocale( string $locale )

Sets the default locale to use for future translator instances. This also affects the intl.default_locale PHP setting.

Parameters
string $locale
The name of the locale to set as default.

setTranslator() public static ¶

setTranslator( string $name , callable $loader , string|null $locale = null )

Sets a translator.

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

Example:

I18n::setTranslator('default', function () {
     $package = new \Aura\Intl\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 src/Locale/custom folder, you would do:

I18n::setTranslator(
 'default',
 new MessagesFileLoader('my_translations', 'custom', 'po'),
 'fr_FR'
);
Parameters
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 null
The locale for the translator.

translator() public static deprecated ¶

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 getLocale() 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::setTranslator('default', function () {
     $package = new \Aura\Intl\Package();
     $package->setMessages([
         'Cake' => 'Gâteau'
     ]);
     return $package;
 }, 'fr_FR');

 $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');
);
Deprecated
3.5 Use getTranslator() and setTranslator()
Parameters
string $name optional 'default'
The domain of the translation messages.
string|null $locale optional null
The locale for the translator.
callable $loader optional null

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

Returns
Aura\Intl\TranslatorInterface|null
The configured translator.
Throws
Aura\Intl\Exception

translators() public static ¶

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.

Returns
Cake\I18n\TranslatorRegistry
The translators collection.

useFallback() public static ¶

useFallback( boolean $enable = true )

Set if the domain fallback is used.

Parameters
boolean $enable optional true
flag to enable or disable fallback

Properties detail

$_collection ¶

protected static Cake\I18n\TranslatorRegistry|null

The translators collection

$_defaultLocale ¶

protected static string

The environment default locale

Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs