PHP code example of wilycode / translation

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




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

$formatter = new LocaleFormatter([
    'locale' => 'pl-PL',
    'date' => [
        'formats' => [
            'short' => 'Y-m-d',
            'medium' => 'd.m.Y',
            'long' => 'j.m.Y',
        ],
    ],
    'number' => [
        'decimal_separator' => ',',
        'thousands_separator' => ' ',
        'currency' => 'PLN',
        'fraction_digits' => 2,
    ],
]);

$date = new DateTimeImmutable('2026-10-14');

echo $formatter->formatDate($date);              // 14.10.2026
echo $formatter->formatDate($date, DateStyle::Short); // 2026-10-14
echo $formatter->formatMoney(1234.5);            // PLN 1 234,50

use WilyCode\Translation\LocaleFormatter\LocaleFormatterFactory;

$factory = new LocaleFormatterFactory(
    __DIR__ . '/resources/locales',
    'pl-PL',
);

$formatter = $factory->forCode('pl-PL');

use WilyCode\Translation\TranslatorInterface;

final readonly class WelcomeService
{
    public function __construct(
        private TranslatorInterface $translator,
    ) {}

    public function message(string $name): string
    {
        return $this->translator->translate(
            'messages',
            'welcome',
            ['name' => $name],
        );
    }
}

use WilyCode\Translation\TranslatorInterface;

function itemSummary(TranslatorInterface $translator, int $count): string
{
    return $translator->translatePlural(
        'shop',
        'items',
        $count,
        ['count' => $count],
    );
}



return [
    'locale' => 'en-US',
    'date' => [
        'formats' => [
            'short' => 'Y-m-d',
            'medium' => 'm/d/Y',
            'long' => 'F j, Y',
        ],
        // Optional: 'long_month_case' => 'genitive' or 'nom',
        // Optional: 'months_genitive' => [1 => '...', /* ... */],
        // Optional: 'months_nominative' => [1 => '...', /* ... */],
    ],
    'time' => [
        'formats' => ['short' => 'H:i', 'medium' => 'H:i:s', 'long' => 'H:i:s'],
    ],
    'number' => [
        'decimal_separator' => '.',
        'thousands_separator' => ',',
        'currency' => 'USD',
        'fraction_digits' => 2,
        // Optional: 'grouping' => [3, 2], // Indian-style digit grouping.
    ],
];

use WilyCode\Translation\LocaleFormatter\LocaleFormatterFactory;

$factory = new LocaleFormatterFactory(__DIR__ . '/resources/locales', 'en-US');

$pl = $factory->forCode('pl-PL');  // pl-pl.php
$en = $factory->forCode('EN_us');  // en-us.php
$fallback = $factory->forCode('xx'); // en-us.php

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

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

$language->getPluralForm(2);            // 1
$formatter->formatMoney(1234.5);       // PLN 1 234,50

use WilyCode\Translation\MessageFormatter;

MessageFormatter::format('Hello, {name}!', ['name' => 'Ada']); // 'Hello, Ada!'
MessageFormatter::format('Hello, {name}!', []); // 'Hello, {name}!'
text
resources/
└── locales/
    ├── pl-pl.php
    ├── en-us.php
    └── pl.php       # Optional alias: return