如何使用 nuxt-i18n 從 Nuxt 2 中的 yaml 檔案加載我的語言環境?
我已經將路徑添加到我的nuxt.config.js:
{
i18n: {
vueI18nLoader: true,
langDir: '~/locales/',
locales: [
{ code: 'en', iso: 'en-GB', file: 'en.yaml', dir: 'ltr' },
{ code: 'de', iso: 'de-DE', file: 'de.yaml', dir: 'ltr' },
],
defaultLocale: 'en',
}
}
我啟用了 vue-i18n-loader,它帶來了自動 yaml 支持和 i18n 塊。
uj5u.com熱心網友回復:
您可以將 Nuxt 配置為對您的語言環境使用yaml-loader:
// nuxt.config.js
{
// ...
build: {
// ...
extend(config) {
config.module.rules.push({
test: /\.ya?ml$/,
type: 'json',
use: 'yaml-loader'
})
}
}
}
在此處了解有關在 Nuxt 中擴展 Webpack 配置的更多資訊:https ://nuxtjs.org/docs/configuration-glossary/configuration-build#extend
這將允許您為nuxt-i18n / vue-i18n加載 yaml 檔案。但是,它也會與Vue 組件中的自定義i18n 塊( <i18n>)發生沖突(假設您使用 SFC 模式)。
為了防止這種情況,您可以將上述規則更改為僅適用于您locales/目錄(或您想要的任何其他目錄)中的yaml 檔案:
// nuxt.config.js
{
// ...
build: {
// ...
extend(config) {
config.module.rules.push({
test: /locales\/.*\.ya?ml$/, // <---
type: 'json',
use: 'yaml-loader'
})
}
}
}
現在,您可以使用 i18n 塊以及locales/目錄中的yaml 檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/400852.html
