2019-09-11 01:10:57 +02:00
|
|
|
<?php
|
2020-10-15 00:19:31 +02:00
|
|
|
|
2021-07-19 07:53:45 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-09-21 16:06:24 +02:00
|
|
|
namespace App\Console\Command\Locale;
|
2019-09-11 01:10:57 +02:00
|
|
|
|
2020-09-21 16:06:24 +02:00
|
|
|
use App\Console\Command\CommandAbstract;
|
2020-12-03 05:18:06 +01:00
|
|
|
use App\Environment;
|
2021-07-08 22:03:54 +02:00
|
|
|
use App\Locale;
|
2021-10-27 08:03:16 +02:00
|
|
|
use Gettext\Translation;
|
2019-09-11 01:10:57 +02:00
|
|
|
use Gettext\Translations;
|
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
|
|
|
|
2020-09-21 16:06:24 +02:00
|
|
|
class ImportCommand extends CommandAbstract
|
2019-09-11 01:10:57 +02:00
|
|
|
{
|
|
|
|
public function __invoke(
|
|
|
|
SymfonyStyle $io,
|
2020-12-03 05:18:06 +01:00
|
|
|
Environment $environment
|
2020-10-15 00:19:31 +02:00
|
|
|
): int {
|
2020-10-05 08:27:12 +02:00
|
|
|
$io->title('Import Locales');
|
|
|
|
|
2021-07-08 22:03:54 +02:00
|
|
|
$locales = Locale::SUPPORTED_LOCALES;
|
2020-12-03 05:18:06 +01:00
|
|
|
$locale_base = $environment->getBaseDirectory() . '/resources/locale';
|
2019-09-11 01:10:57 +02:00
|
|
|
|
2021-10-27 08:03:16 +02:00
|
|
|
$jsTranslations = [];
|
|
|
|
|
2019-09-11 01:10:57 +02:00
|
|
|
foreach ($locales as $locale_key => $locale_name) {
|
|
|
|
$locale_source = $locale_base . '/' . $locale_key . '/LC_MESSAGES/default.po';
|
|
|
|
|
2021-10-27 08:03:16 +02:00
|
|
|
if (is_file($locale_source)) {
|
2019-09-11 01:10:57 +02:00
|
|
|
$translations = Translations::fromPoFile($locale_source);
|
|
|
|
|
2021-10-27 08:03:16 +02:00
|
|
|
// Temporary inclusion of frontend translations
|
|
|
|
$frontendTranslations = $locale_base . '/' . $locale_key . '/LC_MESSAGES/frontend.po';
|
|
|
|
if (is_file($frontendTranslations)) {
|
|
|
|
$frontendTranslations = Translations::fromPoFile($frontendTranslations);
|
|
|
|
$translations->mergeWith($frontendTranslations);
|
|
|
|
}
|
|
|
|
|
2019-09-11 01:10:57 +02:00
|
|
|
$locale_dest = $locale_base . '/compiled/' . $locale_key . '.php';
|
|
|
|
$translations->toPhpArrayFile($locale_dest);
|
|
|
|
|
2021-10-27 08:03:16 +02:00
|
|
|
$localeJsKey = str_replace('.UTF-8', '', $locale_key);
|
|
|
|
|
|
|
|
/** @var Translation $translation */
|
|
|
|
foreach ($translations as $translation) {
|
|
|
|
if ($translation->isDisabled() || !$translation->hasTranslation()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$jsTranslations[$localeJsKey][$translation->getOriginal()] = $translation->getTranslation();
|
|
|
|
}
|
|
|
|
|
2019-09-11 01:10:57 +02:00
|
|
|
$io->writeln(__('Imported locale: %s', $locale_key . ' (' . $locale_name . ')'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 08:03:16 +02:00
|
|
|
$jsTranslations = json_encode(
|
|
|
|
$jsTranslations,
|
|
|
|
JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
|
|
|
|
);
|
|
|
|
$jsTranslationsPath = $locale_base . '/translations.json';
|
|
|
|
|
|
|
|
file_put_contents($jsTranslationsPath, $jsTranslations);
|
|
|
|
|
2020-10-05 08:27:12 +02:00
|
|
|
$io->success('Locales imported.');
|
2019-09-11 01:10:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|