Removed .mo translation files from version control, use .po as fallback

This commit is contained in:
Igor Scheller 2019-10-09 13:54:04 +02:00
parent fcdd116f14
commit d2e3ce27f0
8 changed files with 51 additions and 5 deletions

1
.gitignore vendored
View File

@ -24,6 +24,7 @@ _vimrc_local.vim
/public/coverage /public/coverage
/coverage /coverage
/unittests.xml /unittests.xml
/resources/lang/*/*.mo
# Composer files # Composer files
/vendor/ /vendor/

View File

@ -58,6 +58,11 @@ The following instructions explain how to get, build and run the latest engelsys
```bash ```bash
yarn build yarn build
``` ```
* Optionally (for better performance)
* Generate translation files
```bash
find resources/lang/ -type f -name '*.po' -exec sh -c 'file="{}"; msgfmt "${file%.*}.po" -o "${file%.*}.mo"' \;
```
### Configuration and Setup ### Configuration and Setup
* The webserver must have write access to the ```import``` and ```storage``` directories and read access for all other directories * The webserver must have write access to the ```import``` and ```storage``` directories and read access for all other directories

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -5,6 +5,7 @@ namespace Engelsystem\Helpers\Translation;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Container\ServiceProvider; use Engelsystem\Container\ServiceProvider;
use Gettext\Translations; use Gettext\Translations;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Session\Session;
class TranslationServiceProvider extends ServiceProvider class TranslationServiceProvider extends ServiceProvider
@ -67,14 +68,18 @@ class TranslationServiceProvider extends ServiceProvider
public function getTranslator(string $locale): GettextTranslator public function getTranslator(string $locale): GettextTranslator
{ {
if (!isset($this->translators[$locale])) { if (!isset($this->translators[$locale])) {
$file = $this->app->get('path.lang') . '/' . $locale . '/default.mo'; $file = $this->getFile($locale);
/** @var GettextTranslator $translator */ /** @var GettextTranslator $translator */
$translator = $this->app->make(GettextTranslator::class); $translator = $this->app->make(GettextTranslator::class);
/** @var Translations $translations */ /** @var Translations $translations */
$translations = $this->app->make(Translations::class); $translations = $this->app->make(Translations::class);
if (Str::endsWith($file, '.mo')) {
$translations->addFromMoFile($file); $translations->addFromMoFile($file);
} else {
$translations->addFromPoFile($file);
}
$translator->loadTranslations($translations); $translator->loadTranslations($translations);
@ -83,4 +88,20 @@ class TranslationServiceProvider extends ServiceProvider
return $this->translators[$locale]; return $this->translators[$locale];
} }
/**
* @param string $locale
* @return string
*/
protected function getFile(string $locale): string
{
$filepath = $file = $this->app->get('path.lang') . '/' . $locale . '/default';
$file = $filepath . '.mo';
if (!file_exists($file)) {
$file = $filepath . '.po';
}
return $file;
}
} }

View File

@ -0,0 +1,3 @@
# Testing content
msgid "foo.bar"
msgstr "B Arr!"

View File

@ -12,7 +12,7 @@ use Symfony\Component\HttpFoundation\Session\Session;
class TranslationServiceProviderTest extends ServiceProviderTest class TranslationServiceProviderTest extends ServiceProviderTest
{ {
/** /**
* @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::register() * @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::register
*/ */
public function testRegister(): void public function testRegister(): void
{ {
@ -30,7 +30,7 @@ class TranslationServiceProviderTest extends ServiceProviderTest
/** @var TranslationServiceProvider|MockObject $serviceProvider */ /** @var TranslationServiceProvider|MockObject $serviceProvider */
$serviceProvider = $this->getMockBuilder(TranslationServiceProvider::class) $serviceProvider = $this->getMockBuilder(TranslationServiceProvider::class)
->setConstructorArgs([$app]) ->setConstructorArgs([$app])
->setMethods(['setLocale']) ->onlyMethods(['setLocale'])
->getMock(); ->getMock();
$app->expects($this->exactly(2)) $app->expects($this->exactly(2))
@ -71,7 +71,7 @@ class TranslationServiceProviderTest extends ServiceProviderTest
} }
/** /**
* @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::getTranslator() * @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::getTranslator
*/ */
public function testGetTranslator(): void public function testGetTranslator(): void
{ {
@ -87,4 +87,20 @@ class TranslationServiceProviderTest extends ServiceProviderTest
// Retry from cache // Retry from cache
$serviceProvider->getTranslator('fo_OO'); $serviceProvider->getTranslator('fo_OO');
} }
/**
* @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::getTranslator
* @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::getFile
*/
public function testGetTranslatorFromPo(): void
{
$app = $this->getApp(['get']);
$this->setExpects($app, 'get', ['path.lang'], __DIR__ . '/Assets');
$serviceProvider = new TranslationServiceProvider($app);
// Get translator using a .po file
$translator = $serviceProvider->getTranslator('ba_RR');
$this->assertEquals('B Arr!', $translator->gettext('foo.bar'));
}
} }