aboutsummaryrefslogtreecommitdiff
path: root/cli/i18n
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2018-03-04 15:26:24 +0100
committerGravatar GitHub <noreply@github.com> 2018-03-04 15:26:24 +0100
commitf0fd273199682881b805e968ca36df4ccdbfa7a1 (patch)
tree0f87fcc515fb493193f9c58a9a0ed19f4caf07e8 /cli/i18n
parent5ebeb9e3e5d46195a83211140c1d28d58be19b2a (diff)
parenta37b95f6779e6e2035f0efb72cf5144e7fad2ea3 (diff)
Merge pull request #1810 from FreshRSS/dev1.10.1
FreshRSS 1.10.1
Diffstat (limited to 'cli/i18n')
-rw-r--r--cli/i18n/I18nData.php24
-rw-r--r--cli/i18n/I18nFile.php31
2 files changed, 52 insertions, 3 deletions
diff --git a/cli/i18n/I18nData.php b/cli/i18n/I18nData.php
index cd8ba0765..b8f958288 100644
--- a/cli/i18n/I18nData.php
+++ b/cli/i18n/I18nData.php
@@ -32,6 +32,7 @@ class I18nData {
* Add a new language. It's a copy of the reference language.
*
* @param string $language
+ * @throws Exception
*/
public function addLanguage($language) {
if (array_key_exists($language, $this->data)) {
@@ -45,6 +46,7 @@ class I18nData {
*
* @param string $key
* @param string $value
+ * @throws Exception
*/
public function addKey($key, $value) {
if (array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
@@ -54,9 +56,28 @@ class I18nData {
}
/**
+ * Add a value for a key for the selected language.
+ *
+ * @param string $key
+ * @param string $value
+ * @param string $language
+ * @throws Exception
+ */
+ public function addValue($key, $value, $language) {
+ if (!in_array($language, $this->getAvailableLanguages())) {
+ throw new Exception('The selected language does not exist.');
+ }
+ if (!array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
+ throw new Exception('The selected key does not exist for the selected language.');
+ }
+ $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
+ }
+
+ /**
* Duplicate a key from the reference language to all other languages
*
* @param string $key
+ * @throws Exception
*/
public function duplicateKey($key) {
if (!array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
@@ -68,7 +89,7 @@ class I18nData {
continue;
}
if (array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
- throw new Exception(sprintf('The selected key already exist in %s.', $language));
+ continue;
}
$this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
}
@@ -78,6 +99,7 @@ class I18nData {
* Remove a key in all languages
*
* @param string $key
+ * @throws Exception
*/
public function removeKey($key) {
if (!array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
diff --git a/cli/i18n/I18nFile.php b/cli/i18n/I18nFile.php
index d6489ee21..a07efdf88 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;\n", $translation);
+ }
+
}