germania-kg / i18n
Outsourced i18n stuff
2.1.0
2023-01-04 08:44 UTC
Requires
- php: ^7.4|^8.0
- pimple/pimple: ^3.0
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
- psr/log: ^1.0
- willdurand/negotiation: ^2.3|^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- php-coveralls/php-coveralls: ^2.0
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^8.0|^9.0
- spatie/phpunit-watcher: ^1.0
This package is auto-updated.
Last update: 2024-11-20 00:01:15 UTC
README
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
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