Skip to content

Commit

Permalink
fix: key sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
neznaika0 committed Aug 8, 2024
1 parent 46ddcd1 commit c8919da
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions system/Commands/Translation/LocalizationSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\Helpers\Array\ArrayHelper;
use Config\App;
use Locale;
use RecursiveDirectoryIterator;
Expand Down Expand Up @@ -134,7 +133,6 @@ private function process(string $originalLocale, string $targetLocale): void
}

$targetLanguageKeys = $this->mergeLanguageKeys($originalLanguageKeys, $targetLanguageKeys, $originalLanguageFile->getBasename('.php'));
// ksort($targetLanguageKeys);

$content = "<?php\n\nreturn " . var_export($targetLanguageKeys, true) . ";\n";
file_put_contents($targetLanguageFile, $content);
Expand All @@ -149,28 +147,34 @@ private function process(string $originalLocale, string $targetLocale): void
*/
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
$targetLanguageKeys[$key] = $placeholderValue;
$mergedLanguageKeys[$key] = $placeholderValue;
} else {
if (! array_key_exists($key, $targetLanguageKeys)) {
$targetLanguageKeys[$key] = [];
$mergedLanguageKeys[$key] = $this->mergeLanguageKeys($value, [], $placeholderValue);

continue;
}

$targetLanguageKeys[$key] = $this->mergeLanguageKeys($value, $targetLanguageKeys[$key], $placeholderValue);
$mergedLanguageKeys[$key] = $this->mergeLanguageKeys($value, $targetLanguageKeys[$key], $placeholderValue);
}
}

return ArrayHelper::intersectKeyRecursive($targetLanguageKeys, $originalLanguageKeys);
return $mergedLanguageKeys;
}

private function isIgnoredFile(SplFileInfo $file): bool
Expand Down

0 comments on commit c8919da

Please sign in to comment.