0

我正在 nuxt 开发一个国际博客。我似乎无法访问导航组件和某些页面中的 i18n 对象。您能否说明如何访问它以及为什么我不能访问它?我假设它与生命周期相关但不确定......

在 nuxt-config.js 中:

...
  modules: [
    'nuxt-i18n',
    '@nuxt/content'
  ],
  i18n:{
    strategy: 'prefix_and_default',
    defaultLocale: 'en',
    langDir: 'lang/',
    lazy: true,
    locales: [
      {
        name: '日本語',
        code: 'ja',
        iso: 'ja-JP',
        file: 'ja-JP.js'
      },
      {
        name: 'English',
        code: 'en',
        iso: 'en-US',
        file: 'en-US.js'
      },
    ],
    detectBrowserLanguage: {
      // If enabled, a cookie is set once a user has been redirected to his
      // preferred language to prevent subsequent redirections
      // Set to false to redirect every time
      useCookie: true,
      // Set to override the default domain of the cookie. Defaults to host of the site.
      cookieDomain: null,
      // Cookie name
      cookieKey: 'i18n_redirected',
      // Set to always redirect to value stored in the cookie, not just once
      alwaysRedirect: false,
      // If no locale for the browsers locale is a match, use this one as a fallback
    }
  },
...

我试图根据语言引入不同的降价文件,所以我打电话:

export default {
    async asyncData({ $content, params}) {
      const data = await $content(`${i18n.locale}/data`, params.slug).fetch()
      return { data }
    },

在我拥有的内容中en/data/...ja/data/...如果我将其硬编码为en/data/或者ja/data但我得到 i18n is undefined 错误,它可以工作,我尝试了几种模式但没有成功。

4

1 回答 1

0

我认为在你的代码中使用这个结构更好

https://nuxtjs.org/examples/i18n/

于 2020-08-29T08:17:20.297 回答