A minimal internationalization component which can be used in a piko application or standalone.
It's recommended that you use Composer to install Piko I18n.
composer require piko/i18n
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
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
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>' ;