Skip to content

Commit 8449fc3

Browse files
committed
Change plural-rule guessing to composer.json
1 parent c8e6b9a commit 8449fc3

3 files changed

Lines changed: 21 additions & 26 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ For the easiest results, create a directory called `4.0` in the root of the Tran
1919
phpbb-translation-validator/4.0/de/
2020
phpbb-translation-validator/translation.php
2121

22-
The simplest way to validate is to then run this command (the final argument is the language you wish to test and that has already been uploaded to the `3.2` directory; eg. `fr` for French):
22+
The simplest way to validate is to then run this command (the final argument is the language you wish to test and that has already been uploaded to the `4.0` directory; eg. `fr` for French):
2323

2424
php translation.php validate fr
2525

26-
There are more arguments that can be supplied. For example, suppose you wanted to have your `3.2` directory in a different location, you wanted to explicitly specify phpBB version 3.2 (default validation is against 3.3), you wanted to run in safe mode and you wanted to see all notices displayed - you would run this command:
26+
There are more arguments that can be supplied. For example, suppose you wanted to have your `4.x` directory in a different location, you wanted to explicitly specify phpBB version 4.x (default validation is against 4.0), you wanted to run in safe mode and you wanted to see all notices displayed - you would run this command:
2727

2828
php translation.php validate fr
2929
--package-dir=/path/to/your/4.0

src/Phpbb/TranslationValidator/Validator/ValidatorRunner.php

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -228,36 +228,31 @@ protected function printErrorLevel(Output $output)
228228
}
229229

230230
/**
231-
* Try to find the plural rule for the language
231+
* Try to find the plural rule for the language in composer.json
232232
* @return int
233233
*/
234-
protected function guessPluralRule()
234+
protected function guessPluralRule(): int
235235
{
236-
$filePath = $this->originPath . '/' . $this->originLanguagePath . 'common.php';
236+
// TODO: Check for safeMode and langParser integration here in that function
237+
$filePath = $this->originPath . '/' . $this->originLanguagePath . 'composer.json';
237238

238-
if (file_exists($filePath))
239-
{
240-
if ($this->safeMode)
241-
{
242-
$lang = self::langParser($filePath);
243-
}
244-
245-
else
246-
{
247-
include($filePath);
248-
}
249-
250-
if (!isset($lang['PLURAL_RULE']))
251-
{
252-
$this->output->writelnIfDebug("<info>No plural rule set, falling back to plural rule #1</info>");
253-
}
254-
}
239+
if (file_exists($filePath))
240+
{
241+
242+
$fileContents = (string) file_get_contents($filePath);
243+
$jsonContent = json_decode($fileContents, true);
244+
245+
if (!isset($jsonContent['extra']['plural-rule']))
246+
{
247+
$this->output->writelnIfDebug("<info>No plural rule set, falling back to plural rule #1</info>");
248+
}
249+
}
255250
else
256251
{
257-
$this->output->writelnIfDebug("<info>Could not find common.php, falling back to plural rule #1</info>");
252+
$this->output->writelnIfDebug("<info>Could not find composer.json, falling back to plural rule #1</info>");
258253
}
259254

260-
return isset($lang['PLURAL_RULE']) ? $lang['PLURAL_RULE'] : 1;
255+
return $jsonContent['extra']['plural-rule'] ?? 1;
261256
}
262257

263258
/**
@@ -278,7 +273,7 @@ public static function arrayParser($file)
278273
* @param string $relativePath
279274
* @return array
280275
*/
281-
public static function langParser($filePath, $relativePath = '')
276+
public static function langParser($filePath, string $relativePath = '')
282277
{
283278
$lang = [];
284279
$parsed = self::arrayParser($relativePath . $filePath);

translation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*/
1010

11-
const TRANSLATION_VALIDATOR_VERSION = '1.6.0';
11+
const TRANSLATION_VALIDATOR_VERSION = '2.0.0-dev';
1212

1313
require 'vendor/autoload.php';
1414

0 commit comments

Comments
 (0)