AzuraCast/src/Console/Command/Locale/ImportCommand.php

70 lines
2.3 KiB
PHP
Raw Normal View History

<?php
2021-07-19 07:53:45 +02:00
declare(strict_types=1);
namespace App\Console\Command\Locale;
use App\Console\Command\CommandAbstract;
use App\Environment;
2021-07-08 22:03:54 +02:00
use App\Locale;
use Gettext\Translation;
use Gettext\Translations;
use Symfony\Component\Console\Style\SymfonyStyle;
class ImportCommand extends CommandAbstract
{
public function __invoke(
SymfonyStyle $io,
Environment $environment
): 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;
$locale_base = $environment->getBaseDirectory() . '/resources/locale';
$jsTranslations = [];
foreach ($locales as $locale_key => $locale_name) {
$locale_source = $locale_base . '/' . $locale_key . '/LC_MESSAGES/default.po';
if (is_file($locale_source)) {
$translations = Translations::fromPoFile($locale_source);
// 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);
}
$locale_dest = $locale_base . '/compiled/' . $locale_key . '.php';
$translations->toPhpArrayFile($locale_dest);
$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();
}
$io->writeln(__('Imported locale: %s', $locale_key . ' (' . $locale_name . ')'));
}
}
$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.');
return 0;
}
}