-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
neznaika0
wants to merge
12
commits into
codeigniter4:4.6
Choose a base branch
from
neznaika0:feat-lang-sync
base: 4.6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9ead16f
feat: Add `lang:sync` command
neznaika0 0c7a010
test: Add tests for `lang:sync` command
neznaika0 e5bb6cf
feat: Add array helper `intersectKeyRecursive`
neznaika0 e8f6549
docs: Add changelog
neznaika0 c67616d
docs: Add description for command
neznaika0 e1e8931
fix: Improve condition
neznaika0 0b89adc
fix: docs title underline too short
neznaika0 eeef980
fix: typo
neznaika0 81e570e
fix: Delete debug info
neznaika0 6460df0
fix: Remove sorting lang keys
neznaika0 e1d5d1c
fix: key sorting
neznaika0 435d8d6
Remove array helper `intersectKeyRecursive`
neznaika0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <admin@codeigniter.com> | ||
* | ||
* 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
214
tests/system/Commands/Translation/LocalizationSyncTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <admin@codeigniter.com> | ||
* | ||
* 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, | ||
'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'); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
ornull
as lang messages.So should show an error message when processing?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
string
is invalid, but we use invalid values just for testing".