1

我有以下文件:

// nuxt.config.js
import { locales } from './services/i18n'
...
    i18n: {
        lazy: true,
        langDir: '~/locales/',
        defaultLocale: 'en',
        detectBrowserLanguage: false,
        differentDomains: true,
        locales,
        vueI18n: {
            fallbackLocale: 'en'
        }
    },
    publicRuntimeConfig: {
        ...
        subDomain: process.env.SUB_DOMAIN,
    },
...

// services/i18n/index.js
export const locales = [
    {
        code: 'ar',
        iso: 'ar',
        file: 'ar.json',
        dir: 'rtl',
        domain: `${process.env.SUB_DOMAIN}.example.ae`,
        name: 'العَرَبِيَّة',
        enName: 'Arabic',
        defaultLanguage: true,
        languages: ['ar']
    },
    {
        code: 'bg',
        iso: 'bg',
        file: 'bg.json',
        dir: 'ltr',
        domain: `${process.env.SUB_DOMAIN}.example.bg`,
        name: 'Български',
        enName: 'Bulgarian',
        defaultLanguage: true,
        languages: ['bg']
    },
    ...
]

问题是它process.env.SUB_DOMAIN似乎是未定义的/services/i18n/index.js,尽管它是设置的,因为同一个变量不是未定义的nuxt.config.js。我知道 nuxt 公开了publicRuntimeConfigas的值$config,但是$config/services/i18n/index.js. 如果我移动locales到,它可能会起作用nuxt.config.js,但我不想这样做,因为它会恶化配置文件的可读性。

所以我的问题是最好的方法是让子域里面/services/i18n/index.js

4

1 回答 1

1

编辑:Alexander Lichter 对 Nuxtjs 的讨论给出了很好的回答:https ://github.com/nuxt/nuxt.js/discussions/9289#discussioncomment-729801

// nuxt.config.js
import { locales } from './services/i18n'
...
    i18n: {
        lazy: true,
        langDir: '~/locales/',
        defaultLocale: 'en',
        detectBrowserLanguage: false,
        differentDomains: true,
        locales: locales(process.env.SUB_DOMAIN),
        vueI18n: {
            fallbackLocale: 'en'
        }
    },
    publicRuntimeConfig: {
        ...
        subDomain: process.env.SUB_DOMAIN,
    },
...
// services/i18n/index.js
export const locales = domain => [
    {
        code: 'ar',
        iso: 'ar',
        file: 'ar.json',
        dir: 'rtl',
        domain: `${domain}.example.ae`,
        name: 'العَرَبِيَّة',
        enName: 'Arabic',
        defaultLanguage: true,
        languages: ['ar']
    },
    {
        code: 'bg',
        iso: 'bg',
        file: 'bg.json',
        dir: 'ltr',
        domain: `${domain}.example.bg`,
        name: 'Български',
        enName: 'Bulgarian',
        defaultLanguage: true,
        languages: ['bg']
    },
    ...
]
于 2021-05-12T08:27:23.753 回答