Skip to content

fix: improve two-two-way-mismatch validation#2

Merged
konradmichalik merged 1 commit intomainfrom
two-way-mismatch
May 28, 2025
Merged

fix: improve two-two-way-mismatch validation#2
konradmichalik merged 1 commit intomainfrom
two-way-mismatch

Conversation

@konradmichalik
Copy link
Copy Markdown
Contributor

@konradmichalik konradmichalik commented May 28, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Improved detection of missing translation keys by considering discrepancies in both directions between language files.
    • Enhanced display for missing or empty translation values with a clearer placeholder.
  • New Features

    • Expanded support for XLIFF files to include both .xlf and .xliff extensions.
    • Added the ability to retrieve either source or target content from translation units.
  • Documentation

    • Updated README to clarify terminology and supported file formats.
  • Tests

    • Updated test translation files to reflect new key values and scenarios for improved validation coverage.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented May 28, 2025

Walkthrough

The updates broaden terminology in documentation, enhance translation validation to detect mismatches in both directions, and refine output formatting for missing translations. The XLIFF parser now supports retrieving either source or target content by key. Test XLIFF files are updated to reflect new key values and structure.

Changes

File(s) Change Summary
README.md Generalized language in documentation and clarified supported XLIFF file extensions.
src/Command/ValidateTranslationCommand.php Improved missing key detection logic, updated output formatting, and refined success messaging.
src/Parser/XliffParser.php Modified getContentByKey to accept an attribute parameter, allowing dynamic source/target access.
tests/Build/app/Language/XLIFF-fail/de.locallang.xlf
tests/Build/app/Language/XLIFF-fail/de.locallang_be.xlf
Updated target translation values and added a new translation unit in XLIFF files.
tests/Build/app/Language/XLIFF-fail/locallang.xlf
tests/Build/app/Language/XLIFF-fail/locallang_be.xlf
Updated source translation values in XLIFF files.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ValidateCommand
    participant XliffParser

    User->>ValidateCommand: Run translation validation
    ValidateCommand->>XliffParser: getContentByKey(key, "source")
    ValidateCommand->>XliffParser: getContentByKey(key, "target")
    ValidateCommand->>ValidateCommand: Compare keys in source and target
    alt Missing or extra keys found
        ValidateCommand->>User: Display table with missing/extra keys (yellow dash for empty)
    else No mismatches
        ValidateCommand->>User: Output "No translation mismatches found."
    end
Loading

Poem

🐇
A hop through XLIFF fields anew,
Source and target—now both in view!
Dashes fill the empty space,
No mismatches left to trace.
Docs and tests both join the dance,
In every language, we enhance!
🌼

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b645dd9 and 185dd10.

⛔ Files ignored due to path filters (1)
  • docs/console.jpg is excluded by !**/*.jpg
📒 Files selected for processing (7)
  • README.md (1 hunks)
  • src/Command/ValidateTranslationCommand.php (4 hunks)
  • src/Parser/XliffParser.php (1 hunks)
  • tests/Build/app/Language/XLIFF-fail/de.locallang.xlf (1 hunks)
  • tests/Build/app/Language/XLIFF-fail/de.locallang_be.xlf (1 hunks)
  • tests/Build/app/Language/XLIFF-fail/locallang.xlf (1 hunks)
  • tests/Build/app/Language/XLIFF-fail/locallang_be.xlf (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
src/Parser/XliffParser.php (1)
src/Parser/ParserInterface.php (1)
  • getContentByKey (21-21)
src/Command/ValidateTranslationCommand.php (2)
src/Parser/XliffParser.php (4)
  • getFileDirectory (66-69)
  • getFileName (61-64)
  • extractKeys (32-40)
  • getContentByKey (42-51)
src/Parser/ParserInterface.php (2)
  • extractKeys (19-19)
  • getContentByKey (21-21)
🔇 Additional comments (14)
src/Parser/XliffParser.php (1)

46-46: LGTM: Dynamic attribute access implementation.

The change from hardcoded $unit->source to dynamic $unit->{$attribute} correctly implements the flexibility to retrieve either source or target content. The logic properly handles empty strings by returning null.

tests/Build/app/Language/XLIFF-fail/locallang.xlf (1)

7-7: LGTM: Test data updated for enhanced validation scenarios.

The source text updates from simple "English1/English2" to more descriptive "English Key #1/English Key #2" format align well with the enhanced validation logic and provide clearer test scenarios.

Also applies to: 10-10

tests/Build/app/Language/XLIFF-fail/locallang_be.xlf (1)

7-7: LGTM: Consistent test data updates across XLIFF files.

The source text updates maintain consistency with other test files in the suite, using the same "English Key #N" format for improved test clarity.

Also applies to: 10-10

tests/Build/app/Language/XLIFF-fail/de.locallang_be.xlf (1)

8-8: LGTM: Target text updates complete the test data consistency.

The target text updates to "German Key #N" format complement the source text updates in other test files, creating a comprehensive test suite for the enhanced two-way validation functionality.

Also applies to: 12-12

README.md (3)

3-3: LGTM: Documentation terminology appropriately generalized.

The change from "language source and target files" to "language files" correctly reflects the enhanced bidirectional validation scope implemented in the codebase.


7-7: LGTM: Feature description updated consistently.

The terminology change aligns with the broader validation capabilities now supported by the enhanced validation logic.


10-10: LGTM: More specific XLIFF format documentation.

Adding the explicit file extensions (*.xlf, *.xliff) provides clearer guidance for users about supported file types.

tests/Build/app/Language/XLIFF-fail/de.locallang.xlf (2)

8-8: LGTM: Test data updated with more descriptive content.

The target text change to "German Key #1" makes the test data more descriptive and easier to understand.


10-13: LGTM: Test scenario added for bidirectional validation.

The new translation unit with id="key3" and empty source but non-empty target creates an ideal test case for the enhanced bidirectional validation logic that detects keys present in target files but missing from source files.

src/Command/ValidateTranslationCommand.php (5)

136-136: LGTM: Type annotations improve code clarity.

The inline type annotations for $source and $target variables as ParserInterface instances enhance code readability and provide better IDE support.

Also applies to: 149-149


152-152: LGTM: Excellent implementation of bidirectional validation.

This is the core improvement that implements true two-way mismatch detection by combining:

  • Keys missing in target from source: array_diff($sourceKeys, $target->extractKeys())
  • Keys present in target but missing from source: array_diff($target->extractKeys(), $sourceKeys)

This addresses the PR objective of "two-way-mismatch validation" and ensures comprehensive validation coverage.


154-154: LGTM: More idiomatic PHP condition check.

Using !empty($missingKeys) is more idiomatic and slightly more efficient than count($missingKeys) > 0 for checking non-empty arrays.


163-163: LGTM: Enhanced table formatting with proper content retrieval.

Two excellent improvements:

  1. Correct content retrieval: Using $target->getContentByKey($key, 'target') correctly retrieves the target content instead of the source content from target files
  2. Better UX: The yellow dash placeholder <fg=yellow>–</> provides clear visual indication for missing translation values

This aligns perfectly with the enhanced XliffParser::getContentByKey() method that now accepts an optional $attribute parameter.


199-199: LGTM: Success message aligned with broader terminology.

The updated message "No translation mismatches found" is consistent with the generalized terminology used in the README and reflects the comprehensive bidirectional validation scope.

}

public function getContentByKey(string $key): ?string
public function getContentByKey(string $key, string $attribute = 'source'): ?string
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Critical: Interface signature mismatch detected.

The method signature has been changed to include an optional $attribute parameter, but the ParserInterface still declares the method with only one parameter. This violates the interface contract and breaks the Liskov Substitution Principle.

Update the interface to match the new signature:

// In src/Parser/ParserInterface.php
-public function getContentByKey(string $key): ?string;
+public function getContentByKey(string $key, string $attribute = 'source'): ?string;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function getContentByKey(string $key, string $attribute = 'source'): ?string
// File: src/Parser/ParserInterface.php
-public function getContentByKey(string $key): ?string;
+public function getContentByKey(string $key, string $attribute = 'source'): ?string;
🤖 Prompt for AI Agents
In src/Parser/XliffParser.php at line 42, the method getContentByKey now
includes an optional $attribute parameter, but the ParserInterface still
declares this method with only one parameter. To fix this, update the
ParserInterface method signature for getContentByKey to include the optional
$attribute parameter with the same default value, ensuring the interface
contract matches the implementation and adheres to the Liskov Substitution
Principle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant