Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: New command lang:sync #9023

Open
wants to merge 12 commits into
base: 4.6
Choose a base branch
from
184 changes: 184 additions & 0 deletions system/Commands/Translation/LocalizationSync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Commands\Translation;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use Config\App;
use Locale;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;

/**
* @see \CodeIgniter\Commands\Translation\LocalizationSyncTest
*/
class LocalizationSync extends BaseCommand
{
protected $group = 'Translation';
protected $name = 'lang:sync';
protected $description = 'Synchronize translation files from one language to another.';
protected $usage = 'lang:sync [options]';
protected $arguments = [];
protected $options = [
'--locale' => 'The original locale (en, ru, etc.).',
'--target' => 'Target locale (en, ru, etc.).',
];
private string $languagePath;

public function run(array $params)
{
$optionTargetLocale = '';
$optionLocale = $params['locale'] ?? Locale::getDefault();
$this->languagePath = APPPATH . 'Language';

if (isset($params['target']) && $params['target'] !== '') {
$optionTargetLocale = $params['target'];
}

if (! in_array($optionLocale, config(App::class)->supportedLocales, true)) {
CLI::error(
'Error: "' . $optionLocale . '" is not supported. Supported locales: '
. implode(', ', config(App::class)->supportedLocales)
);

return EXIT_USER_INPUT;
}

if ($optionTargetLocale === '') {
CLI::error(
'Error: "--target" is not configured. Supported locales: '
. implode(', ', config(App::class)->supportedLocales)
);

return EXIT_USER_INPUT;
}

if (! in_array($optionTargetLocale, config(App::class)->supportedLocales, true)) {
CLI::error(
'Error: "' . $optionTargetLocale . '" is not supported. Supported locales: '
. implode(', ', config(App::class)->supportedLocales)
);

return EXIT_USER_INPUT;
}

if ($optionTargetLocale === $optionLocale) {
CLI::error(
'Error: You cannot have the same values "--target" and "--locale".'
);

return EXIT_USER_INPUT;
}

if (ENVIRONMENT === 'testing') {
$this->languagePath = SUPPORTPATH . 'Language';
}

$this->process($optionLocale, $optionTargetLocale);

CLI::write('All operations done!');

return EXIT_SUCCESS;
}

private function process(string $originalLocale, string $targetLocale): void
{
$originalLocaleDir = $this->languagePath . DIRECTORY_SEPARATOR . $originalLocale;
$targetLocaleDir = $this->languagePath . DIRECTORY_SEPARATOR . $targetLocale;

if (! is_dir($originalLocaleDir)) {
CLI::error(
'Error: The "' . $originalLocaleDir . '" directory was not found.'
);
}

if (! is_dir($targetLocaleDir) && ! mkdir($targetLocaleDir, 0775)) {
CLI::error(
'Error: The target directory "' . $targetLocaleDir . '" cannot be accessed.'
);
}

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($originalLocaleDir));

/**
* @var list<SplFileInfo> $files
*/
$files = iterator_to_array($iterator, true);
ksort($files);

foreach ($files as $originalLanguageFile) {
if ($this->isIgnoredFile($originalLanguageFile)) {
continue;
}

$targetLanguageFile = $targetLocaleDir . DIRECTORY_SEPARATOR . $originalLanguageFile->getFilename();

$targetLanguageKeys = [];
$originalLanguageKeys = include $originalLanguageFile;

if (is_file($targetLanguageFile)) {
$targetLanguageKeys = include $targetLanguageFile;
}

$targetLanguageKeys = $this->mergeLanguageKeys($originalLanguageKeys, $targetLanguageKeys, $originalLanguageFile->getBasename('.php'));

$content = "<?php\n\nreturn " . var_export($targetLanguageKeys, true) . ";\n";
file_put_contents($targetLanguageFile, $content);
}
}

/**
* @param array<string, array<string,mixed>|string|null> $originalLanguageKeys
* @param array<string, array<string,mixed>|string|null> $targetLanguageKeys
*
* @return array<string, array<string,mixed>|string|null>
*/
private function mergeLanguageKeys(array $originalLanguageKeys, array $targetLanguageKeys, string $prefix = ''): array
{
$mergedLanguageKeys = [];

foreach ($originalLanguageKeys as $key => $value) {
$placeholderValue = $prefix !== '' ? $prefix . '.' . $key : $key;

if (! is_array($value)) {
// Keep the old value
// TODO: The value type may not match the original one
if (array_key_exists($key, $targetLanguageKeys)) {
$mergedLanguageKeys[$key] = $targetLanguageKeys[$key];

continue;
}

// Set new key with placeholder
$mergedLanguageKeys[$key] = $placeholderValue;
} else {
if (! array_key_exists($key, $targetLanguageKeys)) {
$mergedLanguageKeys[$key] = $this->mergeLanguageKeys($value, [], $placeholderValue);

continue;
}

$mergedLanguageKeys[$key] = $this->mergeLanguageKeys($value, $targetLanguageKeys[$key], $placeholderValue);
}
}

return $mergedLanguageKeys;
}

private function isIgnoredFile(SplFileInfo $file): bool
{
return $file->isDir() || $file->getFilename() === '.' || $file->getFilename() === '..' || $file->getExtension() !== 'php';
}
}
214 changes: 214 additions & 0 deletions tests/system/Commands/Translation/LocalizationSyncTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Commands\Translation;

use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\StreamFilterTrait;
use Config\App;
use Locale;
use PHPUnit\Framework\Attributes\Group;

/**
* @internal
*/
#[Group('Others')]
final class LocalizationSyncTest extends CIUnitTestCase
{
use StreamFilterTrait;

private static string $locale;
private static string $languageTestPath;

/**
* @var array<string, array<string,mixed>|string|null>
*/
private array $expectedKeys = [
'a' => 'Sync.a',
'b' => 'Sync.b',
'c' => 'Sync.c',
'd' => [],
'e' => 'Sync.e',
'f' => [
'g' => 'Sync.f.g',
'h' => [
'i' => 'Sync.f.h.i',
],
],
];

protected function setUp(): void
{
parent::setUp();

config(App::class)->supportedLocales = ['en', 'ru', 'test'];

self::$locale = Locale::getDefault();
self::$languageTestPath = SUPPORTPATH . 'Language' . DIRECTORY_SEPARATOR;
$this->makeLanguageFiles();
}

protected function tearDown(): void
{
parent::tearDown();

$this->clearGeneratedFiles();
}

public function testSyncDefaultLocale(): void
{
command('lang:sync --target test');

$langFile = self::$languageTestPath . 'test/Sync.php';

$this->assertFileExists($langFile);

$langKeys = include $langFile;

$this->assertIsArray($langKeys);
$this->assertSame($this->expectedKeys, $langKeys);
}

public function testSyncWithLocaleOption(): void
{
command('lang:sync --locale ru --target test');

$langFile = self::$languageTestPath . 'test/Sync.php';

$this->assertFileExists($langFile);

$langKeys = include $langFile;

$this->assertIsArray($langKeys);
$this->assertSame($this->expectedKeys, $langKeys);
}

public function testSyncWithExistTranslation(): void
{
// First run, add new keys
command('lang:sync --target test');

$langFile = self::$languageTestPath . 'test/Sync.php';

$this->assertFileExists($langFile);

$langKeys = include $langFile;

$this->assertIsArray($langKeys);
$this->assertSame($this->expectedKeys, $langKeys);

// Second run, save old keys
$oldLangKeys = [
'a' => 'old value 1',
'b' => 2000,
'c' => null,
Comment on lines +113 to +114
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we do not accept int or null as lang messages.
So should show an error message when processing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. These values are more for the translation search test.
I can filter out the bad lines for RU keys, if it's important.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do one of the following:

  • show errors (or just throw Exception?) if there are invalid values in any lang files.
  • add comments in the test code that "values other than string is invalid, but we use invalid values just for testing".

'd' => [],
'e' => '',
'f' => [
'g' => 'old value 2',
'h' => [
'i' => 'old value 3',
],
],
];

$lang = <<<'TEXT_WRAP'
<?php

return [
'a' => 'old value 1',
'b' => 2000,
'c' => null,
'd' => [],
'e' => '',
'f' => [
'g' => 'old value 2',
'h' => [
'i' => 'old value 3',
],
],
];
TEXT_WRAP;

file_put_contents(self::$languageTestPath . 'test/Sync.php', $lang);

command('lang:sync --target test');

$langFile = self::$languageTestPath . 'test/Sync.php';

$this->assertFileExists($langFile);

$langKeys = include $langFile;
$this->assertIsArray($langKeys);
$this->assertSame($oldLangKeys, $langKeys);
}

public function testSyncWithIncorrectLocaleOption(): void
{
command('lang:sync --locale test_locale_incorrect --target test');

$this->assertStringContainsString('is not supported', $this->getStreamFilterBuffer());
}

public function testSyncWithIncorrectTargetOption(): void
{
command('lang:sync --locale en --target test_locale_incorrect');

$this->assertStringContainsString('is not supported', $this->getStreamFilterBuffer());
}

private function makeLanguageFiles(): void
{
$lang = <<<'TEXT_WRAP'
<?php

return [
'a' => 'value 1',
'b' => 2,
'c' => null,
'd' => [],
'e' => '',
'f' => [
'g' => 'value 2',
'h' => [
'i' => 'value 3',
],
],
];
TEXT_WRAP;

file_put_contents(self::$languageTestPath . self::$locale . '/Sync.php', $lang);
file_put_contents(self::$languageTestPath . 'ru/Sync.php', $lang);
}

private function clearGeneratedFiles(): void
{
if (is_file(self::$languageTestPath . self::$locale . '/Sync.php')) {
unlink(self::$languageTestPath . self::$locale . '/Sync.php');
}

if (is_file(self::$languageTestPath . 'ru/Sync.php')) {
unlink(self::$languageTestPath . 'ru/Sync.php');
}

if (is_dir(self::$languageTestPath . 'test')) {
$files = glob(self::$languageTestPath . 'test/*', GLOB_MARK);

foreach ($files as $file) {
unlink($file);
}

rmdir(self::$languageTestPath . 'test');
}
}
}
Loading
Loading