Przeglądaj źródła

Add new tools to manipulate translations (#1818)

I've added a tool to add a new translation for a specific key and language.
I've added a tool to format the i18n files.

This is one of the steps to improve the translation process.
Alexis Degrugillier 8 lat temu
rodzic
commit
67c173f79b
3 zmienionych plików z 41 dodań i 4 usunięć
  1. 23 1
      cli/i18n/I18nData.php
  2. 1 1
      cli/i18n/I18nFile.php
  3. 17 2
      cli/manipulate.translation.php

+ 23 - 1
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)])) {
@@ -53,10 +55,29 @@ class I18nData {
 		$this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)][$key] = $value;
 	}
 
+	/**
+	 * 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)])) {

+ 1 - 1
cli/i18n/I18nFile.php

@@ -113,7 +113,7 @@ class i18nFile {
 		$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);
+		return sprintf("<?php\n\nreturn %s;\n", $translation);
 	}
 
 }

+ 17 - 2
cli/manipulate.translation.php

@@ -6,7 +6,7 @@ if (array_key_exists('h', $options)) {
 	help();
 }
 
-if (1 === $argc || 4 < $argc) {
+if (1 === $argc || 5 < $argc) {
 	help();
 }
 
@@ -25,12 +25,21 @@ switch ($argv[1]) {
 		}
 		$i18nData->addKey($argv[2], $argv[3]);
 		break;
+	case 'add_value':
+		if (4 === $argc) {
+			help();
+		}
+		$i18nData->addValue($argv[2], $argv[3], $argv[4]);
+		break;
 	case 'duplicate_key' :
 		$i18nData->duplicateKey($argv[2]);
 		break;
 	case 'delete_key' :
 		$i18nData->removeKey($argv[2]);
 		break;
+	case 'format' :
+		$i18nFile->dump($i18nData);
+		break;
 	default :
 		help();
 }
@@ -48,7 +57,7 @@ NAME
 	%s
 
 SYNOPSIS
-	php %s [OPTION] [OPERATION] [KEY] [VALUE]
+	php %s [OPTION] [OPERATION] [KEY] [VALUE] [LANGUAGE]
 
 DESCRIPTION
 	Manipulate translation files. Available operations are 
@@ -64,6 +73,10 @@ OPERATION
 	add_key	add a new key in the referential. This operation needs a KEY and
 		a VALUE.
 
+	add_value
+		add a value in the referential. This operation needs a KEY, a
+		VALUE, and a LANGUAGE.
+
 	duplicate_key
 		duplicate a referential key in other languages. This operation
 		needs only a KEY.
@@ -72,6 +85,8 @@ OPERATION
 		delete a referential key from all languages. This operation needs
 		only a KEY.
 
+	format  format i18n files.
+
 HELP;
 	$file = str_replace(__DIR__ . '/', '', __FILE__);
 	echo sprintf($help, $file, $file);