Outsourced i18n stuff

2.1.0 2023-01-04 08:44 UTC

This package is auto-updated.

Last update: 2024-12-20 00:12:12 UTC


README

Packagist PHP version Tests

Installation with Composer

$ composer require germania-kg/i18n

BC breaks from v1

Class DGettextRenderer: This callable now expects returned messages to enclose placeholders with curly braces.

Deprecated

These classes have been used in v1, developers are encouraged to not use them any longer.

  • Germania\i18n\ServiceProvider
  • Germania\i18n\AcceptedLanguageStringFactory

GettextMiddleware

<?php
use Germania\i18n\GettextMiddleware;

$locale = "en_GB";
$domains = ["app", "project"];
$path = "./locales";

$middleware = new GettextMiddleware($locale, $domains, $path);

LanguageNormalizer

<?php
use Germania\i18n\LanguageNormalizer;

$norm = new LanguageNormalizer;
$norm("de-de"); // "de_DE"

Translator

The constructor requires a client and and default language.

<?php
use Germania\i18n\Translator;

$t = new Translator("de", "en");

echo $t("Just a string, nothing to translate");
// "Just a string, nothing to translate"

$var = array(
  "de" => "Deutsch: Nur eine String-Variable",
  "en" => "English: Just a string variable"
);

echo $t($var);
// "Deutsch: Nur eine String-Variable"

echo $t($var, "en");
// "English: Just a string variable"

DGettextRenderer

Callable wrapper around dgettext for a given domain. Optionally expands at runtime variable placeholders given in second parameter:

<?php
use Germania\i18n\DGettextRenderer;

$domain = "app";
$dgr = new DGettextRenderer($domain);

echo $dgr("MsgId");
// "Some translated {placeholder} messages"

echo $dgr("MsgId", [
  "placeholder" => "success"
]);
// "Some translated success messages"

Issues

See full issues list.

Development

$ git clone https://github.com/GermaniaKG/i18n.git
$ cd i18n
$ composer install

Unit tests

Either copy phpunit.xml.dist to phpunit.xml and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:

$ composer test
# or
$ vendor/bin/phpunit