【问题标题】:@nuxtjs/i18n locales changing after refresh@nuxtjs/i18n 语言环境在刷新后发生变化
【发布时间】:2023-01-04 13:57:05
【问题描述】:

我有一个应用程序,我试图在其中进行语言管理,但我遇到了困难。 我正在使用最新版本的@nuxtjs/i18n。当我改变语言时,我的 URl 改变了,我的标签改变了,一切都很好。但是,当我刷新页面时,系统会恢复为旧语言。

这是我的会议:

[
 '@nuxtjs/i18n',
       {
           locales,
           defaultLocale,
           lazy: true,
           langDir: 'locales/',
           vueI18n: {
               fallbackLocale: defaultLocale,
           },
       },
   ],
   ['~/.build/merge-and-compare-locales.js', { defaultLocale }],
export const locales = [
    {
        code: 'en',
        file: 'en.json',
    },
    {
        code: 'fr',
        file: 'fr.json',
    },
]

export const defaultLocale = 'fr'

我还需要做什么才能让它在刷新前保持语言?

我指定我使用此方法更改语言:

changeLocale(code: string) {
    this.$i18n.setLocale(code)
},

在本地它工作正常。另一方面,在任何其他环境中它不起作用,刷新使返回到默认语言

【问题讨论】:

    标签: javascript vue.js nuxt.js vue-i18n


    【解决方案1】:

    我有同样的问题,它来自 ssr ... 在你的商店/索引中你应该有一个 nsi (nuxt server init) 函数 像这样的东西。

      /** initial the store when user fetch the web app */
    

    异步 nuxtServerInit(ssrContext:上下文){

    if (this.$i18n.locale==='ar') {
      ssrContext.redirect("/ar");
    }
    else if (this.$i18n.locale === 'fa')
    {
      ssrContext.redirect('/')
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多