我有以下文件:
// 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 公开了publicRuntimeConfig
as的值$config
,但是$config
在/services/i18n/index.js
. 如果我移动locales
到,它可能会起作用nuxt.config.js
,但我不想这样做,因为它会恶化配置文件的可读性。
所以我的问题是最好的方法是让子域里面/services/i18n/index.js
。