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 committed by msquare
parent 973c108b15
commit fa35187795
8 changed files with 51 additions and 5 deletions

1
.gitignore vendored
View File

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

View File

@ -58,6 +58,11 @@ The following instructions explain how to get, build and run the latest engelsys
```bash
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
* 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\Container\ServiceProvider;
use Gettext\Translations;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\Session\Session;
class TranslationServiceProvider extends ServiceProvider
@ -69,14 +70,18 @@ class TranslationServiceProvider extends ServiceProvider
public function getTranslator(string $locale): GettextTranslator
{
if (!isset($this->translators[$locale])) {
$file = $this->app->get('path.lang') . '/' . $locale . '/default.mo';
$file = $this->getFile($locale);
/** @var GettextTranslator $translator */
$translator = $this->app->make(GettextTranslator::class);
/** @var Translations $translations */
$translations = $this->app->make(Translations::class);
$translations->addFromMoFile($file);
if (Str::endsWith($file, '.mo')) {
$translations->addFromMoFile($file);
} else {
$translations->addFromPoFile($file);
}
$translator->loadTranslations($translations);
@ -85,4 +90,20 @@ class TranslationServiceProvider extends ServiceProvider
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
{
/**
* @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::register()
* @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::register
*/
public function testRegister(): void
{
@ -30,7 +30,7 @@ class TranslationServiceProviderTest extends ServiceProviderTest
/** @var TranslationServiceProvider|MockObject $serviceProvider */
$serviceProvider = $this->getMockBuilder(TranslationServiceProvider::class)
->setConstructorArgs([$app])
->setMethods(['setLocale'])
->onlyMethods(['setLocale'])
->getMock();
$app->expects($this->exactly(2))
@ -75,7 +75,7 @@ class TranslationServiceProviderTest extends ServiceProviderTest
}
/**
* @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::getTranslator()
* @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::getTranslator
*/
public function testGetTranslator(): void
{
@ -91,4 +91,20 @@ class TranslationServiceProviderTest extends ServiceProviderTest
// Retry from cache
$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'));
}
}