Skip to content
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

Multi domain locales, one domain per language not working? #3299

Open
Zyles opened this issue Jan 10, 2025 · 1 comment
Open

Multi domain locales, one domain per language not working? #3299

Zyles opened this issue Jan 10, 2025 · 1 comment

Comments

@Zyles
Copy link

Zyles commented Jan 10, 2025

Environment

Nuxt project info:

------------------------------
- Operating System: Windows_NT
- Node Version:     v22.11.0
- Nuxt Version:     3.15.1
- CLI Version:      3.19.1
- Nitro Version:    2.10.4
- Package Manager:  [email protected]
- Builder:          -
- User Config:      srcDir, compatibilityDate, devtools, modules, typescript, css, app, i18n, routeRules, site
- Runtime Modules:  @nuxtjs/[email protected], @nuxtjs/[email protected], @nuxtjs/[email protected]
- Build Modules:    -
------------------------------

Reproduction

Following guide: https://i18n.nuxtjs.org/docs/guide/multi-domain-locales

import { localeDomains } from "./locale-domains.config";

const i18nDomains = [
  "mydomain.com",
  "mydomain.se",
  localeDomains.en,
  localeDomains.se,
] as any;

export default defineNuxtConfig({
  modules: ["@nuxtjs/tailwindcss", "@nuxtjs/sitemap", "@nuxtjs/i18n"],
  typescript: {
    strict: true,
    typeCheck: true,
  },
  i18n: {
    vueI18n: "./i18n.config.ts",
    multiDomainLocales: true,
    strategy: "no_prefix", // No prefix like "en" or "se" in routes
    detectBrowserLanguage: false,
    defaultLocale: "en",
    langDir: "locales",
    locales: [
      {
        code: "en",
        iso: "en-US",
        domains: i18nDomains,
        name: "English",
        defaultForDomains: ["mydomain.com"],
        file: "en.yaml",
      },
      {
        code: "se",
        iso: "se-SE",
        domains: i18nDomains,
        name: "Svenska",
        defaultForDomains: ["mydomain.se"],
        file: "se.yaml",
      },
    ],
  },
  routeRules: {
    "/*": { ssr: true },
  },
});
export const localeDomains = {
  en: process.env.DOMAIN_EN,
  se: process.env.DOMAIN_SE,
};

Will error in IDE for the "domains" key:

Type '(string | undefined)[]' is not assignable to type 'string[]'.
  Type 'string | undefined' is not assignable to type 'string'.
    Type 'undefined' is not assignable to type 'string'.ts(2322)
module.d.ts(283, 5): The expected type comes from property 'domains' which is declared here on type 'LocaleObject<string>'

If we try set it to "any":

const i18nDomains = [
  "mydomain.com",
  "mydomain.se",
  localeDomains.en,
  localeDomains.se,
] as any; // or as string[]

Let's try with:

defaultForDomains: ["mydomain.com", localeDomains.en],

// Error: 
Type 'string | undefined' is not assignable to type 'string'.
  Type 'undefined' is not assignable to type 'string'.ts(2322)
(alias) const localeDomains: {
    en: string | undefined;
    se: string;
}
import localeDomains

Need to use:

en: process.env.DOMAIN_EN as string,

But this wont make a difference it seems.

Then try to run it:

$env:DOMAIN_SE="mydomain.se"; npm run dev

// This isn't correct either
$env:DOMAIN_SE="se"; npm run dev

// Nor is this
$env:DOMAIN_SE; npm run dev
mydomain.com should load locale EN
mydomain.se should load locale SE
+
work in dev with ENV variables and support "npm run generate" with correct locale.

No translation is happening.

What is the correct way to define the ENV variable?

Describe the bug

Type errors trying to assign an array to domains key.

And no translation is happening trying to use ENV variables.

Additional context

No response

Logs

@vanling
Copy link

vanling commented Jan 15, 2025

Im trying the same here.. Im looking into https://i18n.nuxtjs.org/docs/api/runtime-config#locales which seems to be the way to go, but cant get it to work..

Nuxt itself accepts (via defu) the weird .env notation (nuxt/nuxt#24470 (comment))

NUXT_PUBLIC_TEST=["test","test2","test3"]

That works...

Trying the same here

NUXT_PUBLIC_I18N_DOMAIN_LOCALES_NL_DEFAULT_FOR_DOMAINS=["nl.domain.ext"]
NUXT_PUBLIC_I18N_DOMAIN_LOCALES_NL_DOMAINS=["nl.domain.ext","en.domain.ext"]

But whatever I try, this is not working.

My guess is, this whole key NUXT_PUBLIC_I18N_DOMAIN_LOCALES_NL_DOMAINS is not working or it uses a custom code without defu as used in nuxt.

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

No branches or pull requests

2 participants