PHP code example of delight-im / i18n

1. Go to this page and download the library: Download delight-im/i18n library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

delight-im / i18n example snippets


     

$i18n = new \Delight\I18n\I18n([
    \Delight\I18n\Codes::EN_US,
    \Delight\I18n\Codes::DA_DK,
    \Delight\I18n\Codes::ES,
    \Delight\I18n\Codes::ES_AR,
    \Delight\I18n\Codes::KO,
    \Delight\I18n\Codes::KO_KR,
    \Delight\I18n\Codes::RU_RU,
    \Delight\I18n\Codes::SW
]);

$i18n->setDirectory(__DIR__ . '/../translations');

$i18n->setModule('messages');

$i18n->setLocaleAutomatically();

try {
    $i18n->setLocaleManually('es-AR');
}
catch (\Delight\I18n\Throwable\LocaleNotSupportedException $e) {
    die('The locale requested by the user is not supported');
}

function _f($text, ...$replacements) { global $i18n; return $i18n->translateFormatted($text, ...$replacements); }

function _fe($text, ...$replacements) { global $i18n; return $i18n->translateFormattedExtended($text, ...$replacements); }

function _p($text, $alternative, $count) { global $i18n; return $i18n->translatePlural($text, $alternative, $count); }

function _pf($text, $alternative, $count, ...$replacements) { global $i18n; return $i18n->translatePluralFormatted($text, $alternative, $count, ...$replacements); }

function _pfe($text, $alternative, $count, ...$replacements) { global $i18n; return $i18n->translatePluralFormattedExtended($text, $alternative, $count, ...$replacements); }

function _c($text, $context) { global $i18n; return $i18n->translateWithContext($text, $context); }

function _m($text) { global $i18n; return $i18n->markForTranslation($text); }

_('Welcome to our online store!');
// Welcome to our online store!

_('Create account');
// Create account

_('You have been successfully logged out.');
// You have been successfully logged out.

_f('This is %1$s.', 'Bob');
// This is Bob.

_f('This is %1$d.', 3);
// This is 3.

_f('This is %1$05d.', 3);
// This is 00003.

_f('This is %1$ 5d.', 3);
// This is     3.
// This is ␣␣␣␣3.

_f('This is %1$+d.', 3);
// This is +3.

_f('This is %1$+06d.', 3);
// This is +00003.

_f('This is %1$+ 6d.', 3);
// This is     +3.
// This is ␣␣␣␣+3.

_f('This is %1$f.', 3.14);
// This is 3.140000.

_f('This is %1$012f.', 3.14);
// This is 00003.140000.

_f('This is %1$010.4f.', 3.14);
// This is 00003.1400.

_f('This is %1$ 12f.', 3.14);
// This is     3.140000.
// This is ␣␣␣␣3.140000.

_f('This is %1$ 10.4f.', 3.14);
// This is     3.1400.
// This is ␣␣␣␣3.1400.

_f('This is %1$+f.', 3.14);
// This is +3.140000.

_f('This is %1$+013f.', 3.14);
// This is +00003.140000.

_f('This is %1$+011.4f.', 3.14);
// This is +00003.1400.

_f('This is %1$+ 13f.', 3.14);
// This is     +3.140000.
// This is ␣␣␣␣+3.140000.

_f('This is %1$+ 11.4f.', 3.14);
// This is     +3.1400.
// This is ␣␣␣␣+3.1400.

_f('Hello %s!', 'Jane');
// Hello Jane!

_f('%1$s is %2$d years old.', 'John', 30);
// John is 30 years old.

_fe('This is {0}.', 'Bob');
// This is Bob.

_fe('This is {0, number}.', 1003.14);
// This is 1,003.14.

_fe('This is {0, number, percent}.', 0.42);
// This is 42%.

_fe('This is {0, date}.', -14182916);
// This is Jul 20, 1969.

_fe('This is {0, date, short}.', -14182916);
// This is 7/20/69.

_fe('This is {0, date, medium}.', -14182916);
// This is Jul 20, 1969.

_fe('This is {0, date, long}.', -14182916);
// This is July 20, 1969.

_fe('This is {0, date, full}.', -14182916);
// This is Sunday, July 20, 1969.

_fe('This is {0, time}.', -14182916);
// This is 1:18:04 PM.

_fe('This is {0, time, short}.', -14182916);
// This is 1:18 PM.

_fe('This is {0, time, medium}.', -14182916);
// This is 1:18:04 PM.

_fe('This is {0, time, long}.', -14182916);
// This is 1:18:04 PM GMT-7.

_fe('This is {0, time, full}.', -14182916);
// This is 1:18:04 PM GMT-07:00.

_fe('This is {0, spellout}.', 314159);
// This is three hundred fourteen thousand one hundred fifty-nine.

_fe('This is {0, ordinal}.', 314159);
// This is 314,159th.

_fe('Hello {0}!', 'Jane');
// Hello Jane!

_fe('{0} is {1, number} years old.', 'John', 30);
// John is 30 years old.

_p('cat', 'cats', 1);
// cat

_p('cat', 'cats', 2);
// cats

_p('cat', 'cats', 3);
// cats

_p('The file has been saved.', 'The files have been saved.', 1);
// The file has been saved.

_p('The file has been saved.', 'The files have been saved.', 2);
// The files have been saved.

_p('The file has been saved.', 'The files have been saved.', 3);
// The files have been saved.

_pf('There is %d monkey.', 'There are %d monkeys.', 0);
// There are 0 monkeys.

_pf('There is %d monkey.', 'There are %d monkeys.', 1);
// There is 1 monkey.

_pf('There is %d monkey.', 'There are %d monkeys.', 2);
// There are 2 monkeys.

_pf('There is %1$d monkey in %2$s.', 'There are %1$d monkeys in %2$s.', 3, 'Anytown');
// There are 3 monkeys in Anytown.

_pf('You have %d new message', 'You have %d new messages', 0);
// You have 0 new messages

_pf('You have %d new message', 'You have %d new messages', 1);
// You have 1 new message

_pf('You have %d new message', 'You have %d new messages', 32);
// You have 32 new messages

_pfe('There is {0, number} monkey.', 'There are {0, number} monkeys.', 0);
// There are 0 monkeys.

_pfe('There is {0, number} monkey.', 'There are {0, number} monkeys.', 1);
// There is 1 monkey.

_pfe('There is {0, number} monkey.', 'There are {0, number} monkeys.', 2);
// There are 2 monkeys.

_pfe('There is {0, number} monkey in {1}.', 'There are {0, number} monkeys in {1}.', 3, 'Anytown');
// There are 3 monkeys in Anytown.

_pfe('You have {0, number} new message', 'You have {0, number} new messages', 0);
// You have 0 new messages

_pfe('You have {0, number} new message', 'You have {0, number} new messages', 1);
// You have 1 new message

_pfe('You have {0, number} new message', 'You have {0, number} new messages', 32);
// You have 32 new messages

_c('Order', 'sorting');
// or
_c('Order', 'purchase');
// or
_c('Order', 'mathematics');
// or
_c('Order', 'classification');

_c('Address:', 'location');
// or
_c('Address:', 'www');
// or
_c('Address:', 'email');
// or
_c('Address:', 'letter');
// or
_c('Address:', 'speech');

_m('User');
// User

$text = 'User';
_($text);
// User

$i18n->getLocale();
// en-US

$i18n->getSystemLocale();
// en_US.utf8

$i18n->getLocaleName();
// English (United States)

$i18n->getLocaleName('fr-BE');
// French (Belgium)

\Delight\I18n\Locale::toName('nb-NO');
// Norwegian Bokmål (Norway)

$i18n->getNativeLocaleName();
// English (United States)

$i18n->getNativeLocaleName('fr-BE');
// français (Belgique)

\Delight\I18n\Locale::toNativeName('nb-NO');
// norsk bokmål (Norge)

\Delight\I18n\Locale::toEnglishName('nb-NO');
// Norwegian Bokmål (Norway)

$i18n->getLanguageName();
// English

$i18n->getLanguageName('fr-BE');
// French

\Delight\I18n\Locale::toLanguageName('nb-NO');
// Norwegian Bokmål

$i18n->getNativeLanguageName();
// English

$i18n->getNativeLanguageName('fr-BE');
// français

\Delight\I18n\Locale::toNativeLanguageName('nb-NO');
// norsk bokmål

\Delight\I18n\Locale::toEnglishLanguageName('nb-NO');
// Norwegian Bokmål

\Delight\I18n\Locale::toScriptName('nb-Latn-NO');
// Latin

\Delight\I18n\Locale::toNativeScriptName('nb-Latn-NO');
// latinsk

\Delight\I18n\Locale::toEnglishScriptName('nb-Latn-NO');
// Latin

\Delight\I18n\Locale::toRegionName('nb-NO');
// Norway

\Delight\I18n\Locale::toNativeRegionName('nb-NO');
// Norge

\Delight\I18n\Locale::toEnglishRegionName('nb-NO');
// Norway

\Delight\I18n\Locale::toLanguageCode('nb-Latn-NO');
// nb

\Delight\I18n\Locale::toScriptCode('nb-Latn-NO');
// Latn

\Delight\I18n\Locale::toRegionCode('nb-Latn-NO');
// NO

\Delight\I18n\Locale::isRtl('ur-PK');
// true

\Delight\I18n\Locale::isLtr('ln-CD');
// true