aboutsummaryrefslogtreecommitdiff
path: root/cli/i18n/I18nFile.php
diff options
context:
space:
mode:
authorGravatar Alexis Degrugillier <aledeg@users.noreply.github.com> 2017-11-04 21:17:08 +0100
committerGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2017-11-04 21:17:08 +0100
commit05b1901fcdbb051077d12f776980484d3b782970 (patch)
tree1f005b468544ca6b16e9d1ac63c2d78b00812745 /cli/i18n/I18nFile.php
parentfe84e6135e7bfb35085fafbc7191ba6306bdb20d (diff)
Move translation tools into the cli folder (#1673)
Translation tools must be used on cli. It is better to have them in the cli folder.
Diffstat (limited to 'cli/i18n/I18nFile.php')
-rw-r--r--cli/i18n/I18nFile.php92
1 files changed, 92 insertions, 0 deletions
diff --git a/cli/i18n/I18nFile.php b/cli/i18n/I18nFile.php
new file mode 100644
index 000000000..d6489ee21
--- /dev/null
+++ b/cli/i18n/I18nFile.php
@@ -0,0 +1,92 @@
+<?php
+
+require_once __DIR__ . '/I18nData.php';
+
+class i18nFile {
+
+ private $i18nPath;
+
+ public function __construct() {
+ $this->i18nPath = __DIR__ . '/../../app/i18n';
+ }
+
+ public function load() {
+ $dirs = new DirectoryIterator($this->i18nPath);
+ foreach ($dirs as $dir) {
+ if ($dir->isDot()) {
+ continue;
+ }
+ $files = new DirectoryIterator($dir->getPathname());
+ foreach ($files as $file) {
+ if (!$file->isFile()) {
+ continue;
+ }
+ $i18n[$dir->getFilename()][$file->getFilename()] = $this->flatten(include $file->getPathname(), $file->getBasename('.php'));
+ }
+ }
+
+ return new I18nData($i18n);
+ }
+
+ public function dump(I18nData $i18n) {
+ foreach ($i18n->getData() as $language => $file) {
+ $dir = $this->i18nPath . DIRECTORY_SEPARATOR . $language;
+ if (!file_exists($dir)) {
+ mkdir($dir);
+ }
+ 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));
+ }
+ }
+ }
+
+ /**
+ * Flatten an array of translation
+ *
+ * @param array $translation
+ * @param string $prefix
+ * @return array
+ */
+ private function flatten($translation, $prefix = '') {
+ $a = array();
+
+ if ('' !== $prefix) {
+ $prefix .= '.';
+ }
+
+ foreach ($translation as $key => $value) {
+ if (is_array($value)) {
+ $a += $this->flatten($value, $prefix . $key);
+ } else {
+ $a[$prefix . $key] = $value;
+ }
+ }
+
+ return $a;
+ }
+
+ /**
+ * Unflatten an array of translation
+ *
+ * The first key is dropped since it represents the filename and we have
+ * no use of it.
+ *
+ * @param array $translation
+ * @return array
+ */
+ private function unflatten($translation) {
+ $a = array();
+
+ ksort($translation);
+ foreach ($translation as $compoundKey => $value) {
+ $keys = explode('.', $compoundKey);
+ array_shift($keys);
+ eval("\$a['" . implode("']['", $keys) . "'] = '" . $value . "';");
+ }
+
+ return $a;
+ }
+
+}