Skip to content

Commit 078c97b

Browse files
authored
Merge pull request #69 from battye/issue/48
More lenient validation rules for RTL languages
2 parents 28caf76 + 64f8a6f commit 078c97b

File tree

4 files changed

+66
-18
lines changed

4 files changed

+66
-18
lines changed

src/Phpbb/TranslationValidator/Validator/FileListValidator.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
class FileListValidator
1717
{
18+
/** @var string */
19+
protected $direction = 'ltr';
1820
/** @var string */
1921
protected $originIso;
2022
/** @var string */
@@ -147,9 +149,9 @@ public function validate()
147149
include($filePath);
148150
}
149151

150-
$direction = $lang['DIRECTION'];
152+
$this->direction = $lang['DIRECTION'];
151153
// Throw error, if invalid direction is used
152-
if (!in_array($direction, array('rtl', 'ltr')))
154+
if (!in_array($this->direction, array('rtl', 'ltr')))
153155
{
154156
$this->output->addMessage(Output::FATAL, 'DIRECTION needs to be rtl or ltr');
155157
}
@@ -195,7 +197,7 @@ public function validate()
195197
{
196198
$level = Output::FATAL;
197199
}
198-
else if (in_array(substr($origin_file, -4), array('.gif', '.png')) && $direction === 'rtl')
200+
else if (in_array(substr($origin_file, -4), array('.gif', '.png')) && $this->direction === 'rtl')
199201
{
200202
$level = Output::WARNING;
201203
}
@@ -246,4 +248,13 @@ protected function getFileList($dir)
246248

247249
return $files;
248250
}
251+
252+
/**
253+
* Get the language direction - we want to be more lenient for rtl languages
254+
* @return string
255+
*/
256+
public function getDirection()
257+
{
258+
return $this->direction;
259+
}
249260
}

src/Phpbb/TranslationValidator/Validator/FileValidator.php

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
class FileValidator
1616
{
17+
/** @var string */
18+
protected $direction;
1719
/** @var string */
1820
protected $originIso;
1921
/** @var string */
@@ -39,6 +41,9 @@ class FileValidator
3941
/** @var \Phpbb\TranslationValidator\Output\OutputInterface */
4042
protected $output;
4143

44+
/** @var LangKeyValidator */
45+
protected $langKeyValidator;
46+
4247
/** @var array List from https://developers.google.com/recaptcha/docs/language */
4348
private $reCaptchaLanguages = [
4449
'ar',
@@ -122,7 +127,19 @@ public function __construct(InputInterface $input, OutputInterface $output)
122127
{
123128
$this->input = $input;
124129
$this->output = $output;
125-
$this->langkeyValidator = new LangKeyValidator($input, $output);
130+
$this->langKeyValidator = new LangKeyValidator($input, $output);
131+
}
132+
133+
/**
134+
* Set the language direction
135+
* @param $direction
136+
* @return $this
137+
*/
138+
public function setDirection($direction)
139+
{
140+
$this->direction = $direction;
141+
$this->langKeyValidator->setDirection($direction);
142+
return $this;
126143
}
127144

128145
/**
@@ -138,7 +155,7 @@ public function setOrigin($originIso, $originPath, $originLanguagePath)
138155
$this->originIso = $originIso;
139156
$this->originPath = $originPath;
140157
$this->originLanguagePath = $originLanguagePath;
141-
$this->langkeyValidator->setOrigin($originIso, $originPath, $originLanguagePath);
158+
$this->langKeyValidator->setOrigin($originIso, $originPath, $originLanguagePath);
142159
return $this;
143160
}
144161

@@ -155,7 +172,7 @@ public function setSource($sourceIso, $sourcePath, $sourceLanguagePath)
155172
$this->sourceIso = $sourceIso;
156173
$this->sourcePath = $sourcePath;
157174
$this->sourceLanguagePath = $sourceLanguagePath;
158-
$this->langkeyValidator->setSource($sourceIso, $sourcePath, $sourceLanguagePath);
175+
$this->langKeyValidator->setSource($sourceIso, $sourcePath, $sourceLanguagePath);
159176
return $this;
160177
}
161178

@@ -168,7 +185,7 @@ public function setSource($sourceIso, $sourcePath, $sourceLanguagePath)
168185
public function setPhpbbVersion($phpbbVersion)
169186
{
170187
$this->phpbbVersion = $phpbbVersion;
171-
$this->langkeyValidator->setPhpbbVersion($phpbbVersion);
188+
$this->langKeyValidator->setPhpbbVersion($phpbbVersion);
172189
return $this;
173190
}
174191

@@ -181,7 +198,7 @@ public function setPhpbbVersion($phpbbVersion)
181198
public function setPluralRule($pluralRule)
182199
{
183200
$this->pluralRule = $pluralRule;
184-
$this->langkeyValidator->setPluralRule($pluralRule);
201+
$this->langKeyValidator->setPluralRule($pluralRule);
185202
return $this;
186203
}
187204

@@ -194,7 +211,7 @@ public function setPluralRule($pluralRule)
194211
public function setDebug($debug)
195212
{
196213
$this->debug = $debug;
197-
$this->langkeyValidator->setDebug($debug);
214+
$this->langKeyValidator->setDebug($debug);
198215
return $this;
199216
}
200217

@@ -346,7 +363,7 @@ public function validateLangFile($sourceFile, $originFile)
346363
continue;
347364
}
348365

349-
$this->langkeyValidator->validate($originFile, $againstLangKey, $againstLanguage, $validate[$againstLangKey]);
366+
$this->langKeyValidator->validate($originFile, $againstLangKey, $againstLanguage, $validate[$againstLangKey]);
350367
}
351368

352369
foreach ($validate as $validateLangKey => $validateLanguage)
@@ -555,13 +572,13 @@ public function validateHelpFile($sourceFile, $originFile)
555572
if (isset($help[0]))
556573
{
557574
$compare = isset($against[$entry][0]) ? $against[$entry][0] : '';
558-
$this->langkeyValidator->validate($originFile, $entry . '.0', $compare, $help[0]);
575+
$this->langKeyValidator->validate($originFile, $entry . '.0', $compare, $help[0]);
559576
}
560577

561578
if (isset($help[1]))
562579
{
563580
$compare = isset($against[$entry][1]) ? $against[$entry][1] : '';
564-
$this->langkeyValidator->validate($originFile, $entry . '.1', $compare, $help[1]);
581+
$this->langKeyValidator->validate($originFile, $entry . '.1', $compare, $help[1]);
565582
}
566583
$entry++;
567584
}
@@ -817,7 +834,8 @@ public function validateCSSFile($sourceFile, $originFile)
817834
}
818835
if (!empty($additionalRules))
819836
{
820-
$this->output->addMessage(Output::FATAL, 'Stylesheet file has additional CSS rules: ' . implode(', ', $additionalRules), $originFile);
837+
$additionalRulesLevel = ($this->direction == 'rtl') ? Output::WARNING : Output::FATAL; // be more lenient for RTL
838+
$this->output->addMessage($additionalRulesLevel, 'Stylesheet file has additional CSS rules: ' . implode(', ', $additionalRules), $originFile);
821839
}
822840
}
823841

src/Phpbb/TranslationValidator/Validator/LangKeyValidator.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
class LangKeyValidator
1717
{
18+
/** @var string */
19+
protected $direction;
1820
/** @var string */
1921
protected $originIso;
2022
/** @var string */
@@ -61,6 +63,17 @@ public function __construct(InputInterface $input, OutputInterface $output)
6163
$this->output = $output;
6264
}
6365

66+
/**
67+
* Set the language direction
68+
* @param $direction
69+
* @return $this
70+
*/
71+
public function setDirection($direction)
72+
{
73+
$this->direction = $direction;
74+
return $this;
75+
}
76+
6477
/**
6578
* Set phpBB Version
6679
*
@@ -777,6 +790,11 @@ protected function getErrorLevelForAdditionalHtml($html)
777790
return Output::ERROR;
778791
}
779792

793+
if ($this->direction == 'rtl' && (strpos($html, 'ltr') !== false || strpos($html, 'rtl') !== false))
794+
{
795+
return Output::WARNING; // be more lenient for RTL
796+
}
797+
780798
return Output::FATAL;
781799
}
782800
}

src/Phpbb/TranslationValidator/Validator/ValidatorRunner.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ public function setDebug($debug)
138138
*/
139139
public function runValidators()
140140
{
141-
$filelistValidator = new FileListValidator($this->input, $this->output);
141+
$fileListValidator = new FileListValidator($this->input, $this->output);
142142

143-
$validateFiles = $filelistValidator->setSource($this->sourceIso, $this->sourcePath, $this->sourceLanguagePath)
143+
$validateFiles = $fileListValidator->setSource($this->sourceIso, $this->sourcePath, $this->sourceLanguagePath)
144144
->setOrigin($this->originIso, $this->originPath, $this->originLanguagePath)
145145
->setPhpbbVersion($this->phpbbVersion)
146146
->setDebug($this->debug)
@@ -164,9 +164,10 @@ public function runValidators()
164164
$this->maxProgress = sizeof($validateFiles) + 1;
165165
$this->progressLength = 11 + strlen($this->maxProgress) * 2;
166166

167-
$filelistValidator = new FileValidator($this->input, $this->output);
168-
$filelistValidator->setSource($this->sourceIso, $this->sourcePath, $this->sourceLanguagePath)
167+
$fileValidator = new FileValidator($this->input, $this->output);
168+
$fileValidator->setSource($this->sourceIso, $this->sourcePath, $this->sourceLanguagePath)
169169
->setOrigin($this->originIso, $this->originPath, $this->originLanguagePath)
170+
->setDirection($fileListValidator->getDirection())
170171
->setPhpbbVersion($this->phpbbVersion)
171172
->setPluralRule($pluralRule)
172173
->setDebug($this->debug)
@@ -176,7 +177,7 @@ public function runValidators()
176177
{
177178
$this->output->writelnIfDebug('');
178179
$this->output->writelnIfDebug("Validating file: $originFile");
179-
$filelistValidator->validate($sourceFile, $originFile);
180+
$fileValidator->validate($sourceFile, $originFile);
180181
$this->printErrorLevel($this->output);
181182

182183
usleep(31250);//125000);

0 commit comments

Comments
 (0)