Skip to content

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
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 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
339 changes: 339 additions & 0 deletions system/Commands/Translation/LocalizationFinder.php
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);
Copy link
Collaborator

@ddevsr ddevsr Sep 3, 2023

Choose a reason for hiding this comment

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

I would like use

Suggested change
$tableRows = array_merge($this->arrayToTableRows($langFileName, $languageDiff), $tableRows);
$tableRows = [$this->arrayToTableRows($langFileName, $languageDiff), $tableRows];

Ref : https://github.com/rectorphp/rector/blob/main/docs/rector_rules_overview.md#arraymergeofnonarraystosimplearrayrector

Copy link
Contributor Author

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.

  • I applied the Rector before the commit

} 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);
}
}
}
36 changes: 36 additions & 0 deletions tests/_support/Services/Translation/TranslationOne.php
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');
}
}
Loading