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 17 commits into
base: 4.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 200 additions & 0 deletions system/Commands/Translation/LocalizationSync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?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_SUCCESS) {
CLI::write('All operations done!');
}

return EXIT_SUCCESS;
Comment on lines +92 to +96
Copy link
Member

Choose a reason for hiding this comment

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

Ahm, something's wrong here. Should one of them be EXIT_ERROR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

EXIT_ERROR will interrupt process(). EXIT_SUCCESS will not be executed.

Copy link
Member

Choose a reason for hiding this comment

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

How will it interrupt the process? I mean, should this if be checked against EXIT_ERROR. If yes, return EXIT_ERROR. If no, write to CLI that all operations were done. Then 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 list<SplFileInfo> $files
*/
$files = iterator_to_array($iterator, true);
ksort($files);
Comment on lines +135 to +136
Copy link
Member

Choose a reason for hiding this comment

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

If this is already a list, then you don't need to ksort? Or maybe this should be sort?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Inside the array "File.text" => "...", "File.text2" => "..." Sorting keys is necessary for correct output and writing to a file.

Copy link
Member

Choose a reason for hiding this comment

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

Okay, I missed the KEY_AS_PATHNAME flag. In that case, the array is not a list as you have written in the phpdoc. It should be @var array<non-empty-string, SplFileInfo> $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;
}
}
Loading
Loading