Skip to content

Commit b19b8e2

Browse files
committed
feature #27655 [Translation] Added support for translation files with other filename patterns (javiereguiluz)
This PR was squashed before being merged into the 4.2-dev branch (closes #27655). Discussion ---------- [Translation] Added support for translation files with other filename patterns | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #27644 | License | MIT | Doc PR | - This implements the #27644 feature request in case we accept it. My vote is 👍 because the changes required are tiny and the resulting code is even more robust thanks to the new `preg_match()` call. Commits ------- 0ee912dbd8 [Translation] Added support for translation files with other filename patterns
2 parents 54077c3 + deab768 commit b19b8e2

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

Command/XliffLintCommand.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ class XliffLintCommand extends Command
3434
private $displayCorrectFiles;
3535
private $directoryIteratorProvider;
3636
private $isReadableProvider;
37+
private $requireStrictFileNames;
3738

38-
public function __construct(string $name = null, callable $directoryIteratorProvider = null, callable $isReadableProvider = null)
39+
public function __construct(string $name = null, callable $directoryIteratorProvider = null, callable $isReadableProvider = null, bool $requireStrictFileNames = true)
3940
{
4041
parent::__construct($name);
4142

4243
$this->directoryIteratorProvider = $directoryIteratorProvider;
4344
$this->isReadableProvider = $isReadableProvider;
45+
$this->requireStrictFileNames = $requireStrictFileNames;
4446
}
4547

4648
/**
@@ -116,14 +118,16 @@ private function validate($content, $file = null)
116118
$document->loadXML($content);
117119

118120
if (null !== $targetLanguage = $this->getTargetLanguageFromFile($document)) {
119-
$expectedFileExtension = sprintf('%s.xlf', str_replace('-', '_', $targetLanguage));
120-
$realFileExtension = explode('.', basename($file), 2)[1] ?? '';
121+
$normalizedLocale = preg_quote(str_replace('-', '_', $targetLanguage), '/');
122+
// strict file names require translation files to be named '____.locale.xlf'
123+
// otherwise, both '____.locale.xlf' and 'locale.____.xlf' are allowed
124+
$expectedFilenamePattern = $this->requireStrictFileNames ? sprintf('/^.*\.%s\.xlf/', $normalizedLocale) : sprintf('/^(.*\.%s\.xlf|%s\..*\.xlf)/', $normalizedLocale, $normalizedLocale);
121125

122-
if ($expectedFileExtension !== $realFileExtension) {
126+
if (0 === preg_match($expectedFilenamePattern, basename($file))) {
123127
$errors[] = array(
124128
'line' => -1,
125129
'column' => -1,
126-
'message' => sprintf('There is a mismatch between the file extension ("%s") and the "%s" value used in the "target-language" attribute of the file.', $realFileExtension, $targetLanguage),
130+
'message' => sprintf('There is a mismatch between the language included in the file name ("%s") and the "%s" value used in the "target-language" attribute of the file.', basename($file), $targetLanguage),
127131
);
128132
}
129133
}

Tests/Command/XliffLintCommandTest.php

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ public function testLintCorrectFile()
4040
$this->assertContains('OK', trim($tester->getDisplay()));
4141
}
4242

43+
/**
44+
* @dataProvider provideStrictFilenames
45+
*/
46+
public function testStrictFilenames($requireStrictFileNames, $fileNamePattern, $targetLanguage, $mustFail)
47+
{
48+
$tester = $this->createCommandTester($requireStrictFileNames);
49+
$filename = $this->createFile('note', $targetLanguage, $fileNamePattern);
50+
51+
$tester->execute(
52+
array('filename' => $filename),
53+
array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false)
54+
);
55+
56+
$this->assertEquals($mustFail ? 1 : 0, $tester->getStatusCode());
57+
$this->assertContains($mustFail ? '[WARNING] 0 XLIFF files have valid syntax and 1 contain errors.' : '[OK] All 1 XLIFF files contain valid syntax.', $tester->getDisplay());
58+
}
59+
4360
public function testLintIncorrectXmlSyntax()
4461
{
4562
$tester = $this->createCommandTester();
@@ -59,7 +76,7 @@ public function testLintIncorrectTargetLanguage()
5976
$tester->execute(array('filename' => $filename), array('decorated' => false));
6077

6178
$this->assertEquals(1, $tester->getStatusCode(), 'Returns 1 in case of error');
62-
$this->assertContains('There is a mismatch between the file extension ("en.xlf") and the "es" value used in the "target-language" attribute of the file.', trim($tester->getDisplay()));
79+
$this->assertContains('There is a mismatch between the language included in the file name ("messages.en.xlf") and the "es" value used in the "target-language" attribute of the file.', trim($tester->getDisplay()));
6380
}
6481

6582
/**
@@ -102,7 +119,7 @@ public function testGetHelp()
102119
/**
103120
* @return string Path to the new file
104121
*/
105-
private function createFile($sourceContent = 'note', $targetLanguage = 'en')
122+
private function createFile($sourceContent = 'note', $targetLanguage = 'en', $fileNamePattern = 'messages.%locale%.xlf')
106123
{
107124
$xliffContent = <<<XLIFF
108125
<?xml version="1.0"?>
@@ -118,7 +135,7 @@ private function createFile($sourceContent = 'note', $targetLanguage = 'en')
118135
</xliff>
119136
XLIFF;
120137

121-
$filename = sprintf('%s/translation-xliff-lint-test/messages.en.xlf', sys_get_temp_dir());
138+
$filename = sprintf('%s/translation-xliff-lint-test/%s', sys_get_temp_dir(), str_replace('%locale%', 'en', $fileNamePattern));
122139
file_put_contents($filename, $xliffContent);
123140

124141
$this->files[] = $filename;
@@ -129,11 +146,11 @@ private function createFile($sourceContent = 'note', $targetLanguage = 'en')
129146
/**
130147
* @return CommandTester
131148
*/
132-
private function createCommandTester($application = null)
149+
private function createCommandTester($requireStrictFileNames = true, $application = null)
133150
{
134151
if (!$application) {
135152
$application = new Application();
136-
$application->add(new XliffLintCommand());
153+
$application->add(new XliffLintCommand(null, null, null, $requireStrictFileNames));
137154
}
138155

139156
$command = $application->find('lint:xliff');
@@ -160,4 +177,16 @@ protected function tearDown()
160177
}
161178
rmdir(sys_get_temp_dir().'/translation-xliff-lint-test');
162179
}
180+
181+
public function provideStrictFilenames()
182+
{
183+
yield array(false, 'messages.%locale%.xlf', 'en', false);
184+
yield array(false, 'messages.%locale%.xlf', 'es', true);
185+
yield array(false, '%locale%.messages.xlf', 'en', false);
186+
yield array(false, '%locale%.messages.xlf', 'es', true);
187+
yield array(true, 'messages.%locale%.xlf', 'en', false);
188+
yield array(true, 'messages.%locale%.xlf', 'es', true);
189+
yield array(true, '%locale%.messages.xlf', 'en', true);
190+
yield array(true, '%locale%.messages.xlf', 'es', true);
191+
}
163192
}

0 commit comments

Comments
 (0)