Replaced gettext translation with package
This allows to check if no translation is availablemain
parent
f90ab26fee
commit
508695efb2
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Migrations;
|
||||
|
||||
use Engelsystem\Database\Migration\Migration;
|
||||
|
||||
class FixUserLanguages extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migration
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
$connection = $this->schema->getConnection();
|
||||
$connection
|
||||
->table('users_settings')
|
||||
->update([
|
||||
'language' => $connection->raw('REPLACE(language, ".UTF-8", "")')
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migration
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
$connection = $this->schema->getConnection();
|
||||
$connection
|
||||
->table('users_settings')
|
||||
->update([
|
||||
'language' => $connection->raw('CONCAT(language, ".UTF-8")')
|
||||
]);
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Helpers\Translation;
|
||||
|
||||
use Gettext\Translator;
|
||||
|
||||
class GettextTranslator extends Translator
|
||||
{
|
||||
/**
|
||||
* @param string $domain
|
||||
* @param string $context
|
||||
* @param string $original
|
||||
* @return string
|
||||
* @throws TranslationNotFound
|
||||
*/
|
||||
public function dpgettext($domain, $context, $original)
|
||||
{
|
||||
$this->assertHasTranslation($domain, $context, $original);
|
||||
|
||||
return parent::dpgettext($domain, $context, $original);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $domain
|
||||
* @param string $context
|
||||
* @param string $original
|
||||
* @param string $plural
|
||||
* @param string $value
|
||||
* @return string
|
||||
* @throws TranslationNotFound
|
||||
*/
|
||||
public function dnpgettext($domain, $context, $original, $plural, $value)
|
||||
{
|
||||
$this->assertHasTranslation($domain, $context, $original);
|
||||
|
||||
return parent::dnpgettext($domain, $context, $original, $plural, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $domain
|
||||
* @param string $context
|
||||
* @param string $original
|
||||
* @throws TranslationNotFound
|
||||
*/
|
||||
protected function assertHasTranslation($domain, $context, $original)
|
||||
{
|
||||
if ($this->getTranslation($domain, $context, $original)) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new TranslationNotFound(implode('/', [$domain, $context, $original]));
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Helpers\Translation;
|
||||
|
||||
use Exception;
|
||||
|
||||
class TranslationNotFound extends Exception
|
||||
{
|
||||
}
|
Binary file not shown.
@ -0,0 +1,3 @@
|
||||
# Testing content
|
||||
msgid "foo.bar"
|
||||
msgstr "Foo Bar!"
|
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Helpers\Translation;
|
||||
|
||||
use Engelsystem\Helpers\Translation\GettextTranslator;
|
||||
use Engelsystem\Helpers\Translation\TranslationNotFound;
|
||||
use Engelsystem\Test\Unit\ServiceProviderTest;
|
||||
use Gettext\Translation;
|
||||
use Gettext\Translations;
|
||||
|
||||
class GettextTranslatorTest extends ServiceProviderTest
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Helpers\Translation\GettextTranslator::assertHasTranslation()
|
||||
*/
|
||||
public function testNoTranslation()
|
||||
{
|
||||
$translations = $this->getTranslations();
|
||||
|
||||
$translator = new GettextTranslator();
|
||||
$translator->loadTranslations($translations);
|
||||
|
||||
$this->assertEquals('Translation!', $translator->gettext('test.value'));
|
||||
|
||||
$this->expectException(TranslationNotFound::class);
|
||||
$this->expectExceptionMessage('//foo.bar');
|
||||
|
||||
$translator->gettext('foo.bar');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Helpers\Translation\GettextTranslator::dpgettext()
|
||||
*/
|
||||
public function testDpgettext()
|
||||
{
|
||||
$translations = $this->getTranslations();
|
||||
|
||||
$translator = new GettextTranslator();
|
||||
$translator->loadTranslations($translations);
|
||||
|
||||
$this->assertEquals('Translation!', $translator->dpgettext(null, null, 'test.value'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Helpers\Translation\GettextTranslator::dnpgettext()
|
||||
*/
|
||||
public function testDnpgettext()
|
||||
{
|
||||
$translations = $this->getTranslations();
|
||||
|
||||
$translator = new GettextTranslator();
|
||||
$translator->loadTranslations($translations);
|
||||
|
||||
$this->assertEquals('Translations!', $translator->dnpgettext(null, null, 'test.value', 'test.values', 2));
|
||||
}
|
||||
|
||||
protected function getTranslations(): Translations
|
||||
{
|
||||
$translations = new Translations();
|
||||
$translations[] =
|
||||
(new Translation(null, 'test.value', 'test.values'))
|
||||
->setTranslation('Translation!')
|
||||
->setPluralTranslations(['Translations!']);
|
||||
|
||||
return $translations;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue