Merge pull request #655 from MyIgel/ci-build-translation
CI: Build translation files for container and release archive, load .po if .mo not generated
This commit is contained in:
commit
f7bbcfb345
|
@ -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/
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -4,7 +4,12 @@ COPY ./ /app/
|
||||||
RUN composer --no-ansi install --no-dev --ignore-platform-reqs
|
RUN composer --no-ansi install --no-dev --ignore-platform-reqs
|
||||||
RUN composer --no-ansi dump-autoload --optimize
|
RUN composer --no-ansi dump-autoload --optimize
|
||||||
|
|
||||||
# Intermediate container for less layers
|
# Intermediate containers for less layers
|
||||||
|
FROM alpine as translation
|
||||||
|
RUN apk add gettext
|
||||||
|
COPY resources/lang/ /data
|
||||||
|
RUN find /data -type f -name '*.po' -exec sh -c 'file="{}"; msgfmt "${file%.*}.po" -o "${file%.*}.mo"' \;
|
||||||
|
|
||||||
FROM alpine as data
|
FROM alpine as data
|
||||||
COPY .babelrc .browserslistrc composer.json LICENSE package.json README.md webpack.config.js yarn.lock /app/
|
COPY .babelrc .browserslistrc composer.json LICENSE package.json README.md webpack.config.js yarn.lock /app/
|
||||||
COPY bin/ /app/bin
|
COPY bin/ /app/bin
|
||||||
|
@ -13,11 +18,11 @@ COPY db/ /app/db
|
||||||
RUN mkdir /app/import/
|
RUN mkdir /app/import/
|
||||||
COPY includes/ /app/includes
|
COPY includes/ /app/includes
|
||||||
COPY public/ /app/public
|
COPY public/ /app/public
|
||||||
COPY resources/lang /app/resources/lang
|
|
||||||
COPY resources/views /app/resources/views
|
COPY resources/views /app/resources/views
|
||||||
COPY src/ /app/src
|
COPY src/ /app/src
|
||||||
COPY storage/ /app/storage
|
COPY storage/ /app/storage
|
||||||
|
|
||||||
|
COPY --from=translation /data/ /app/resources/lang
|
||||||
COPY --from=composer /app/vendor/ /app/vendor
|
COPY --from=composer /app/vendor/ /app/vendor
|
||||||
COPY --from=composer /app/composer.lock /app/
|
COPY --from=composer /app/composer.lock /app/
|
||||||
|
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
# Testing content
|
||||||
|
msgid "foo.bar"
|
||||||
|
msgstr "B Arr!"
|
|
@ -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'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue