Mark Scherer a2318a3bec Run sniffer to auto-fix default params into doc blocks. 10 years ago
..
Formatter ce3b97201d Avoding errors when there is no translation for the given plural form 10 years ago
Parser 59211333d3 Suppress notice errors in PO file parsing. 10 years ago
ChainMessagesLoader.php 4105c2dc5d Docblock Absolute Path 11 years ago
Date.php aaf9817849 Use the diffFormatter hook provided by chronos 10 years ago
DateFormatTrait.php a2318a3bec Run sniffer to auto-fix default params into doc blocks. 10 years ago
FrozenDate.php aaf9817849 Use the diffFormatter hook provided by chronos 10 years ago
FrozenTime.php a2318a3bec Run sniffer to auto-fix default params into doc blocks. 10 years ago
I18n.php 4c525a08af Do not mix void with other return types. 10 years ago
LICENSE.txt 0cd70a6f30 Adds license file and badges for I18n subtree split 10 years ago
MessagesFileLoader.php 2fb0343646 remove all reference to DS contant 10 years ago
Number.php 2fc2fa4d6a Merge branch 'master' into 3.1 10 years ago
PluralRules.php be845a3a01 Run phpcbf for PSR2 CS fixers 11 years ago
README.md 0cd70a6f30 Adds license file and badges for I18n subtree split 10 years ago
RelativeTimeFormatter.php c12162d8bc Fix RelativeTimeFormatter incorrectly formatting 'about a month ago'/'in about a month' 10 years ago
Time.php a2318a3bec Run sniffer to auto-fix default params into doc blocks. 10 years ago
TranslatorRegistry.php c8d3974a48 Always import classes. 10 years ago
composer.json ba30f67136 Merge branch 'master' into 3.2 10 years ago
functions.php d1abb79491 Fix __xn() and __dxn() so that plural forms are used. 10 years ago

README.md

Total Downloads License

CakePHP Internationalization Library

The I18n library provides a I18n service locator that can be used for setting the current locale, building translation bundles and translating messages.

Additionally, it provides the Time and Number classes which can be used to output dates, currencies and any numbers in the right format for the specified locale.

Usage

Internally, the I18n class uses Aura.Intl. Getting familiar with it will help you understand how to build and manipulate translation bundles, should you wish to create them manually instead of using the conventions this library uses.

Setting the Current Locale

use Cake\I18n\I18n;

I18n::locale('en_US');

Translating a Message

echo __(
    'Hi {0,string}, your balance on the {1,date} is {2,number,currency}',
    ['Charles', '2014-01-13 11:12:00', 1354.37]
);

// Returns
Hi Charles, your balance on the Jan 13, 2014, 11:12 AM is $ 1,354.37

Creating Your Own Translators

use Cake\I18n\I18n;
use Aura\Intl\Package;

I18n::translator('animals', 'fr_FR', function () {
    $package = new Package(
        'default', // The formatting strategy (ICU)
        'default', // The fallback domain
    );
    $package->setMessages([
        'Dog' => 'Chien',
        'Cat' => 'Chat',
        'Bird' => 'Oiseau'
        ...
    ]);

    return $package;
});

I18n::locale('fr_FR');
__d('animals', 'Dog'); // Returns "Chien"

Formatting Time

$time = Time::now();
echo $time; // shows '4/20/14, 10:10 PM' for the en-US locale

Formatting Numbers

echo Number::format(100100100);
echo Number::currency(123456.7890, 'EUR');
// outputs €123,456.79

Documentation

Please make sure you check the official I18n documentation.

The documentation for the Time class contains instructions on how to configure and output time strings for selected locales.

The documentation for the Number class shows how to use the Number class for displaying numbers in specific locales.