Skip to content

Commit

Permalink
Merge pull request #15 from creative-commoners/pulls/1.1/sort
Browse files Browse the repository at this point in the history
ENH Alpha sort keys to match i18nTextCollector
  • Loading branch information
GuySartorelli authored Jun 21, 2023
2 parents 8cb77bd + 05ded67 commit f3d5f98
Showing 1 changed file with 17 additions and 2 deletions.
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]);
}
}
}
}

0 comments on commit f3d5f98

Please sign in to comment.