問題描述
nuxt‑i18n 的延遲加載語言環境 (Lazy load locales for nuxt‑i18n)
在 nuxt‑i18n 中,您必須使用您的應用將支持的語言環境指定 locales 屬性,如下所示:
locales: [
{
code: 'en',
file: 'en‑US.js'
},
{
code: 'es',
file: 'es‑ES.js'
},
{
code: 'fr',
file: 'fr‑FR.js'
}
],
這可能是從 api 端點獲取然後填充數組嗎?
我在這個問題上看到過類似的東西 https:/ /github.com/nuxt‑community/nuxt‑i18n/issues/256#issuecomment‑505344965 但我不知道如何運行該代碼,甚至有可能。
參考解法
方法 1:
The solution was simple. Nuxt allows you to export an async function as options so I can get the data and next assign it to the i18n object.
https://nuxtjs.org/guide/configuration/#asynchronous‑configuration
export default async () => {
const data = await getCountries()
const locales = []
for (const locale of data) {
locales.push({
code: locale,
file: `${locale}.json`
})
}
return {
build: {
...
},
env: {
...
},
modules: [
'nuxt‑i18n',
...
],
i18n: {
locales,
strategy: 'prefix',
detectBrowserLanguage: false,
lazy: true,
defaultLocale: 'es',
langDir: 'translations/'
},
...
}
}
(by legomolina、legomolina)