10000 [Translation] Added support for translation files with other filename… · symfony/symfony@0ee912d · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ee912d

Browse files
javiereguiluzfabpot
authored andcommitted
[Translation] Added support for translation files with other filename patterns
1 parent 683bbf9 commit 0ee912d

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

src/Symfony/Component/Translation/Command/XliffLintCommand.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ class XliffLintCommand extends Command
3333
private $displayCorrectFiles;
3434
private $directoryIteratorProvider;
3535< 10000 div class="diff-text-inner"> private $isReadableProvider;
36+
private $requireStrictFileNames;
3637

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

4142
$this->directoryIteratorProvider = $directoryIteratorProvider;
4243
$this->isReadableProvider = $isReadableProvider;
44+
$this->requireStrictFileNames = $requireStrictFileNames;
4345
}
4446

4547
/**
@@ -115,14 +117,16 @@ private function validate($content, $file = null)
115117
$document->loadXML($content);
116118

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

121-
if ($expectedFileExtension !== $realFileExtension) {
125+
if (0 === preg_match($expectedFilenamePattern, basename($file))) {
122126
$errors[] = array(
123127
'line' => -1,
124128
'column' => -1,
125-
'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),
129+
'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),
126130
);
127131
}
128132
}

src/Symfony/Component/Translation/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)
0