-
Notifications
You must be signed in to change notification settings - Fork 275
π fix(qa): validate ICU patterns on locked segments at project creation #4513
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
Changes from all commits
2ce8256
a78edea
62cfbbc
e0535bf
1931571
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,12 @@ | |
|
|
||
| namespace Model\ProjectCreation; | ||
|
|
||
| use Exception; | ||
| use Matecat\ICU\MessagePatternComparator; | ||
| use Matecat\ICU\MessagePatternValidator; | ||
| use Matecat\SubFiltering\MateCatFilter; | ||
| use Model\FeaturesBase\FeatureSet; | ||
| use Utils\LQA\ICUSourceSegmentDetector; | ||
| use Utils\LQA\QA; | ||
|
|
||
| /** | ||
|
|
@@ -13,6 +17,10 @@ | |
| * selects the appropriate target string, converts back to Layer 0, and writes the results | ||
| * onto each {@see TranslationTuple} in place. | ||
| * | ||
| * When ICU is enabled for the project, the processor also detects ICU MessageFormat | ||
| * patterns in each segment and passes a {@see MessagePatternComparator} to QA so that | ||
| * ICU consistency is validated alongside tag checks. | ||
| * | ||
|
Comment on lines
+20
to
+23
|
||
| * Produces 4 scalars per tuple: | ||
| * - translationLayer0: the QA-processed target (Layer 0) | ||
| * - suggestionLayer0: same as translationLayer0 (may diverge in future) | ||
|
|
@@ -24,6 +32,7 @@ class QAProcessor | |
| public function __construct( | ||
| private readonly MateCatFilter $filter, | ||
| private readonly FeatureSet $features, | ||
| private readonly bool $icuEnabled = false, | ||
| ) { | ||
| } | ||
|
|
||
|
|
@@ -35,6 +44,8 @@ public function __construct( | |
| * @param ProjectStructure $projectStructure contains translations to process | ||
| * @param string $sourceLang source language code (e.g. 'en-US') | ||
| * @param string $targetLang target language code (e.g. 'it-IT') | ||
| * | ||
| * @throws Exception | ||
| */ | ||
| public function process( | ||
| ProjectStructure $projectStructure, | ||
|
|
@@ -50,7 +61,14 @@ public function process( | |
| $source = $this->filter->fromLayer0ToLayer1($tuple->source); | ||
| $target = $this->filter->fromLayer0ToLayer1($tuple->target); | ||
|
|
||
| $check = $this->createQA($source, $target); | ||
| [$comparator, $sourceContainsIcu] = $this->detectIcu( | ||
| $sourceLang, | ||
| $targetLang, | ||
| $tuple->source, | ||
| $tuple->target, | ||
| ); | ||
|
|
||
| $check = $this->createQA($source, $target, $comparator, $sourceContainsIcu); | ||
| $check->setFeatureSet($this->features); | ||
| $check->setSourceSegLang($sourceLang); | ||
| $check->setTargetSegLang($targetLang); | ||
|
|
@@ -70,12 +88,62 @@ public function process( | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Detect ICU MessageFormat patterns in the source segment. | ||
| * | ||
| * Uses the raw Layer 0 content for detection because curly-bracket filter | ||
| * handlers are disabled by default at project-creation time, so ICU syntax | ||
| * survives the L0βL1 round-trip. | ||
| * | ||
| * @param string $sourceLang source language code | ||
| * @param string $targetLang target language code | ||
| * @param string $rawSource raw Layer 0 source content | ||
| * @param string $rawTarget raw Layer 0 target content | ||
| * | ||
| * @return array{0: ?MessagePatternComparator, 1: bool} | ||
| * [comparator (null when ICU is not detected), sourceContainsIcu flag] | ||
| */ | ||
| private function detectIcu( | ||
| string $sourceLang, | ||
| string $targetLang, | ||
| string $rawSource, | ||
| string $rawTarget, | ||
| ): array { | ||
| if (!$this->icuEnabled) { | ||
| return [null, false]; | ||
| } | ||
|
|
||
| $sourceValidator = new MessagePatternValidator($sourceLang, $rawSource); | ||
|
|
||
| $sourceContainsIcu = ICUSourceSegmentDetector::sourceContainsIcu($sourceValidator, $this->icuEnabled); | ||
|
|
||
|
Ostico marked this conversation as resolved.
|
||
| if (!$sourceContainsIcu) { | ||
| return [null, false]; | ||
| } | ||
|
|
||
| $targetValidator = new MessagePatternValidator($targetLang, $rawTarget); | ||
|
|
||
| return [ | ||
| MessagePatternComparator::fromValidators($sourceValidator, $targetValidator), | ||
| true, | ||
| ]; | ||
|
Ostico marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** | ||
| * Create a new QA instance. | ||
| * Protected so test subclasses can override to inject stubs. | ||
| * | ||
| * @param string $source Layer 1 source segment | ||
| * @param string $target Layer 1 target segment | ||
| * @param MessagePatternComparator|null $comparator ICU pattern comparator (null when ICU not detected) | ||
| * @param bool $sourceContainsIcu whether the source contains ICU patterns | ||
| */ | ||
| protected function createQA(string $source, string $target): QA | ||
| { | ||
| return new QA($source, $target); | ||
| protected function createQA( | ||
| string $source, | ||
| string $target, | ||
| ?MessagePatternComparator $comparator = null, | ||
| bool $sourceContainsIcu = false, | ||
| ): QA { | ||
| return new QA($source, $target, $comparator, $sourceContainsIcu); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
| namespace Utils\LQA; | ||
|
|
||
| use Matecat\ICU\MessagePatternValidator; | ||
|
|
||
| /** | ||
| * Shared ICU source-segment detection logic. | ||
| */ | ||
| final class ICUSourceSegmentDetector | ||
| { | ||
| /** | ||
| * Determines whether a source segment contains valid ICU MessageFormat patterns. | ||
| * | ||
| * @param MessagePatternValidator $validator ICU validator for the source segment | ||
| * @param bool $icuEnabled Whether ICU support is enabled for the current project | ||
| * | ||
| * @return bool True when ICU is enabled and the source has valid complex ICU syntax | ||
| */ | ||
| public static function sourceContainsIcu(MessagePatternValidator $validator, bool $icuEnabled): bool | ||
| { | ||
| return $icuEnabled | ||
| && $validator->containsComplexSyntax() | ||
| && $validator->isValidSyntax(); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.