bug #40172 [Translation] Allow using dashes in locale when linting Xliff files (localheinz)

This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | fixes #40170
| License       | MIT
| Doc PR        | n/a

This pull request

* [x] asserts that the `XliffLintCommand` succeeds linting an Xliff file where both the the target language and the locale in the file name use dashes as separators
* [x] adjusts the `XliffLintCommand` to allow using the same value for target language and locale in the corresponding file name

Commits
-------

d106aa3f2d [Translation] Allow using dashes in locale when linting Xliff files
This commit is contained in:
Fabien Potencier 2021-02-16 07:36:55 +01:00
commit aa21944d37
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);