diff options
| author | 2018-03-03 10:26:54 +0100 | |
|---|---|---|
| committer | 2018-03-03 10:26:54 +0100 | |
| commit | a2312a608a5f995b7c2d81301730aac8ea49024b (patch) | |
| tree | 7baa5ba85e20cd01a1179aa857d581036dc7ef3f /cli/i18n/I18nFile.php | |
| parent | 56304d4a7c54c5a9b632076d3000a5201cff669b (diff) | |
Improve translating tools (#1808)
To ease the use of the translating tools, I've added a formatting method to
keep the output files identicals. This will reduce the amount of time needed
to handle translations.
Diffstat (limited to 'cli/i18n/I18nFile.php')
| -rw-r--r-- | cli/i18n/I18nFile.php | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/cli/i18n/I18nFile.php b/cli/i18n/I18nFile.php index d6489ee21..627f6f580 100644 --- a/cli/i18n/I18nFile.php +++ b/cli/i18n/I18nFile.php @@ -36,8 +36,7 @@ class i18nFile { } foreach ($file as $name => $content) { $filename = $dir . DIRECTORY_SEPARATOR . $name; - $fullContent = var_export($this->unflatten($content), true); - file_put_contents($filename, sprintf('<?php return %s;', $fullContent)); + file_put_contents($filename, $this->format($content)); } } } @@ -89,4 +88,32 @@ class i18nFile { return $a; } + /** + * Format an array of translation + * + * It takes an array of translation and format it to be dumped in a + * translation file. The array is first converted to a string then some + * formatting regexes are applied to match the original content. + * + * @param array $translation + * @return string + */ + private function format($translation) { + $translation = var_export($this->unflatten($translation), true); + $patterns = array( + '/array \(/', + '/=>\s*array/', + '/ {2}/', + ); + $replacements = array( + 'array(', + '=> array', + "\t", // Double quoting is mandatory to have a tab instead of the \t string + ); + $translation = preg_replace($patterns, $replacements, $translation); + + // Double quoting is mandatory to have new lines instead of \n strings + return sprintf("<?php\n\nreturn %s;", $translation); + } + } |
