-
Notifications
You must be signed in to change notification settings - Fork 1
Validate target-language in XLF files #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
konradmichalik
merged 7 commits into
move-elevator:main
from
maikschneider:target-language-check
May 3, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f59930a
feat: validate target-language against filename prefix
maikschneider 86e1176
fix: correct error message for XML parse failure
maikschneider c2e79d1
feat: add language detection of XLF file names, read target language
maikschneider 260d6e0
feat: refactor xlf validator to read language from parser
maikschneider f5d1012
fix: use correct attribute name in XLIFF 2.x error message
maikschneider c8a2bfe
fix: extract only base language from filename locale prefix
maikschneider 6dae368
fix: stripe region suffix from targetLanguage
maikschneider File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| use Symfony\Component\Translation\Util\XliffUtils; | ||
|
|
||
| use function sprintf; | ||
| use function strtolower; | ||
|
|
||
| /** | ||
| * XliffSchemaValidator. | ||
|
|
@@ -31,40 +32,79 @@ class XliffSchemaValidator extends AbstractValidator implements ValidatorInterfa | |
| { | ||
| public function processFile(ParserInterface $file): array | ||
| { | ||
| try { | ||
| /* | ||
| * With XmlUtils::loadFile() we always get a strange symfony error related to global composer autoloading issue. | ||
| * Call to undefined method Symfony\Component\Filesystem\Filesystem::readFile() | ||
| */ | ||
| if (!file_exists($file->getFilePath())) { | ||
| $this->logger?->error('File does not exist: '.$file->getFileName()); | ||
|
|
||
| return []; | ||
| } | ||
| /* | ||
| * With XmlUtils::loadFile() we always get a strange symfony error related to global composer autoloading issue. | ||
| * Call to undefined method Symfony\Component\Filesystem\Filesystem::readFile() | ||
| */ | ||
| if (!file_exists($file->getFilePath())) { | ||
| $this->logger?->error('File does not exist: '.$file->getFileName()); | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| $fileContent = file_get_contents($file->getFilePath()); | ||
| if (false === $fileContent) { | ||
| $this->logger?->error('Failed to read file: '.$file->getFileName()); | ||
| $fileContent = file_get_contents($file->getFilePath()); | ||
| if (false === $fileContent) { | ||
| $this->logger?->error('Failed to read file: '.$file->getFileName()); | ||
|
|
||
| return []; | ||
| } | ||
| return []; | ||
| } | ||
|
|
||
| try { | ||
| $dom = XmlUtils::parse($fileContent); | ||
| } catch (Exception $e) { | ||
| $this->logger?->error('Failed to parse XML: '.$e->getMessage()); | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| // Schema validation — may throw for unsupported XLIFF versions (e.g. 2.x) | ||
| $errors = []; | ||
| try { | ||
| $errors = XliffUtils::validateSchema($dom); | ||
| } catch (Exception $e) { | ||
| if (str_contains($e->getMessage(), 'No support implemented for loading XLIFF version')) { | ||
| $this->logger?->notice(sprintf('Skipping %s: %s', $this->getShortName(), $e->getMessage())); | ||
| } else { | ||
|
Comment on lines
65
to
67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Notice wording is misleading (
🤖 Prompt for AI Agents |
||
| $this->logger?->error('Failed to validate XML schema: '.$e->getMessage()); | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| if (!empty($errors)) { | ||
| // Additional check: if filename encodes a locale, verify it matches target-language in the file header | ||
| if (!$file instanceof XliffParser) { | ||
| return $errors; | ||
| } | ||
|
|
||
| return []; | ||
| $expectedLanguage = $file->getLanguageFromFileName(); | ||
| if (null !== $expectedLanguage) { | ||
| $targetLang = $file->getTargetLanguage(); | ||
| $isVersion2 = $file->isVersion2(); | ||
| $attribute = $isVersion2 ? 'trgLang' : 'target-language'; | ||
| $element = $isVersion2 ? '<xliff>' : '<file>'; | ||
|
|
||
| if (null === $targetLang) { | ||
| $errors[] = [ | ||
| 'message' => sprintf( | ||
| 'Missing "%s" attribute on %s node; expected "%s" based on filename', | ||
| $attribute, | ||
| $element, | ||
| $expectedLanguage, | ||
| ), | ||
| 'level' => 'ERROR', | ||
| ]; | ||
| } elseif (strtolower($targetLang) !== $expectedLanguage) { | ||
| $errors[] = [ | ||
| 'message' => sprintf( | ||
| '"%s" attribute "%s" does not match filename language "%s"', | ||
| $attribute, | ||
| $targetLang, | ||
| $expectedLanguage, | ||
| ), | ||
| 'level' => 'ERROR', | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| return $errors; | ||
| } | ||
|
|
||
| public function formatIssueMessage(Issue $issue, string $prefix = ''): string | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.