Skip to content

fix: locale detection should respect runtime-configured domains (#2931) #3697

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 1 commit into
base: next
Choose a base branch
from

Conversation

cjpearson
Copy link
Contributor

@cjpearson cjpearson commented Jun 15, 2025

🔗 Linked issue

Ports #3693 to the next branch.

📚 Description

This change checks for runtime-configured domain overrides when determining the locale from the host.

Summary by CodeRabbit

  • New Features
    • Added Korean language support to the app, including translations for key interface elements.
  • Tests
    • Introduced a new test to verify locale detection for the Korean domain using runtime configuration.

Copy link
Contributor

coderabbitai bot commented Jun 15, 2025

Walkthrough

A new test was introduced to verify locale detection using a domain defined in runtime configuration for Korean. The i18n configuration was updated to include Korean translations. The locale detection logic was enhanced to utilize runtime-configured domain locales, updating the getHostLocale function and its usage in the detection process.

Changes

File(s) Change Summary
specs/different_domains/different_domains.spec.ts Added a test for detecting the Korean locale using a runtimeConfig domain.
specs/fixtures/different_domains/i18n/i18n.config.ts Added Korean ("kr") locale translations to the i18n messages object.
src/runtime/shared/detection.ts Extended getHostLocale to accept domainLocales and updated detection logic to use runtimeConfig domains.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test Runner
    participant App as Application
    participant Detection as Locale Detection
    participant Config as RuntimeConfig

    Test->>App: Send request with host "kr.staging.nuxt-app.localhost"
    App->>Detection: Call getHostLocale(event, path, domainLocales)
    Detection->>Config: Retrieve domainLocales from runtimeConfig
    Detection->>Detection: Map locales with domain from domainLocales
    Detection->>App: Return detected locale ("kr")
    App->>Test: Respond with Korean welcome text
Loading

Possibly related PRs

Poem

A hop to Korea, a leap through the code,
Now domains can tell which language is owed.
With runtime configs and a test in the mix,
The rabbit ensures locale logic clicks.
환영하다, we say with glee—
Internationalization, as easy as can be! 🐇🌐

✨ 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: 1

🧹 Nitpick comments (4)
src/runtime/shared/detection.ts (2)

8-9: Avoid importing from a sibling shared directory

useRuntimeI18n is already in the same shared folder as this file. Importing via ../shared/utils works, but it weakens the folder-cohesion signal and is a brittle relative path if the file ever moves. Prefer a root-alias import (e.g. #i18n-shared/utils) like the other kit imports to keep paths stable.


54-61: useDetectors now depends on runtime config – cache or inject

useRuntimeI18n() is executed every time useDetectors is called.
Given that useDetectors itself is typically called for each navigation, consider:

  1. Reading runtimeI18n once at module scope, or
  2. Accepting it as an argument that the caller passes only once.

Not critical, but will cut a bit of overhead on the client.

specs/different_domains/different_domains.spec.ts (2)

14-18: Runtime-only domains: assert they override build-time ones

Nice addition! 👍
Consider an extra assertion ensuring kr.nuxt-app.localhost does not match once runtime overrides are in place. This guarantees the override is exclusive and keeps future refactors from silently re-introducing the build-time domain.

await expect(
  undiciRequest('/', { headers: { Host: 'kr.nuxt-app.localhost' } })
).resolves.toHaveProperty('statusCode', 404) // or whatever the expected fallback is

154-162: Consolidate duplicated host-detection tests

The new test is almost identical to the previous host-loop.
Reduce duplication and improve discoverability:

- test('(#2931) detect using runtimeConfig domain', async () => { … })
+ test.each([
+   ['kr.staging.nuxt-app.localhost', '환영하다'],
+ ])('(#2931) detect %s using runtimeConfig domain', async (host, expected) => { … })

This keeps all host-locale assertions in the same table.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between feb4672 and 5d01745.

📒 Files selected for processing (3)
  • specs/different_domains/different_domains.spec.ts (1 hunks)
  • specs/fixtures/different_domains/i18n/i18n.config.ts (1 hunks)
  • src/runtime/shared/detection.ts (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
specs/different_domains/different_domains.spec.ts (2)
specs/utils/server.ts (1)
  • undiciRequest (109-111)
specs/helper.ts (1)
  • getDom (47-49)
src/runtime/shared/detection.ts (4)
src/types.ts (1)
  • I18nPublicRuntimeConfig (356-370)
src/runtime/shared/domain.ts (1)
  • matchDomainLocale (7-23)
src/runtime/kit/routing.ts (1)
  • getLocaleFromRoutePath (32-32)
src/runtime/shared/utils.ts (1)
  • useRuntimeI18n (4-6)
🔇 Additional comments (1)
specs/fixtures/different_domains/i18n/i18n.config.ts (1)

43-56: Translation quality – confirm Korean strings

The Korean copy uses literal dictionary forms (환영하다, 동적).
Native UI wording is usually polite style (환영합니다, 동적 페이지, etc.).
If the strings ship to production, have a native speaker review them to avoid an awkward tone.

Comment on lines +33 to 47
const getHostLocale = (
event: H3Event | undefined,
path: string,
domainLocales: I18nPublicRuntimeConfig['domainLocales']
) => {
const host = import.meta.client
? new URL(window.location.href).host
: getRequestURL(event!, { xForwardedHost: true }).host
return matchDomainLocale(normalizedLocales, host, getLocaleFromRoutePath(path))

const locales = normalizedLocales.map(l => ({
...l,
domain: domainLocales[l.code]?.domain ?? l.domain
}))
return matchDomainLocale(locales, host, getLocaleFromRoutePath(path))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Per-request cloning of normalizedLocales is wasteful

getHostLocale maps over normalizedLocales on every invocation to attach runtime domains.
For heavily trafficked apps this runs once per request, causing unnecessary allocations.

-  const locales = normalizedLocales.map(l => ({
-    ...l,
-    domain: domainLocales[l.code]?.domain ?? l.domain
-  }))
+  // cache by reference – memoised on first call
+  const locales =
+    cachedRuntimeDomains ??
+    (cachedRuntimeDomains = normalizedLocales.map(l => ({
+      ...l,
+      domain: domainLocales[l.code]?.domain ?? l.domain
+    })))

You can stick a simple let cachedRuntimeDomains: LocaleObject[] | undefined outside the function to memoise until domainLocales changes (which is only on hot-reload).

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In src/runtime/shared/detection.ts around lines 33 to 47, the function
getHostLocale creates a new mapped array from normalizedLocales on every call,
causing unnecessary allocations per request. To fix this, declare a variable
cachedRuntimeDomains outside the function to store the mapped locales with
runtime domains, and update this cache only when domainLocales changes (such as
on hot-reload). Inside getHostLocale, return the cached array instead of
remapping normalizedLocales each time.

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