-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: New command lang:sync
#9023
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
3fa7394
feat: Add `lang:sync` command
neznaika0 872434a
test: Add tests for `lang:sync` command
neznaika0 49d7a87
feat: Add array helper `intersectKeyRecursive`
neznaika0 4ffb69c
docs: Add changelog
neznaika0 96d640d
docs: Add description for command
neznaika0 7867ecb
fix: Improve condition
neznaika0 042dbea
fix: docs title underline too short
neznaika0 b57def9
fix: typo
neznaika0 5448d7a
fix: Delete debug info
neznaika0 8933d3f
fix: Remove sorting lang keys
neznaika0 b29462b
fix: key sorting
neznaika0 d23c879
Remove array helper `intersectKeyRecursive`
neznaika0 5b90b6f
refactor: Check type translated value
neznaika0 9c92041
fix: Update tests to be readable
neznaika0 76cd73f
fix: Update changelog
neznaika0 cd84a6e
fix: Typo
neznaika0 47a6f8a
refactor: Rework exit codes, skip dots
neznaika0 1eae834
refactor: Accurate command finishing
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 hidden or 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,202 @@ | ||
<?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 CodeIgniter\Exceptions\LogicException; | ||
use Config\App; | ||
use ErrorException; | ||
use FilesystemIterator; | ||
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 for "--target" and "--locale".' | ||
); | ||
|
||
return EXIT_USER_INPUT; | ||
} | ||
|
||
if (ENVIRONMENT === 'testing') { | ||
$this->languagePath = SUPPORTPATH . 'Language'; | ||
} | ||
|
||
if ($this->process($optionLocale, $optionTargetLocale) === EXIT_ERROR) { | ||
return EXIT_ERROR; | ||
} | ||
|
||
CLI::write('All operations done!'); | ||
|
||
return EXIT_SUCCESS; | ||
} | ||
|
||
private function process(string $originalLocale, string $targetLocale): int | ||
{ | ||
$originalLocaleDir = $this->languagePath . DIRECTORY_SEPARATOR . $originalLocale; | ||
$targetLocaleDir = $this->languagePath . DIRECTORY_SEPARATOR . $targetLocale; | ||
|
||
if (! is_dir($originalLocaleDir)) { | ||
CLI::error( | ||
'Error: The "' . clean_path($originalLocaleDir) . '" directory was not found.' | ||
); | ||
|
||
return EXIT_ERROR; | ||
} | ||
|
||
// Unifying the error - mkdir() may cause an exception. | ||
try { | ||
if (! is_dir($targetLocaleDir) && ! mkdir($targetLocaleDir, 0775)) { | ||
throw new ErrorException(); | ||
} | ||
} catch (ErrorException $e) { | ||
CLI::error( | ||
'Error: The target directory "' . clean_path($targetLocaleDir) . '" cannot be accessed.' | ||
); | ||
|
||
return EXIT_ERROR; | ||
} | ||
|
||
$iterator = new RecursiveIteratorIterator( | ||
new RecursiveDirectoryIterator( | ||
$originalLocaleDir, | ||
FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS | ||
) | ||
); | ||
|
||
/** | ||
* @var array<non-empty-string, SplFileInfo> $files | ||
*/ | ||
$files = iterator_to_array($iterator, true); | ||
ksort($files); | ||
|
||
foreach ($files as $originalLanguageFile) { | ||
if ($originalLanguageFile->getExtension() !== 'php') { | ||
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); | ||
} | ||
|
||
return EXIT_SUCCESS; | ||
} | ||
|
||
/** | ||
* @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_string($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; | ||
} elseif (is_array($value)) { | ||
if (! array_key_exists($key, $targetLanguageKeys)) { | ||
$mergedLanguageKeys[$key] = $this->mergeLanguageKeys($value, [], $placeholderValue); | ||
|
||
continue; | ||
} | ||
|
||
$mergedLanguageKeys[$key] = $this->mergeLanguageKeys($value, $targetLanguageKeys[$key], $placeholderValue); | ||
} else { | ||
throw new LogicException('Value for the key "' . $placeholderValue . '" is of the wrong type. Only "array" or "string" is allowed.'); | ||
} | ||
} | ||
|
||
return $mergedLanguageKeys; | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.