Skip to content

Conversation

icai
Copy link

@icai icai commented Jul 13, 2025

🔗 Linked issue

resolves #3716

📚 Description

Allow modules to modify route localization

nuxt.hook('i18n:filterPages', (pages: NuxtPage[]) => {
    for (const page of pages) {
      if (page.path.startsWith('/admin')) {
        page.meta = page.meta || {}
        page.meta.i18n = false
      }
    }
})

Summary by CodeRabbit

  • New Features

    • Added support for disabling internationalization (i18n) on specific pages using a new hook, allowing finer control over which pages are localized.
    • Introduced configuration options to easily exclude pages (e.g., those under '/admin') from i18n processing.
  • Bug Fixes

    • Ensured that pages explicitly marked to disable i18n are no longer processed for localization.
  • Tests

    • Added new test fixtures and example pages to verify the correct handling of i18n-disabled pages.

Copy link
Contributor

coderabbitai bot commented Jul 13, 2025

Walkthrough

This update introduces a new Nuxt module hook, 'i18n:filterPages', which allows mutation of the pages array before localization is applied. The hook is called after the pages are scanned, enabling users to set a meta.i18n = false property on specific pages to disable internationalization for them. The localization logic is updated to skip localization for any page where meta.i18n is set to false. A new fixture demonstrates this feature by disabling i18n for admin pages, and supporting configuration and component files are added for testing this scenario. No changes are made to existing exported function signatures.

✨ 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
Contributor

@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: 0

🧹 Nitpick comments (2)
specs/fixtures/routing/nuxt.config.ts (1)

11-19: Excellent implementation of the i18n:filterPages hook.

The logic correctly identifies admin pages and disables i18n by setting meta.i18n = false. The defensive initialization of page.meta prevents potential errors.

Minor suggestion: Consider using NuxtPage[] instead of any[] for better type safety, though any[] is acceptable for test fixtures.

src/pages.ts (1)

86-89: Well-implemented hook invocation with proper defensive programming.

The hook call allows external modules to mutate page metadata before localization. The await is appropriate since hooks can be asynchronous, and the defensive check ensures compatibility.

Minor suggestion: The if (nuxt.callHook) check might be unnecessary in a proper Nuxt context, but it's good defensive programming.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4af7a56 and 1bd27f0.

📒 Files selected for processing (4)
  • specs/fixtures/routing/nuxt.config.ts (1 hunks)
  • specs/fixtures/routing/pages/admin.vue (1 hunks)
  • src/module.ts (2 hunks)
  • src/pages.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (4)
📓 Common learnings
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In nuxt-modules/i18n's route detection, the `detectLocale` function returns a string representing the locale code, not an object. This string can be passed directly to `ctx.setLocale`.
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In nuxt-modules/i18n's route detection, the `detectLocale` function returns a string representing the locale code, not an object. This string can be passed directly to `ctx.setLocale`.
specs/fixtures/routing/nuxt.config.ts (2)
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In nuxt-modules/i18n's route detection, the `detectLocale` function returns a string representing the locale code, not an object. This string can be passed directly to `ctx.setLocale`.
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In nuxt-modules/i18n's route detection, the `detectLocale` function returns a string representing the locale code, not an object. This string can be passed directly to `ctx.setLocale`.
src/module.ts (3)
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In the nuxt-modules/i18n package, `_nuxtI18nCtx.setLocale` is a synchronous function that accepts a string locale parameter and returns void. It doesn't need to be awaited when called.
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In the nuxt-modules/i18n package, `_nuxtI18nCtx.setLocale` is a synchronous function that accepts a string locale parameter and returns void. It doesn't need to be awaited when called.
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3584
File: src/runtime/server/context.ts:1-4
Timestamp: 2025-05-04T11:52:41.396Z
Learning: In Nuxt applications, `$fetch` is globally available and doesn't need to be explicitly imported.
src/pages.ts (4)
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In nuxt-modules/i18n's route detection, the `detectLocale` function returns a string representing the locale code, not an object. This string can be passed directly to `ctx.setLocale`.
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In nuxt-modules/i18n's route detection, the `detectLocale` function returns a string representing the locale code, not an object. This string can be passed directly to `ctx.setLocale`.
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In the nuxt-modules/i18n package, `_nuxtI18nCtx.setLocale` is a synchronous function that accepts a string locale parameter and returns void. It doesn't need to be awaited when called.
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In the nuxt-modules/i18n package, `_nuxtI18nCtx.setLocale` is a synchronous function that accepts a string locale parameter and returns void. It doesn't need to be awaited when called.
🧬 Code Graph Analysis (2)
src/module.ts (1)
specs/fixtures/routing/nuxt.config.ts (1)
  • pages (11-19)
src/pages.ts (2)
specs/fixtures/routing/nuxt.config.ts (1)
  • pages (11-19)
src/routing.ts (1)
  • localizeRoutes (70-123)
🔇 Additional comments (5)
src/module.ts (2)

7-7: LGTM!

The addition of NuxtPage import is necessary for the new hook signature and correctly placed alongside the existing HookResult import.


129-129: LGTM!

The hook signature is well-defined with proper typing. The void return type correctly indicates that the hook is intended to mutate the pages array in place rather than return a new array.

specs/fixtures/routing/pages/admin.vue (1)

1-3: LGTM!

This simple test fixture appropriately demonstrates the i18n filtering functionality for admin pages. The minimal implementation is suitable for its purpose.

src/pages.ts (2)

91-93: Excellent filtering logic with precise condition checking.

The filtering correctly excludes only pages with meta.i18n explicitly set to false, not just falsy values. This preserves pages where meta.i18n is undefined or truthy, which is the intended behavior.


95-95: Correct usage of filtered pages for localization.

Using i18nPages instead of the full pages array ensures that only pages intended for internationalization are processed by localizeRoutes, while keeping excluded pages available for other functionality.

Copy link
Contributor

@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: 0

🧹 Nitpick comments (1)
specs/fixtures/i18n-disable-pages/nuxt.config.ts (1)

10-10: Improve type safety in the hook parameter.

The fixture correctly demonstrates the feature, but the pages parameter should use proper typing for better type safety.

-    'i18n:filterPages'(pages: any[]) {
+    'i18n:filterPages'(pages: import('#app').NuxtPage[]) {

Alternatively, if the NuxtPage type is not available in this context, you could use a more descriptive interface:

-    'i18n:filterPages'(pages: any[]) {
+    'i18n:filterPages'(pages: Array<{ path: string; meta?: { i18n?: boolean } }>) {
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 1bd27f0 and 3246d52.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (6)
  • specs/fixtures/i18n-disable-pages/nuxt.config.ts (1 hunks)
  • specs/fixtures/i18n-disable-pages/package.json (1 hunks)
  • specs/fixtures/i18n-disable-pages/pages/admin.vue (1 hunks)
  • specs/fixtures/i18n-disable-pages/pages/index.vue (1 hunks)
  • src/kit/gen.ts (1 hunks)
  • src/pages.ts (1 hunks)
✅ Files skipped from review due to trivial changes (3)
  • specs/fixtures/i18n-disable-pages/pages/admin.vue
  • specs/fixtures/i18n-disable-pages/pages/index.vue
  • specs/fixtures/i18n-disable-pages/package.json
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/pages.ts
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In the nuxt-modules/i18n package, `_nuxtI18nCtx.setLocale` is a synchronous function that accepts a string locale parameter and returns void. It doesn't need to be awaited when called.
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In the nuxt-modules/i18n package, `_nuxtI18nCtx.setLocale` is a synchronous function that accepts a string locale parameter and returns void. It doesn't need to be awaited when called.
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In nuxt-modules/i18n's route detection, the `detectLocale` function returns a string representing the locale code, not an object. This string can be passed directly to `ctx.setLocale`.
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In nuxt-modules/i18n's route detection, the `detectLocale` function returns a string representing the locale code, not an object. This string can be passed directly to `ctx.setLocale`.
src/kit/gen.ts (2)
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In nuxt-modules/i18n's route detection, the `detectLocale` function returns a string representing the locale code, not an object. This string can be passed directly to `ctx.setLocale`.
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In nuxt-modules/i18n's route detection, the `detectLocale` function returns a string representing the locale code, not an object. This string can be passed directly to `ctx.setLocale`.
specs/fixtures/i18n-disable-pages/nuxt.config.ts (4)
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In the nuxt-modules/i18n package, `_nuxtI18nCtx.setLocale` is a synchronous function that accepts a string locale parameter and returns void. It doesn't need to be awaited when called.
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In the nuxt-modules/i18n package, `_nuxtI18nCtx.setLocale` is a synchronous function that accepts a string locale parameter and returns void. It doesn't need to be awaited when called.
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In nuxt-modules/i18n's route detection, the `detectLocale` function returns a string representing the locale code, not an object. This string can be passed directly to `ctx.setLocale`.
Learnt from: BobbieGoede
PR: nuxt-modules/i18n#3587
File: src/runtime/plugins/route-locale-detect.ts:24-31
Timestamp: 2025-05-05T20:42:02.900Z
Learning: In nuxt-modules/i18n's route detection, the `detectLocale` function returns a string representing the locale code, not an object. This string can be passed directly to `ctx.setLocale`.
🔇 Additional comments (2)
src/kit/gen.ts (1)

101-104: LGTM! Well-implemented early exit condition.

The implementation correctly:

  • Uses optional chaining for safe property access
  • Explicitly checks for false (not just falsy values)
  • Returns the original route without localization processing
  • Improves performance with early exit before expensive operations
specs/fixtures/i18n-disable-pages/nuxt.config.ts (1)

12-17: Well-structured hook implementation.

The logic correctly:

  • Iterates through all pages
  • Uses appropriate path matching for the demo
  • Safely initializes the meta object before setting properties
  • Demonstrates the feature clearly

@BobbieGoede
Copy link
Member

Thank you for your PR!

After taking a look at the changes in this PR, I'm not sure about adding the i18n:filterPages hook. Replacing the hook usage in the i18n-disable-pages fixture with the pages:extend hook works as well 🤔

Effectively this change https://github.com/nuxt-modules/i18n/pull/3726/files#diff-eb1a20dccd87e92592b66458a2c4d10f5309b3b774fa41bcdc9925f3ad76c1f2R102 would partially enable the customRoutes: 'meta' option, which would be a breaking change and possibly unexpected behavior, so this will need some reconsidering.

@icai
Copy link
Author

icai commented Jul 15, 2025

@BobbieGoede This feature skips microcode compilation, so there is no conflict. Is there a priority issue? This priority must be relatively high, right?

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.

Allow modules to modify route localization

2 participants