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

ENH Alpha sort keys to match i18nTextCollector #15

Merged
merged 1 commit into from
Jun 21, 2023
Merged
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
19 changes: 17 additions & 2 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ private function mergeYaml(): void
$this->removeBlankStrings($parsedYaml);
// transifex yaml has precedence over original yaml
$contentYaml = $this->arrayMergeRecursive($contentYaml, $parsedYaml);
$this->recursiveKeySort($contentYaml);
}
// Write back to local
file_put_contents($path, Yaml::dump($contentYaml));
Expand Down Expand Up @@ -366,6 +367,7 @@ private function cleanYaml()
$dirty = file_get_contents($sourceFile);
$sourceData = Yaml::parse($dirty);
$this->removeBlankStrings($sourceData);
$this->recursiveKeySort($sourceData);
$cleaned = Yaml::dump($sourceData, 9999, 2);
if ($dirty !== $cleaned) {
$num++;
Expand All @@ -387,6 +389,7 @@ private function mergeJson(): void
if (file_exists($path)) {
$parsedJson = $this->jsonDecode(file_get_contents($path));
$contentJson = array_merge($contentJson, $parsedJson);
$this->recursiveKeySort($contentJson);
}
// Write back to local
file_put_contents($path, $this->jsonEncode($contentJson));
Expand Down Expand Up @@ -628,7 +631,9 @@ private function generateJavascriptInDirectory(string $modulePath, string $jsPat
$count = 0;
foreach (glob("{$jsPath}/src/*.js*") as $sourceFile) {
// re-encode contents to ensure they're consistently formatted
$sourceContents = $this->jsonEncode($this->jsonDecode(file_get_contents($sourceFile)));
$sourceContentsDecoded = $this->jsonDecode(file_get_contents($sourceFile));
$this->recursiveKeySort($sourceContentsDecoded);
$sourceContentsEncoded = $this->jsonEncode($sourceContentsDecoded);
$locale = preg_replace('/\.js.*$/', '', basename($sourceFile));
$targetFile = dirname(dirname($sourceFile)) . '/' . $locale . '.js';
$this->log("Generating file {$targetFile}", true);
Expand All @@ -641,7 +646,7 @@ private function generateJavascriptInDirectory(string $modulePath, string $jsPat
console.error('Class ss.i18n not defined'); // eslint-disable-line no-console
}
} else {
ss.i18n.addDictionary('$locale', $sourceContents);
ss.i18n.addDictionary('$locale', $sourceContentsEncoded);
}
EOT;
file_put_contents($targetFile, $targetContents);
Expand Down Expand Up @@ -687,4 +692,14 @@ private function arrayMergeRecursive(array $array1, array $array2): array
}
return $array1;
}

private function recursiveKeySort(array &$arr): void
{
ksort($arr);
foreach (array_keys($arr) as $key) {
if (is_array($arr[$key])) {
$this->recursiveKeySort($arr[$key]);
}
}
}
}