[Translation] Allow using dashes in locale when linting Xliff files

This commit is contained in:
Andreas Möller 2021-02-12 20:06:23 +01:00 committed by Fabien Potencier
parent aad1d094a5
commit d106aa3f2d
2 changed files with 13 additions and 2 deletions

View File

@ -129,12 +129,12 @@ EOF
$document->loadXML($content);
if (null !== $targetLanguage = $this->getTargetLanguageFromFile($document)) {
$normalizedLocale = preg_quote(str_replace('-', '_', $targetLanguage), '/');
$normalizedLocalePattern = sprintf('(%s|%s)', preg_quote($targetLanguage, '/'), preg_quote(str_replace('-', '_', $targetLanguage), '/'));
// strict file names require translation files to be named '____.locale.xlf'
// otherwise, both '____.locale.xlf' and 'locale.____.xlf' are allowed
// also, the regexp matching must be case-insensitive, as defined for 'target-language' values
// http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#target-language
$expectedFilenamePattern = $this->requireStrictFileNames ? sprintf('/^.*\.(?i:%s)\.(?:xlf|xliff)/', $normalizedLocale) : sprintf('/^(?:.*\.(?i:%s)|(?i:%s)\..*)\.(?:xlf|xliff)/', $normalizedLocale, $normalizedLocale);
$expectedFilenamePattern = $this->requireStrictFileNames ? sprintf('/^.*\.(?i:%s)\.(?:xlf|xliff)/', $normalizedLocalePattern) : sprintf('/^(?:.*\.(?i:%s)|(?i:%s)\..*)\.(?:xlf|xliff)/', $normalizedLocalePattern, $normalizedLocalePattern);
if (0 === preg_match($expectedFilenamePattern, basename($file))) {
$errors[] = [

View File

@ -105,6 +105,17 @@ class XliffLintCommandTest extends TestCase
$this->assertStringContainsString('[OK] All 1 XLIFF files contain valid syntax.', trim($tester->getDisplay()));
}
public function testLintSucceedsWhenLocaleInFileAndInTargetLanguageNameUsesDashesInsteadOfUnderscores()
{
$tester = $this->createCommandTester();
$filename = $this->createFile('note', 'en-GB', 'messages.en-GB.xlf');
$tester->execute(['filename' => $filename], ['decorated' => false]);
$this->assertSame(0, $tester->getStatusCode());
$this->assertStringContainsString('[OK] All 1 XLIFF files contain valid syntax.', trim($tester->getDisplay()));
}
public function testLintFileNotReadable()
{
$this->expectException(\RuntimeException::class);