Skip to content

Fix nested key linked message #2237

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Guillaumedemoff
Copy link

@Guillaumedemoff Guillaumedemoff commented Jul 25, 2025

Problem description

When using a flat JSON for translation messages, linked messages are not resolved.

For example:

messages: {
      en: {
        'nested.key': 'Nested key',
        'message.linkedWithNested': '@:nested.key'
      }
    }

The following does not return anything: t('message.linkedWithNested')

Summary by CodeRabbit

  • New Features

    • Added support for linking translation keys to nested keys outside their namespace, with examples in both English and Japanese locales.
    • Updated examples to display linked nested translations in the user interface.
  • Bug Fixes

    • Improved fallback logic for resolving linked translation keys to ensure accurate value resolution.
  • Tests

    • Added tests to verify correct translation and linking of nested keys.
    • Extended end-to-end tests to check rendering and locale switching for linked nested translations.

Copy link

coderabbitai bot commented Jul 25, 2025

Walkthrough

A new translation scenario involving linked nested keys was introduced across the codebase. Translation files and HTML templates were updated to add and demonstrate the new linked key. The translation core logic was adjusted to refine fallback resolution. Corresponding tests, including end-to-end and unit tests, were added or extended to cover these changes.

Changes

File(s) Change Summary
examples/composition/formatting/linked.html
examples/petite/formatting/linked.html
Added 'nested.key' and linked 'message.linkedWithNested' translation keys to both English and Japanese locales; updated templates to display the new translation.
packages/core-base/src/translate.ts Modified fallback resolution logic in resolveMessage to use the directly resolved format value.
packages/core-base/test/translate.test.ts Added a unit test for resolving a linked nested translation key.
e2e/formatting/linked.spec.ts Extended end-to-end tests to assert correct rendering and locale switching for the new linked nested key.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant App
    participant Translator
    participant Messages

    User->>App: Request translation for 'message.linkedWithNested'
    App->>Translator: translate('message.linkedWithNested')
    Translator->>Messages: Lookup 'message.linkedWithNested'
    Messages-->>Translator: "@:nested.key"
    Translator->>Messages: Lookup 'nested.key'
    Messages-->>Translator: "Nested key"
    Translator-->>App: "Nested key"
    App-->>User: Display "Nested key"
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

In the warren of words, a new tunnel appears,
Linking keys together, across translation frontiers.
Nested and tidy, the messages flow,
From English to Japanese, the meanings now show.
With each little hop, our code grows more neat—
A rabbit’s delight in a multilingual feat! 🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 6045c95 and 3b0f2cc.

📒 Files selected for processing (5)
  • e2e/formatting/linked.spec.ts (2 hunks)
  • examples/composition/formatting/linked.html (4 hunks)
  • examples/petite/formatting/linked.html (4 hunks)
  • packages/core-base/src/translate.ts (1 hunks)
  • packages/core-base/test/translate.test.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
e2e/formatting/linked.spec.ts (1)
e2e/helper.ts (1)
  • getText (10-16)
packages/core-base/test/translate.test.ts (1)
packages/core-base/src/translate.ts (1)
  • translate (614-802)
🔇 Additional comments (10)
packages/core-base/src/translate.ts (1)

1168-1176: LGTM! Core fix for nested key linked message resolution.

The simplification of the fallback logic correctly addresses the issue by directly using the resolved format from resolveMessageFormat. This eliminates the redundant resolution step that was preventing proper resolution of linked nested keys like @:nested.key.

e2e/formatting/linked.spec.ts (2)

18-18: LGTM! Proper e2e test coverage for initial rendering.

The test correctly verifies that the new linked nested key functionality renders "Nested key" in the initial English locale.


32-34: LGTM! Comprehensive locale switching test coverage.

The test properly verifies that the linked nested key functionality works correctly when switching to the Japanese locale, displaying "ネストされたキー".

packages/core-base/test/translate.test.ts (1)

1057-1068: LGTM! Essential unit test for the core functionality.

This test directly addresses the PR objective by verifying that linked messages referencing nested keys in a flat JSON structure are correctly resolved. The test setup perfectly matches the problem scenario and confirms the fix works as expected.

examples/petite/formatting/linked.html (3)

24-24: LGTM! Proper HTML template addition for demonstrating the feature.

The new paragraph element correctly displays the linked nested message functionality.


34-34: LGTM! Correct English locale setup for nested key linking.

The message structure properly demonstrates the issue and fix:

  • 'nested.key' provides the target message outside the message namespace
  • 'message.linkedWithNested' correctly links to it using @:nested.key syntax

Also applies to: 45-45


48-48: LGTM! Proper Japanese locale implementation.

The Japanese translations correctly mirror the English structure and provide appropriate localized content.

Also applies to: 60-60

examples/composition/formatting/linked.html (3)

24-24: LGTM! Consistent HTML template addition.

The paragraph element correctly demonstrates the linked nested key functionality in the composition API example.


34-34: LGTM! Proper English locale configuration.

The nested key structure correctly demonstrates cross-namespace linking:

  • 'nested.key' at root level provides the target message
  • linkedWithNested within the message object links to it using @:nested.key

Also applies to: 45-45


49-49: LGTM! Consistent Japanese locale implementation.

The Japanese translations properly mirror the English structure with appropriate localized content.

Also applies to: 62-62

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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 generate unit tests to generate unit tests for 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.

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