-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: Language translations finder and update #7889
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
Closed
Closed
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f8f93ac
feat: Add lang:find command
neznaika0 1507f01
test: Add test for lang:find command
neznaika0 af13b96
fix: Move typehint to PHPDOc for PHP7.4
neznaika0 90631ae
fix: Added group test
neznaika0 cd55ab5
fix: Formatting Rector
neznaika0 57760e9
fix: Move examples to Services folder
neznaika0 a4cd48e
fix: Update to new folder with examples
neznaika0 731168f
docs: Example code with command
neznaika0 495d65b
docs: Description using command
neznaika0 28c71eb
build(deps): bump actions/checkout from 3 to 4
dependabot[bot] 546087e
Merge pull request #7895 from codeigniter4/dependabot/github_actions/…
samsonasik 21ac56f
Merge branch 'codeigniter4:develop' into feat-language-finder
neznaika0 b1001a8
test: Fix namespace in tests
neznaika0 15cfd15
test: Add test dashed lang keys
neznaika0 a168060
fix: Find lang() with dashed keys
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,339 @@ | ||
<?php | ||
|
||
/** | ||
* 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; | ||
|
||
class LocalizationFinder extends BaseCommand | ||
{ | ||
protected $group = 'Translation'; | ||
protected $name = 'lang:find'; | ||
protected $description = 'Find and save available phrases to translate'; | ||
protected $usage = 'lang:find [options]'; | ||
protected $arguments = []; | ||
protected $options = [ | ||
'--locale' => 'Apply changes to the selected locale (en, ru, etc...).', | ||
'--dir' => 'Directory for searching for translations (in relation to the APPPATH).', | ||
'--show-new' => 'See only new translations as table.', | ||
'--verbose' => 'Output detailed information.', | ||
]; | ||
|
||
/** | ||
* Flag for output detailed information | ||
*/ | ||
private bool $verbose = false; | ||
|
||
private string $languagePath; | ||
|
||
public function run(array $params) | ||
{ | ||
$this->verbose = array_key_exists('verbose', $params); | ||
$cliOptionShowNew = array_key_exists('show-new', $params); | ||
$cliOptionLocale = ! empty($params['locale']) ? $params['locale'] : null; | ||
$cliOptionDir = ! empty($params['dir']) ? $params['dir'] : null; | ||
$currentLocale = Locale::getDefault(); | ||
$currentDir = APPPATH; | ||
$this->languagePath = $currentDir . 'Language'; | ||
$tableRows = []; | ||
$languageFoundKeys = []; | ||
$countFiles = 0; | ||
$countNewKeys = 0; | ||
|
||
if (ENVIRONMENT === 'testing') { | ||
$currentDir = SUPPORTPATH; | ||
$this->languagePath = $currentDir . 'Language/'; | ||
} | ||
|
||
if (is_string($cliOptionLocale)) { | ||
if (! in_array($cliOptionLocale, config(App::class)->supportedLocales, true)) { | ||
CLI::error('Error: Supported locales ' . implode(', ', config(App::class)->supportedLocales)); | ||
|
||
return -1; | ||
} | ||
|
||
$currentLocale = $cliOptionLocale; | ||
} | ||
|
||
if (is_string($cliOptionDir)) { | ||
$tempCurrentDir = realpath($currentDir . $cliOptionDir); | ||
|
||
if (false === $tempCurrentDir) { | ||
CLI::error('Error: Dir must be located in ' . $currentDir); | ||
|
||
return -1; | ||
} | ||
|
||
if (str_starts_with($tempCurrentDir, $this->languagePath)) { | ||
CLI::error('Error: Dir ' . $this->languagePath . ' restricted to scan.'); | ||
|
||
return -1; | ||
} | ||
|
||
$currentDir = $tempCurrentDir; | ||
} | ||
|
||
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($currentDir)); | ||
$this->writeIsVerbose('Current locale: ' . $currentLocale); | ||
$this->writeIsVerbose('Find phrases in ' . $currentDir . ' folder...'); | ||
|
||
/** | ||
* @var SplFileInfo $file | ||
*/ | ||
foreach ($iterator as $file) { | ||
if ($this->isIgnoredFile($file)) { | ||
continue; | ||
} | ||
|
||
$this->writeIsVerbose('File found: ' . mb_substr($file->getRealPath(), mb_strlen(APPPATH))); | ||
$countFiles++; | ||
$languageFoundKeys = array_replace_recursive($this->findTranslationsInFile($file), $languageFoundKeys); | ||
} | ||
|
||
ksort($languageFoundKeys); | ||
$languageDiff = []; | ||
$languageFoundGroups = array_unique(array_keys($languageFoundKeys)); | ||
|
||
foreach ($languageFoundGroups as $langFileName) { | ||
$languageStoredKeys = []; | ||
$languageFilePath = $this->languagePath . DIRECTORY_SEPARATOR . $currentLocale . DIRECTORY_SEPARATOR . $langFileName . '.php'; | ||
|
||
if (is_file($languageFilePath)) { | ||
/** | ||
* Load old localization | ||
*/ | ||
$languageStoredKeys = require $languageFilePath; | ||
} | ||
|
||
$languageDiff = $this->arrayDiffRecursive($languageFoundKeys[$langFileName], $languageStoredKeys); | ||
$countNewKeys += $this->arrayCountRecursive($languageDiff); | ||
|
||
if ($cliOptionShowNew) { | ||
$tableRows = array_merge($this->arrayToTableRows($langFileName, $languageDiff), $tableRows); | ||
} else { | ||
$newLanguageKeys = array_replace_recursive($languageFoundKeys[$langFileName], $languageStoredKeys); | ||
|
||
/** | ||
* New translates exists | ||
*/ | ||
if ($languageDiff !== []) { | ||
if (false === file_put_contents($languageFilePath, $this->templateFile($newLanguageKeys))) { | ||
$this->writeIsVerbose('Lang file ' . $langFileName . ' (error write).', 'red'); | ||
} else { | ||
/** | ||
* FIXME: Need help command (slow run) | ||
*/ | ||
exec('composer cs-fix --quiet ' . $this->languagePath); | ||
$this->writeIsVerbose('Lang file "' . $langFileName . '" successful updated!', 'green'); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if ($cliOptionShowNew && ! empty($tableRows)) { | ||
sort($tableRows); | ||
CLI::table($tableRows, ['File', 'Key']); | ||
} | ||
|
||
$this->writeIsVerbose('Files found: ' . $countFiles); | ||
$this->writeIsVerbose('New translates found: ' . $countNewKeys); | ||
CLI::write('All operations done!'); | ||
} | ||
|
||
/** | ||
* @param SplFileInfo|string $file | ||
*/ | ||
private function findTranslationsInFile($file): array | ||
{ | ||
$languageFoundKeys = []; | ||
|
||
if (is_string($file) && is_file($file)) { | ||
$file = new SplFileInfo($file); | ||
} | ||
|
||
$fileContent = file_get_contents($file->getRealPath()); | ||
preg_match_all('/lang\(\'([._a-z0-9]+)\'\)/ui', $fileContent, $matches); | ||
|
||
if (empty($matches[1])) { | ||
return []; | ||
} | ||
|
||
foreach ($matches[1] as $phraseKey) { | ||
$phraseKeys = explode('.', $phraseKey); | ||
|
||
/** | ||
* Language key not have Filename or Lang key | ||
*/ | ||
if (count($phraseKeys) < 2) { | ||
continue; | ||
} | ||
|
||
$languageFileName = array_shift($phraseKeys); | ||
$isEmptyNestedArray = (! empty($languageFileName) && empty($phraseKeys[0])) | ||
|| (empty($languageFileName) && ! empty($phraseKeys[0])) | ||
|| (empty($languageFileName) && empty($phraseKeys[0])); | ||
|
||
if ($isEmptyNestedArray) { | ||
continue; | ||
} | ||
|
||
/** | ||
* Language key as string | ||
*/ | ||
if (count($phraseKeys) === 1) { | ||
/** | ||
* Add found language keys to temporary array | ||
*/ | ||
$languageFoundKeys[$languageFileName][$phraseKeys[0]] = $phraseKey; | ||
} else { | ||
$childKeys = $this->buildMultiArray($phraseKeys, $phraseKey); | ||
$languageFoundKeys[$languageFileName] = array_replace_recursive($languageFoundKeys[$languageFileName] ?? [], $childKeys); | ||
} | ||
} | ||
|
||
return $languageFoundKeys; | ||
} | ||
|
||
private function isIgnoredFile(SplFileInfo $file): bool | ||
{ | ||
if ($file->isDir() || str_contains($file->getRealPath(), $this->languagePath)) { | ||
return true; | ||
} | ||
|
||
return 'php' !== $file->getExtension(); | ||
} | ||
|
||
private function templateFile(array $language = []): string | ||
{ | ||
if (! empty($language)) { | ||
$languageArrayString = $this->languageKeysDump($language); | ||
|
||
return <<<PHP | ||
<?php | ||
|
||
return {$languageArrayString}; | ||
PHP; | ||
} | ||
|
||
return <<<'PHP' | ||
<?php | ||
|
||
return []; | ||
PHP; | ||
} | ||
|
||
/** | ||
* Create multidimensional array from another keys | ||
*/ | ||
private function buildMultiArray(array $fromKeys, string $lastArrayValue = ''): array | ||
{ | ||
$newArray = []; | ||
$lastIndex = array_pop($fromKeys); | ||
$current = &$newArray; | ||
|
||
foreach ($fromKeys as $value) { | ||
$current[$value] = []; | ||
$current = &$current[$value]; | ||
} | ||
|
||
$current[$lastIndex] = $lastArrayValue; | ||
|
||
return $newArray; | ||
} | ||
|
||
/** | ||
* Compare recursive two arrays and return new array (diference) | ||
*/ | ||
private function arrayDiffRecursive(array $originalArray, array $compareArray): array | ||
{ | ||
$difference = []; | ||
|
||
if (count($compareArray) < 1) { | ||
return $originalArray; | ||
} | ||
|
||
foreach ($originalArray as $originalKey => $originalValue) { | ||
if (is_array($originalValue)) { | ||
$diffArrays = null; | ||
|
||
if (isset($compareArray[$originalKey])) { | ||
$diffArrays = $this->arrayDiffRecursive($originalValue, $compareArray[$originalKey]); | ||
} else { | ||
$difference[$originalKey] = $originalValue; | ||
} | ||
|
||
if (! empty($diffArrays)) { | ||
$difference[$originalKey] = $diffArrays; | ||
} | ||
} elseif (is_string($originalValue) && ! array_key_exists($originalKey, $compareArray)) { | ||
$difference[$originalKey] = $originalValue; | ||
} | ||
} | ||
|
||
return $difference; | ||
} | ||
|
||
/** | ||
* Convert multi arrays to specific CLI table rows (flat array) | ||
*/ | ||
private function arrayToTableRows(string $langFileName, array $array): array | ||
{ | ||
$rows = []; | ||
|
||
foreach ($array as $value) { | ||
if (is_array($value)) { | ||
$rows = array_merge($rows, $this->arrayToTableRows($langFileName, $value)); | ||
|
||
continue; | ||
} | ||
|
||
if (is_string($value)) { | ||
$rows[] = [$langFileName, $value]; | ||
} | ||
} | ||
|
||
return $rows; | ||
} | ||
|
||
private function arrayCountRecursive(array $array, int $counter = 0): int | ||
{ | ||
foreach ($array as $value) { | ||
if (! is_array($value)) { | ||
$counter++; | ||
} else { | ||
$counter = $this->arrayCountRecursive($value, $counter); | ||
} | ||
} | ||
|
||
return $counter; | ||
} | ||
|
||
private function languageKeysDump(array $inputArray): string | ||
{ | ||
return var_export($inputArray, true); | ||
} | ||
|
||
/** | ||
* Show details in the console if the flag is set | ||
*/ | ||
private function writeIsVerbose(string $text = '', ?string $foreground = null, ?string $background = null): void | ||
{ | ||
if ($this->verbose) { | ||
CLI::write($text, $foreground, $background); | ||
} | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
/** | ||
* 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 Tests\Support\Controllers\Translation; | ||
|
||
class TranslationOne | ||
{ | ||
public function list() | ||
{ | ||
$translationOne1 = lang('TranslationOne.title'); | ||
$translationOne2 = lang('TranslationOne.DESCRIPTION'); | ||
$translationOne3 = lang('TranslationOne.metaTags'); | ||
$translationOne4 = lang('TranslationOne.Copyright'); | ||
$translationOne5 = lang('TranslationOne.last_operation_success'); | ||
|
||
$translationThree1 = lang('TranslationThree.alerts.created'); | ||
$translationThree2 = lang('TranslationThree.alerts.failed_insert'); | ||
$translationThree3 = lang('TranslationThree.alerts.Updated'); | ||
$translationThree4 = lang('TranslationThree.alerts.DELETED'); | ||
|
||
$translationThree5 = lang('TranslationThree.formFields.new.name'); | ||
$translationThree6 = lang('TranslationThree.formFields.new.TEXT'); | ||
$translationThree7 = lang('TranslationThree.formFields.new.short_tag'); | ||
$translationThree8 = lang('TranslationThree.formFields.edit.name'); | ||
$translationThree9 = lang('TranslationThree.formFields.edit.TEXT'); | ||
$translationThree10 = lang('TranslationThree.formFields.edit.short_tag'); | ||
} | ||
} |
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.
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 would like use
Ref : https://github.com/rectorphp/rector/blob/main/docs/rector_rules_overview.md#arraymergeofnonarraystosimplearrayrector
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.
No. Read the instructions carefully.
You are trying to merge two arrays, and the documentation talks about strings. This will result in an error.