Skip to content

Commit

Permalink
Merge branch '1.1' into 1
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Nov 7, 2023
2 parents 22d8ee2 + 715713c commit 7b3f6bd
Showing 1 changed file with 108 additions and 6 deletions.
114 changes: 108 additions & 6 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ public function run()
$this->setJsonAndYmlFileTimes();
$this->transifexPullSource();
$this->mergeYaml();
$this->removeEnglishStringsFromYamlTranslations();
$this->cleanYaml();
$this->mergeJson();
$this->removeEnglishStringsFromJsonTranslations();
}
if ($this->doCollectStrings) {
$this->collectStrings();
Expand Down Expand Up @@ -296,13 +298,63 @@ 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));
}
$this->log('Finished merging ' . count($this->originalYaml) . ' yaml files');
}

/**
* Transifex may add english strings to translation yaml files, remove them
*/
private function removeEnglishStringsFromYamlTranslations(): void
{
$this->log('Removing english from yml translation files');
$enPath = '';
$enYaml = null;
$count = 0;
foreach (array_keys($this->originalYaml) as $path) {
if (!file_exists($path)) {
continue;
}
if (!$enPath) {
$enPath = preg_replace('#/[^/\.]+\.yml#', '/en.yml', $path);
}
if (!$enYaml) {
$enYaml = Yaml::parse(file_get_contents($enPath));
}
if ($enPath === $path) {
continue;
}
// Remove any keys where the value is the same as the source english value
$contentYaml = Yaml::parse(file_get_contents($path));
foreach (array_keys($contentYaml) as $countryCode) {
foreach (array_keys($contentYaml[$countryCode] ?? []) as $className) {
foreach (array_keys($contentYaml[$countryCode][$className]) as $key) {
$value = $contentYaml[$countryCode][$className][$key] ?? null;
$enValue = $enYaml['en'][$className][$key] ?? null;
if ($value === $enValue) {
unset($contentYaml[$countryCode][$className][$key]);
$count++;
}
// Remove any className nodes that have had all their keys removed
if (
isset($contentYaml[$countryCode][$className])
&& empty($contentYaml[$countryCode][$className])
) {
unset($contentYaml[$countryCode][$className]);
}
}
}
}
// Write back to local
file_put_contents($path, Yaml::dump($contentYaml));
}
$this->log("Removed $count english string(s) from yml translation files");
}

private $blankEnStrings;

private function removeBlankStrings(array &$sourceData)
Expand Down Expand Up @@ -366,6 +418,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,13 +440,50 @@ 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));
}
$this->log('Finished merging ' . count($this->originalJson) . ' json files');
}

/**
* Transifex may add english strings to translation json files, remove them
*/
private function removeEnglishStringsFromJsonTranslations(): void
{
$this->log('Removing english from json translation files');
$enPath = '';
$enJson = null;
$count = 0;
foreach (array_keys($this->originalJson) as $path) {
if (!file_exists($path)) {
continue;
}
if (!$enPath) {
$enPath = preg_replace('#/[^/\.]+\.json$#', '/en.json', $path);
}
if (!$enJson) {
$enJson = $this->jsonDecode(file_get_contents($enPath));
}
if ($enPath === $path) {
continue;
}
// Remove any keys where the value is the same as the source english value
$contentJson = $this->jsonDecode(file_get_contents($path));
foreach (array_keys($contentJson) as $key) {
if (array_key_exists($key, $enJson) && $enJson[$key] === $contentJson[$key]) {
unset($contentJson[$key]);
$count++;
}
}
// Write back to local
file_put_contents($path, $this->jsonEncode($contentJson));
}
$this->log("Removed $count english string(s) from json translation files");
}

/**
* Run text collector on the given modules
*/
Expand All @@ -406,7 +496,7 @@ private function collectStrings(): void
}
$module = urlencode(implode(',', $modulesNames));
$site = rtrim($this->txSite, '/');
$this->exec("wget --content-on-error $site/dev/tasks/i18nTextCollectorTask?flush=all&merge=1&module=$module");
$this->exec("wget --content-on-error $site/dev/tasks/i18nTextCollectorTask?module=$module");
}

/**
Expand Down Expand Up @@ -470,7 +560,7 @@ private function gitCommitPushAndPullRequest(): void
$this->exec("git checkout -b $branch", $modulePath);

// Commit changes
$title = 'ENH Update translations';
$title = 'TLN Update translations';
$this->exec("git commit -m \"$title\"", $modulePath);

if ($this->isDevMode) {
Expand Down Expand Up @@ -560,7 +650,7 @@ private function log(string $message, bool $isVerbose = false): void
/**
* Gets the module lang dir
*/
private function getYmlLangDirectory(string $modulePath): string
private function getYmlLangDirectory(string $modulePath): ?string
{
$sources = $this->getTransifexSources($modulePath);
foreach ($sources as $source) {
Expand Down Expand Up @@ -596,7 +686,7 @@ private function getTransifexSources(string $modulePath): array
$content = file_get_contents($path);
$sources = [];
foreach (preg_split('#\R#u', $content) as $line) {
if (preg_match('#source_file\s=\s(?<path>\S+)#', $line, $matches)) {
if (preg_match('#source_file\s+=\s+(?<path>\S+)#', $line, $matches)) {
$sources[] = $matches['path'];
}
}
Expand Down Expand Up @@ -628,7 +718,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 +733,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 +779,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 7b3f6bd

Please sign in to comment.