diff --git a/composer.json b/composer.json index 94294231..ea5db6a4 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,8 @@ "ext-xml": "*", "doctrine/dbal": "^2.9", "erusev/parsedown": "^1.7", - "gettext/gettext": "^4.6", + "gettext/gettext": "^5.4", + "gettext/translator": "^1.0", "guzzlehttp/guzzle": "^6.3", "illuminate/container": "5.8.*", "illuminate/database": "5.8.*", diff --git a/src/Helpers/Translation/GettextTranslator.php b/src/Helpers/Translation/GettextTranslator.php index 7f2299e2..e91d7fa1 100644 --- a/src/Helpers/Translation/GettextTranslator.php +++ b/src/Helpers/Translation/GettextTranslator.php @@ -7,33 +7,38 @@ use Gettext\Translator; class GettextTranslator extends Translator { /** - * @param string $domain - * @param string $context - * @param string $original + * @param string|null $domain + * @param string|null $context + * @param string $original * @return string * @throws TranslationNotFound */ - public function dpgettext($domain, $context, $original) + protected function translate(?string $domain, ?string $context, string $original): string { $this->assertHasTranslation($domain, $context, $original); - return parent::dpgettext($domain, $context, $original); + return parent::translate($domain, $context, $original); } /** - * @param string $domain - * @param string $context - * @param string $original - * @param string $plural - * @param string $value + * @param string|null $domain + * @param string|null $context + * @param string $original + * @param string $plural + * @param int $value * @return string * @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); - return parent::dnpgettext($domain, $context, $original, $plural, $value); + return parent::translatePlural($domain, $context, $original, $plural, $value); } /** diff --git a/src/Helpers/Translation/TranslationServiceProvider.php b/src/Helpers/Translation/TranslationServiceProvider.php index 5534a1c3..0a9c41a6 100644 --- a/src/Helpers/Translation/TranslationServiceProvider.php +++ b/src/Helpers/Translation/TranslationServiceProvider.php @@ -4,6 +4,8 @@ namespace Engelsystem\Helpers\Translation; use Engelsystem\Config\Config; use Engelsystem\Container\ServiceProvider; +use Gettext\Loader\MoLoader; +use Gettext\Loader\PoLoader; use Gettext\Translations; use Illuminate\Support\Str; use Symfony\Component\HttpFoundation\Session\Session; @@ -72,22 +74,22 @@ class TranslationServiceProvider extends ServiceProvider if (!isset($this->translators[$locale])) { $names = ['default', 'additional']; - /** @var GettextTranslator $translator */ - $translator = $this->app->make(GettextTranslator::class); - /** @var Translations $translations */ - $translations = $this->app->make(Translations::class); + $translations = $this->app->call([Translations::class, 'create']); foreach ($names as $name) { $file = $this->getFile($locale, $name); if (Str::endsWith($file, '.mo')) { - $translations->addFromMoFile($file); + /** @var MoLoader $loader */ + $loader = $this->app->make(MoLoader::class); } else { - $translations->addFromPoFile($file); + /** @var PoLoader $loader */ + $loader = $this->app->make(PoLoader::class); } - } - $translator->loadTranslations($translations); + $translations = $loader->loadFile($file, $translations); + } + $translator = GettextTranslator::createFromTranslations($translations); $this->translators[$locale] = $translator; } diff --git a/tests/Unit/Helpers/Translation/GettextTranslatorTest.php b/tests/Unit/Helpers/Translation/GettextTranslatorTest.php index 176b3093..20876c98 100644 --- a/tests/Unit/Helpers/Translation/GettextTranslatorTest.php +++ b/tests/Unit/Helpers/Translation/GettextTranslatorTest.php @@ -16,9 +16,7 @@ class GettextTranslatorTest extends ServiceProviderTest public function testNoTranslation() { $translations = $this->getTranslations(); - - $translator = new GettextTranslator(); - $translator->loadTranslations($translations); + $translator = GettextTranslator::createFromTranslations($translations); $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(); + $translator = GettextTranslator::createFromTranslations($translations); - $translator = new GettextTranslator(); - $translator->loadTranslations($translations); - - $this->assertEquals('Translation!', $translator->dpgettext(null, null, 'test.value')); + $this->assertEquals('Translation!', $translator->gettext('test.value')); } /** - * @covers \Engelsystem\Helpers\Translation\GettextTranslator::dnpgettext() + * @covers \Engelsystem\Helpers\Translation\GettextTranslator::translatePlural */ - public function testDnpgettext() + public function testTranslatePlural() { $translations = $this->getTranslations(); + $translator = GettextTranslator::createFromTranslations($translations); - $translator = new GettextTranslator(); - $translator->loadTranslations($translations); - - $this->assertEquals('Translations!', $translator->dnpgettext(null, null, 'test.value', 'test.values', 2)); + $this->assertEquals('Translations!', $translator->ngettext('test.value', 'test.value', 2)); } /** @@ -59,11 +53,12 @@ class GettextTranslatorTest extends ServiceProviderTest */ protected function getTranslations(): Translations { - $translations = new Translations(); - $translations[] = - (new Translation(null, 'test.value', 'test.values')) - ->setTranslation('Translation!') - ->setPluralTranslations(['Translations!']); + $translation = Translation::create(null, 'test.value') + ->translate('Translation!') + ->translatePlural('Translations!'); + + $translations = Translations::create(); + $translations->add($translation); return $translations; }