فهرست منبع

Add new file in i18n (#7917)

When manipulating I18N files, it is now possible to add a new file to all
languages. This action is available both in the manipulation script and
the makefile.
Alexis Degrugillier 11 ماه پیش
والد
کامیت
de624dc8ce
4فایلهای تغییر یافته به همراه62 افزوده شده و 1 حذف شده
  1. 8 0
      Makefile
  2. 26 1
      cli/i18n/I18nData.php
  3. 4 0
      cli/manipulate.translation.php
  4. 24 0
      tests/cli/i18n/I18nDataTest.php

+ 8 - 0
Makefile

@@ -95,6 +95,14 @@ node_modules/.bin/rtlcss:
 	npm install
 
 ##@ I18n
+.PHONY: i18n-add-file
+i18n-add-file: ## Add a new translation file to all supported languages
+ifndef key
+	$(error To add a new file, you need to provide one in the "key" variable)
+endif
+	@$(PHP) ./cli/manipulate.translation.php --action add --key $(key)
+	@echo File added.
+
 .PHONY: i18n-add-key
 i18n-add-key: ## Add a translation key to all supported languages
 ifndef key

+ 26 - 1
cli/i18n/I18nData.php

@@ -110,10 +110,17 @@ class I18nData {
 	 * Check if the key is known.
 	 */
 	public function isKnown(string $key): bool {
-		return array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) &&
+		return $this->exists($key) &&
 			array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)]);
 	}
 
+	/**
+	 * Check if the file exists
+	 */
+	public function exists(string $file): bool {
+		return array_key_exists($this->getFilenamePrefix($file), $this->data[static::REFERENCE_LANGUAGE]);
+	}
+
 	/**
 	 * Return the parent key for a specified key.
 	 * To get the parent key, you need to remove the last section of the key. Each
@@ -186,6 +193,24 @@ class I18nData {
 		return count($children) !== 0;
 	}
 
+	/**
+	 * Add a new translation file to all languages
+	 * @throws Exception
+	 */
+	public function addFile(string $file): void {
+		$file = strtolower($file);
+		if (!str_ends_with($file, '.php')) {
+			throw new Exception('The selected file name is not supported.');
+		}
+		if ($this->exists($file)) {
+			throw new Exception('The selected file exists already.');
+		}
+
+		foreach ($this->getAvailableLanguages() as $language) {
+			$this->data[$language][$this->getFilenamePrefix($file)] = [];
+		}
+	}
+
 	/**
 	 * Add a new key to all languages.
 	 * @throws Exception

+ 4 - 0
cli/manipulate.translation.php

@@ -43,6 +43,8 @@ switch ($cliOptions->action) {
 			$i18nData->addValue($cliOptions->key, $cliOptions->value, $cliOptions->language);
 		} elseif (isset($cliOptions->key) && isset($cliOptions->value)) {
 			$i18nData->addKey($cliOptions->key, $cliOptions->value);
+		} elseif (isset($cliOptions->key)) {
+			$i18nData->addFile($cliOptions->key);
 		} elseif (isset($cliOptions->language)) {
 			$reference = null;
 			if (isset($cliOptions->originLanguage)) {
@@ -172,6 +174,8 @@ Example 9:	revert ignore on all unmodified keys. Removes IGNORE comments from al
 Example 10:	check if a key exist.
 	php $file -a exist -k my_key
 
+Example 11:	add a new file to all languages
+	php $file -a add -k my_file.php
 HELP;
 	exit();
 }

+ 24 - 0
tests/cli/i18n/I18nDataTest.php

@@ -485,6 +485,30 @@ class I18nDataTest extends PHPUnit\Framework\TestCase {
 		self::assertSame($frValue, $enValue);
 	}
 
+	public function testAddFileWhenNotPhpFile(): void {
+		$this->expectException(\Exception::class);
+		$this->expectExceptionMessage('The selected file name is not supported.');
+
+		$data = new I18nData($this->referenceData);
+		$data->addFile('file2');
+	}
+
+	public function testAddFileWhenAlreadyExists(): void {
+		$this->expectException(\Exception::class);
+		$this->expectExceptionMessage('The selected file exists already.');
+
+		$data = new I18nData($this->referenceData);
+		self::assertTrue($data->exists('file2.php'));
+		$data->addFile('file2.php');
+	}
+
+	public function testAddFileWhenNotExists(): void {
+		$data = new I18nData($this->referenceData);
+		self::assertFalse($data->exists('newfile.php'));
+		$data->addFile('newfile.php');
+		self::assertTrue($data->exists('newfile.php'));
+	}
+
 	public function testAddValueWhenLanguageDoesNotExist(): void {
 		$this->expectException(\Exception::class);
 		$this->expectExceptionMessage('The selected language does not exist.');