https://github.com/piko-framework/i18n
A minimal internationalization component which can be used in a piko application or standalone.
https://github.com/piko-framework/i18n
Last synced: 9 months ago
JSON representation
A minimal internationalization component which can be used in a piko application or standalone.
- Host: GitHub
- URL: https://github.com/piko-framework/i18n
- Owner: piko-framework
- License: lgpl-3.0
- Created: 2022-10-04T10:44:44.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-11-13T21:43:15.000Z (over 3 years ago)
- Last Synced: 2025-08-14T05:24:38.577Z (11 months ago)
- Language: PHP
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Piko I18n
[](https://github.com/piko-framework/i18n/actions/workflows/php.yml)
[](https://coveralls.io/github/piko-framework/i18n?branch=main)
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.
```bash
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* :
```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 :
```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') . '
'; // Test de traduction
echo $i18n->translate('app', 'Hello {name}', ['name' => 'John']) . '
' ; // Bonjour John
// Using the proxy function __() :
echo __('app', 'Translation test') . '
'; // Test de traduction
echo __('app', 'Hello {name}', ['name' => 'John']) . '
' ; // Bonjour John
```
## Usage in a standalone script
```php
use Piko\I18n;
use function Piko\I18n\__;
require('vendor/autoload.php');
$i18n = new I18n(['app' => __DIR__ . '/messages'], 'fr');
echo $i18n->translate('app', 'Translation test') . '
';
echo $i18n->translate('app', 'Hello {name}', ['name' => 'John']) . '
' ;
// Using the proxy function __() :
echo __('app', 'Translation test') . '
';
echo __('app', 'Hello {name}', ['name' => 'John']) . '
' ;
```