Updated gettext/gettext package to v5

This commit is contained in:
Igor Scheller 2020-04-20 02:02:26 +02:00 committed by msquare
parent 351fc1f749
commit d455b95f5d
4 changed files with 45 additions and 42 deletions

View File

@ -27,7 +27,8 @@
"ext-xml": "*", "ext-xml": "*",
"doctrine/dbal": "^2.9", "doctrine/dbal": "^2.9",
"erusev/parsedown": "^1.7", "erusev/parsedown": "^1.7",
"gettext/gettext": "^4.6", "gettext/gettext": "^5.4",
"gettext/translator": "^1.0",
"guzzlehttp/guzzle": "^6.3", "guzzlehttp/guzzle": "^6.3",
"illuminate/container": "5.8.*", "illuminate/container": "5.8.*",
"illuminate/database": "5.8.*", "illuminate/database": "5.8.*",

View File

@ -7,33 +7,38 @@ use Gettext\Translator;
class GettextTranslator extends Translator class GettextTranslator extends Translator
{ {
/** /**
* @param string $domain * @param string|null $domain
* @param string $context * @param string|null $context
* @param string $original * @param string $original
* @return string * @return string
* @throws TranslationNotFound * @throws TranslationNotFound
*/ */
public function dpgettext($domain, $context, $original) protected function translate(?string $domain, ?string $context, string $original): string
{ {
$this->assertHasTranslation($domain, $context, $original); $this->assertHasTranslation($domain, $context, $original);
return parent::dpgettext($domain, $context, $original); return parent::translate($domain, $context, $original);
} }
/** /**
* @param string $domain * @param string|null $domain
* @param string $context * @param string|null $context
* @param string $original * @param string $original
* @param string $plural * @param string $plural
* @param string $value * @param int $value
* @return string * @return string
* @throws TranslationNotFound * @throws TranslationNotFound
*/ */
public function dnpgettext($domain, $context, $original, $plural, $value) protected function translatePlural(
{ ?string $domain,
?string $context,
string $original,
string $plural,
int $value
): string {
$this->assertHasTranslation($domain, $context, $original); $this->assertHasTranslation($domain, $context, $original);
return parent::dnpgettext($domain, $context, $original, $plural, $value); return parent::translatePlural($domain, $context, $original, $plural, $value);
} }
/** /**

View File

@ -4,6 +4,8 @@ namespace Engelsystem\Helpers\Translation;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Container\ServiceProvider; use Engelsystem\Container\ServiceProvider;
use Gettext\Loader\MoLoader;
use Gettext\Loader\PoLoader;
use Gettext\Translations; use Gettext\Translations;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Session\Session;
@ -72,22 +74,22 @@ class TranslationServiceProvider extends ServiceProvider
if (!isset($this->translators[$locale])) { if (!isset($this->translators[$locale])) {
$names = ['default', 'additional']; $names = ['default', 'additional'];
/** @var GettextTranslator $translator */
$translator = $this->app->make(GettextTranslator::class);
/** @var Translations $translations */ /** @var Translations $translations */
$translations = $this->app->make(Translations::class); $translations = $this->app->call([Translations::class, 'create']);
foreach ($names as $name) { foreach ($names as $name) {
$file = $this->getFile($locale, $name); $file = $this->getFile($locale, $name);
if (Str::endsWith($file, '.mo')) { if (Str::endsWith($file, '.mo')) {
$translations->addFromMoFile($file); /** @var MoLoader $loader */
$loader = $this->app->make(MoLoader::class);
} else { } else {
$translations->addFromPoFile($file); /** @var PoLoader $loader */
$loader = $this->app->make(PoLoader::class);
} }
$translations = $loader->loadFile($file, $translations);
} }
$translator->loadTranslations($translations); $translator = GettextTranslator::createFromTranslations($translations);
$this->translators[$locale] = $translator; $this->translators[$locale] = $translator;
} }

View File

@ -16,9 +16,7 @@ class GettextTranslatorTest extends ServiceProviderTest
public function testNoTranslation() public function testNoTranslation()
{ {
$translations = $this->getTranslations(); $translations = $this->getTranslations();
$translator = GettextTranslator::createFromTranslations($translations);
$translator = new GettextTranslator();
$translator->loadTranslations($translations);
$this->assertEquals('Translation!', $translator->gettext('test.value')); $this->assertEquals('Translation!', $translator->gettext('test.value'));
@ -29,29 +27,25 @@ class GettextTranslatorTest extends ServiceProviderTest
} }
/** /**
* @covers \Engelsystem\Helpers\Translation\GettextTranslator::dpgettext() * @covers \Engelsystem\Helpers\Translation\GettextTranslator::translate()
*/ */
public function testDpgettext() public function testTranslate()
{ {
$translations = $this->getTranslations(); $translations = $this->getTranslations();
$translator = GettextTranslator::createFromTranslations($translations);
$translator = new GettextTranslator(); $this->assertEquals('Translation!', $translator->gettext('test.value'));
$translator->loadTranslations($translations);
$this->assertEquals('Translation!', $translator->dpgettext(null, null, 'test.value'));
} }
/** /**
* @covers \Engelsystem\Helpers\Translation\GettextTranslator::dnpgettext() * @covers \Engelsystem\Helpers\Translation\GettextTranslator::translatePlural
*/ */
public function testDnpgettext() public function testTranslatePlural()
{ {
$translations = $this->getTranslations(); $translations = $this->getTranslations();
$translator = GettextTranslator::createFromTranslations($translations);
$translator = new GettextTranslator(); $this->assertEquals('Translations!', $translator->ngettext('test.value', 'test.value', 2));
$translator->loadTranslations($translations);
$this->assertEquals('Translations!', $translator->dnpgettext(null, null, 'test.value', 'test.values', 2));
} }
/** /**
@ -59,11 +53,12 @@ class GettextTranslatorTest extends ServiceProviderTest
*/ */
protected function getTranslations(): Translations protected function getTranslations(): Translations
{ {
$translations = new Translations(); $translation = Translation::create(null, 'test.value')
$translations[] = ->translate('Translation!')
(new Translation(null, 'test.value', 'test.values')) ->translatePlural('Translations!');
->setTranslation('Translation!')
->setPluralTranslations(['Translations!']); $translations = Translations::create();
$translations->add($translation);
return $translations; return $translations;
} }