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

FIX Remove translation values that match english source values #19

Merged
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
87 changes: 87 additions & 0 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 @@ -304,6 +306,55 @@ private function mergeYaml(): void
$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);
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
}
if (!$enYaml) {
$enYaml = Yaml::parse(file_get_contents($enPath));
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
}
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 @@ -397,6 +448,42 @@ private function mergeJson(): void
$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));
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
}
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));
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
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 Down
Loading