piko/i18n

A minimal internationalization component that can be used in a piko application or standalone.

v2.1 2022-11-13 21:40 UTC

This package is auto-updated.

Last update: 2024-10-14 02:02:27 UTC


README

build Coverage Status

A minimal internationalization component which can be used in a piko application or standalone.

Installation

It's recommended that you use Composer to install Piko I18n.

composer require piko/i18n

Usage

In order to use the I18n component, translations have to be stored in PHP files that return a key-value pair array of translations. Keys are strings to translate and values are corresponding translated strings.

Example of translation file fr.php :

return [
    'Translation test' => 'Test de traduction',
    'Hello {name}' => 'Bonjour {name}',
];

Application structure example:

App root
  |__messages
    |__fr.php
  |__index.php

Usage in a piko application

index.php :

use Piko\Application;
use function Piko\I18n\__;

require('vendor/autoload.php');

$config = [
    'basePath' => __DIR__,
    'components' => [
        'Piko\I18n' => [
            'translations' => [
                'app' => '@app/messages',
            ],
            'language' => 'fr'
        ],
    ],
];

$app = new Application($config);

$i18n = $app->getComponent('Piko\I18n');

echo $i18n->translate('app', 'Translation test') . '<br>'; // Test de traduction
echo $i18n->translate('app', 'Hello {name}', ['name' => 'John']) . '<br>' ; // Bonjour John

// Using the proxy function __() :
echo __('app', 'Translation test') . '<br>'; // Test de traduction
echo __('app', 'Hello {name}', ['name' => 'John']) . '<br>' ;  // Bonjour John

Usage in a standalone script

use Piko\I18n;
use function Piko\I18n\__;

require('vendor/autoload.php');

$i18n = new I18n(['app' => __DIR__ . '/messages'], 'fr');

echo $i18n->translate('app', 'Translation test') . '<br>';
echo $i18n->translate('app', 'Hello {name}', ['name' => 'John']) . '<br>' ;

// Using the proxy function __() :

echo __('app', 'Translation test') . '<br>';
echo __('app', 'Hello {name}', ['name' => 'John']) . '<br>' ;