summaryrefslogtreecommitdiff
path: root/cli/i18n/I18nIgnoreFile.php
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2018-06-03 13:35:38 +0200
committerGravatar GitHub <noreply@github.com> 2018-06-03 13:35:38 +0200
commitc0122003fe3031926546012b86a38b5187082613 (patch)
tree5502841327e7775f280fbd12732b4e8b8b7be6ff /cli/i18n/I18nIgnoreFile.php
parent029f4107123f6c318584bf9a43da7118c318657f (diff)
parentbe778c6bc2d8075e5a923153183b47507a2a71e3 (diff)
Merge pull request #1902 from FreshRSS/dev1.11.0
FreshRSS 1.11.0
Diffstat (limited to 'cli/i18n/I18nIgnoreFile.php')
-rw-r--r--cli/i18n/I18nIgnoreFile.php64
1 files changed, 64 insertions, 0 deletions
diff --git a/cli/i18n/I18nIgnoreFile.php b/cli/i18n/I18nIgnoreFile.php
new file mode 100644
index 000000000..714e000ca
--- /dev/null
+++ b/cli/i18n/I18nIgnoreFile.php
@@ -0,0 +1,64 @@
+<?php
+
+require_once __DIR__ . '/I18nData.php';
+require_once __DIR__ . '/I18nFileInterface.php';
+
+class I18nIgnoreFile implements I18nFileInterface {
+
+ private $i18nPath;
+
+ public function __construct() {
+ $this->i18nPath = __DIR__ . '/ignore';
+ }
+
+ public function dump(I18nData $i18n) {
+ foreach ($i18n->getData() as $language => $content) {
+ $filename = $this->i18nPath . DIRECTORY_SEPARATOR . $language . '.php';
+ file_put_contents($filename, $this->format($content));
+ }
+ }
+
+ public function load() {
+ $i18n = array();
+ $files = new DirectoryIterator($this->i18nPath);
+ foreach ($files as $file) {
+ if (!$file->isFile()) {
+ continue;
+ }
+ $i18n[$file->getBasename('.php')] = (include $file->getPathname());
+ }
+
+ return new I18nData($i18n);
+ }
+
+ /**
+ * 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(($translation), true);
+ $patterns = array(
+ '/array \(/',
+ '/=>\s*array/',
+ '/ {2}/',
+ '/\d+ => /',
+ );
+ $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;\n", $translation);
+ }
+
+}