koded/i18n

Simple internationalization library with support for .po and .php translation files

0.9.2 2023-10-17 13:58 UTC

This package is auto-updated.

Last update: 2024-10-23 00:53:59 UTC


README

CI Latest Stable Version Minimum PHP Version: 8.1

A simple internationalization library with support for .po and .php translation files.

composer require koded/i18n

Requirements

  • intl
  • gettext

Usage

__(string $message, array $arguments => [], string $locale = '');

Configuration

The I18n class is static and should be configured somewhere in the beginning of your PHP code/script.

use Koded\Stdlib\Config;
use Koded\I18n\{ArrayCatalog, DefaultFormatter, I18n, I18nCatalog};

// all conf directives are optional
$config = new Config([
    'translation.catalog' => ArrayCatalog::class,
    'translation.formatter' => DefaultFormatter::class,
    'translation.dir' => '/path/to/your/translation/files',
    'translation.locale' => ['mk_MK', 'de_DE', 'it_IT'],
]);

I18n::register(I18nCatalog::new($config));

Translation files

.po files

poedit is a very good app to work with your translation files.

.php files

The translation file has a simple structure

return [
    'language' => '',
    'messages' => [
        // your translation strings goes here
        'original string' => 'translated string',
    ]
];

where messages contains key => value pairs for the translated strings.

Code quality

Code Coverage Scrutinizer Code Quality

vendor/bin/phpunit

License

Software license

The code is distributed under the terms of The 3-Clause BSD license.