PHP code example of wilycode / translation-languages

1. Go to this page and download the library: Download wilycode/translation-languages 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/ */

    

wilycode / translation-languages example snippets




ilyCode\Translation\Language\Pl;

$polish = new Pl();

echo $polish->getCode();                // pl
echo $polish->getNumberOfPluralForms(); // 3
echo $polish->getPluralForm(1);        // 0
echo $polish->getPluralForm(2);        // 1
echo $polish->getPluralForm(5);        // 2



ilyCode\Translation\DateStyle;
use WilyCode\Translation\LocaleFormatter\LocaleFormatterFactory;

$factory = new LocaleFormatterFactory(
    __DIR__ . '/vendor/wilycode/translation-languages/src/resources/locales',
    'en-US',
);

$formatter = $factory->forCode('pl-PL');
$date = new DateTimeImmutable('2026-07-08');

echo $formatter->formatDate($date);                  // 08.07.2026
echo $formatter->formatDate($date, DateStyle::Long); // 8 lipca 2026 r.
echo $formatter->formatNumber(1234.5);              // 1 234,50
echo $formatter->formatMoney(1234.5);               // PLN 1 234,50

namespace App\Translation\Language;



pp\Translation\Language\Pl;
use WilyCode\Translation\DateStyle;
use WilyCode\Translation\LocaleFormatter\LocaleFormatterFactory;

$language = new Pl();
$factory = new LocaleFormatterFactory(__DIR__ . '/resources/locales', 'pl-PL');
$formatter = $factory->forCode('pl-PL');

echo $language->getPluralForm(2); // 1
echo $formatter->formatDate(
    new DateTimeImmutable('2026-07-08'),
    DateStyle::Long,
); // 8 lipca 2026 r.

use WilyCode\Translation\Language\Ar;
use WilyCode\Translation\Language\Ja;
use WilyCode\Translation\Language\Pt;
use WilyCode\Translation\Language\PtBr;

(new Ar())->getPluralForm(0);    // 0 (zero)
(new Ar())->getPluralForm(2);    // 2 (two)
(new Ja())->getPluralForm(5);    // 0 (only form)
(new Pt())->getPluralForm(0);    // 2 (Portugal: other)
(new PtBr())->getPluralForm(0);  // 0 (Brazil: one)

use WilyCode\Translation\DateStyle;
use WilyCode\Translation\LocaleFormatter\LocaleFormatterFactory;

$factory = new LocaleFormatterFactory(
    __DIR__ . '/vendor/wilycode/translation-languages/src/resources/locales',
    'en-US',
);

$date = new DateTimeImmutable('2026-07-08');

$factory->forCode('en-US')->formatDate($date);                // 07/08/2026
$factory->forCode('pl-PL')->formatDate($date);                // 08.07.2026
$factory->forCode('pl-PL')->formatDate($date, DateStyle::Short); // 2026-07-08
$factory->forCode('pl-PL')->formatDate($date, DateStyle::Long);  // 8 lipca 2026 r.

use WilyCode\Translation\LocaleFormatter\LocaleFormatterFactory;

$factory = new LocaleFormatterFactory(__DIR__ . '/resources/locales', 'en-US');
$polish = $factory->forCode('pl-PL');

use App\Language\Id;
use WilyCode\Translation\LocaleFormatter\LocaleFormatterFactory;

$language = new Id();
$factory = new LocaleFormatterFactory(__DIR__ . '/resources/locales', 'id-ID');

$language->getPluralForm(10);            // 0
$factory->forCode('id-ID')->formatMoney(1234.5); // IDR 1.235
text
app/
└── Translation/
    └── Language/
        └── Pl.php
resources/
└── locales/
    └── pl-pl.php
text
resources/
└── locales/
    ├── en-us.php
    └── pl-pl.php