Locale unification part 2 (make PHP handle all locale gen/import).

This commit is contained in:
Buster "Silver Eagle" Neece 2021-10-27 01:03:16 -05:00
parent bf90348788
commit b02bf1a3ed
No known key found for this signature in database
GPG Key ID: 6D9E12FF03411F4E
22 changed files with 47010 additions and 1623 deletions

View File

@ -70,17 +70,13 @@ jobs:
if: github.event_name == 'push' || github.event_name == 'schedule' if: github.event_name == 'push' || github.event_name == 'schedule'
run: | run: |
rm -rf resources/locale/compiled rm -rf resources/locale/compiled
rm -rf resources/locale/compiled_js
rm -rf resources/locale/*.UTF-8 rm -rf resources/locale/*.UTF-8
rm -rf resources/locale/translations.json
mkdir -p resources/locale/compiled mkdir -p resources/locale/compiled
- name: Generate new translations from existing code. - name: Generate new translations from existing code.
if: github.event_name == 'push' || github.event_name == 'schedule' if: github.event_name == 'push' || github.event_name == 'schedule'
run: | run: bin/console locale:generate
cd frontend
npm ci
npm run generate-locales
bin/console locale:generate
- name: Pull latest translations. - name: Pull latest translations.
if: github.event_name == 'push' || github.event_name == 'schedule' if: github.event_name == 'push' || github.event_name == 'schedule'
@ -103,7 +99,6 @@ jobs:
cd frontend cd frontend
npm ci npm ci
npm run import-locales
npm run build npm run build
- name: Build OpenAPI Docs - name: Build OpenAPI Docs

File diff suppressed because it is too large Load Diff

View File

@ -2,12 +2,10 @@
"name": "azuracast", "name": "azuracast",
"license": "Apache-2.0", "license": "Apache-2.0",
"scripts": { "scripts": {
"dev": "npm ci && bash", "dev": "npm ci && bash",
"dev-build": "npm ci && npm run build", "dev-build": "npm ci && npm run build",
"build": "gulp", "build": "gulp",
"generate-locales": "gettext-extract --attribute v-translate --keywords '$gettext' --output ../resources/locale/frontend.pot $(find ./vue -type f -name '*.vue' -o -name '*.js')", "ci": "npm run build"
"import-locales": "gettext-compile --output ../resources/locale/translations.json $(find ../resources/locale/*.UTF-8 -type f -name '*.po')",
"ci": "npm run import-locales && npm run build"
}, },
"dependencies": { "dependencies": {
"@babel/core": "^7.15.5", "@babel/core": "^7.15.5",
@ -32,7 +30,6 @@
"css-loader": "^1.0", "css-loader": "^1.0",
"del": "^3.0.0", "del": "^3.0.0",
"dirrty": "^1.0.0", "dirrty": "^1.0.0",
"easygettext": "^2.17.0",
"gulp": "^4.0.2", "gulp": "^4.0.2",
"gulp-babel": "^8.0.0", "gulp-babel": "^8.0.0",
"gulp-clean-css": "^3.9.2", "gulp-clean-css": "^3.9.2",

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -24,12 +24,21 @@ class GenerateCommand extends CommandAbstract
$exportDir = $environment->getBaseDirectory() . '/resources/locale'; $exportDir = $environment->getBaseDirectory() . '/resources/locale';
$dest_file = $exportDir . '/default.pot'; $dest_file = $exportDir . '/default.pot';
$frontendFile = $exportDir . '/frontend.pot';
$translations = new Translations(); $translations = new Translations();
if (file_exists($frontendFile)) { // Find all JS/Vue file translations.
$translations->addFromPoFile($frontendFile); $directory = new RecursiveDirectoryIterator($environment->getBaseDirectory() . '/frontend/vue');
$iterator = new RecursiveIteratorIterator($directory);
$vueRegex = new RegexIterator($iterator, '/^.+\.(vue)$/i', RecursiveRegexIterator::GET_MATCH);
foreach ($vueRegex as $pathMatch) {
$translations->addFromVueJsFile($pathMatch[0]);
}
$jsRegex = new RegexIterator($iterator, '/^.+\.(js)$/i', RecursiveRegexIterator::GET_MATCH);
foreach ($jsRegex as $pathMatch) {
$translations->addFromJsCodeFile($pathMatch[0]);
} }
// Find all PHP/PHTML files in the application's code. // Find all PHP/PHTML files in the application's code.

View File

@ -7,6 +7,7 @@ namespace App\Console\Command\Locale;
use App\Console\Command\CommandAbstract; use App\Console\Command\CommandAbstract;
use App\Environment; use App\Environment;
use App\Locale; use App\Locale;
use Gettext\Translation;
use Gettext\Translations; use Gettext\Translations;
use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Style\SymfonyStyle;
@ -21,19 +22,47 @@ class ImportCommand extends CommandAbstract
$locales = Locale::SUPPORTED_LOCALES; $locales = Locale::SUPPORTED_LOCALES;
$locale_base = $environment->getBaseDirectory() . '/resources/locale'; $locale_base = $environment->getBaseDirectory() . '/resources/locale';
$jsTranslations = [];
foreach ($locales as $locale_key => $locale_name) { foreach ($locales as $locale_key => $locale_name) {
$locale_source = $locale_base . '/' . $locale_key . '/LC_MESSAGES/default.po'; $locale_source = $locale_base . '/' . $locale_key . '/LC_MESSAGES/default.po';
if (file_exists($locale_source)) { if (is_file($locale_source)) {
$translations = Translations::fromPoFile($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'; $locale_dest = $locale_base . '/compiled/' . $locale_key . '.php';
$translations->toPhpArrayFile($locale_dest); $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 . ')')); $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);
$io->success('Locales imported.'); $io->success('Locales imported.');
return 0; return 0;
} }