Skip to content

Add localeDetector option to customize locale detection during Vue component rendering #3609

Open

Description

@odanado

Describe the feature

Background

The nuxt-i18n module provides a strategy option with the following values:

  • no_prefix
  • prefix_except_default
  • prefix
  • prefix_and_default

ref: https://i18n.nuxtjs.org/docs/guide

The no_prefix strategy disables the use of locale prefixes (e.g., /en, /ja) in the URL.

Additionally, nuxt-i18n has a feature that automatically detects the locale based on the URL. For example, accessing a URL like /en/* will switch the locale to en.

However, when the no_prefix strategy is used, this automatic locale detection based on the pathname is disabled, as the pathname does not contain locale information.
ref:

i18n/src/runtime/utils.ts

Lines 247 to 249 in 25b900a

} else if (__I18N_STRATEGY__ !== 'no_prefix') {
detected ||= routeLocale
}

This behavior seems logical given the absence of locale information in the URL. However, it causes issues when building multilingual documentation sites using nuxt/content.

Example Use Case:

Suppose we have the following markdown files for multilingual content:

  • docs/en/about.md
  • docs/ja/about.md

In this case, we want to access /en/about and /ja/about to display the corresponding localized content. However, since the locale detection from pathname is disabled with no_prefix, the locale is not correctly detected based on the URL.

As a result, during SSG (Static Site Generation), both /en/about and /ja/about will be generated using the language setting of the build machine, causing incorrect locale assignments.

e.g. https://github.com/odan-sandbox/nuxt-content-and-i18n-no_prefix-example

http://localhost:3000/en/about http://localhost:3000/ja/about
Image Image

Proposed Feature: localeDetector Option

Current Behavior Example

To illustrate the current behavior, consider the following scenario:

  • Accessing /en/about and /ja/about under the no_prefix strategy will not trigger locale detection based on the pathname.
  • As a result, both paths will use the language setting of the build machine during SSG, resulting in both paths potentially being generated in the same language.

Screenshots and repository links demonstrating the current behavior will be provided to further clarify the issue.

To address this issue, I propose adding a new localeDetector option to the module configuration. This option allows specifying the methods for locale detection as follows:

type LocaleDetector = ("pathname" | "domain" | "cookie" | "browser")[];

const localeDetector: LocaleDetector = ["pathname", "domain", "cookie", "browser"];

If pathname is specified, the locale will be automatically detected based on the URL pathname.

The default value of localeDetector will vary depending on the strategy value to maintain backward compatibility. For instance, when no_prefix is selected, the default value would be:

localeDetector: ["domain", "cookie", "browser"]
Example Configuration for My Use Case:
strategy: "no_prefix",
localeDetector: ["pathname", "cookie", "browser"]

This configuration would resolve the issue by enabling locale detection based on pathname while maintaining backward compatibility with existing behavior.

Thank you for considering this feature request. I would be happy to provide further clarifications or contribute to the implementation if needed.

Additional information

  • Would you be willing to help implement this feature?
  • Could this feature be implemented as a module?

Final checks

Activity

  1. odanado commented on May 16, 2025

    @odanado
    Author

    The detection function specified by experimental.localeDetector is only executed for API calls and is not invoked during the rendering of Vue components. Therefore, it does not conflict with the proposed localeDetector option.
    ref: https://i18n.nuxtjs.org/docs/guide/server-side-translations

    Additionally, since experimental.localeDetector is intended for server-side translations, it might be more appropriate to rename it to experimental.serverLocaleDetector if the proposed localeDetector option is adopted.

  2. BobbieGoede commented on May 17, 2025

    @BobbieGoede
    Member

    Thank you for the well thought out feature request 🙏

    I have considered changing locale detection to be modular and configurable before, much like the approach you suggest, but other changes took priority at that time and I wasn't sure if there was any demand for it.

    The current locale detection is a bit complicated:

    • Nuxt SSR/client vs Nitro (server)
    • First visit vs page navigation
    • Side-effects (detection cookie, maybe more)

    I have started work on refactors #3611 and will rework the detection to hopefully work towards a configurable approach.

    One request, could you extract the issue (problem/use case) out of this feature request into a separate issue? 🙏 There may be other (temporary or better suited) solutions that could work for your use case.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions