@nuxtjs/i18n Locales在刷新后更改

发布于 2025-02-10 08:24:13 字数 836 浏览 1 评论 0原文

我有一个应用程序,试图将语言管理放置,但我面临困难。 我使用的是@nuxtjs/i18n的最新版本。当我更改语言时,我的URL改变,标签会改变,一切都很好。但是,当我刷新页面时,系统会恢复到旧语言。

这是我的conf:

[
 '@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)
},

本地效果很好。另一方面,在任何其他环境中,刷新都可以返回默认语言

I have an application where I try to put the language management but I face a difficulty.
I'm on the latest version of @nuxtjs/i18n. When I change language, my URl changes, my labels change, everything is fine. When I refresh my page, however, the system reverts to the old language.

Here is my conf:

[
 '@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'

What more do I need to do to make it keep the language before refresh?

I specify that I use this method to change the language:

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

Locally it works fine. On the other hand on any other environment it does not work, the refresh makes return to the default language

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

大海や 2025-02-17 08:24:13

我有同样的问题,它来自SSR ...
在您的商店/索引内部,您应该具有NSI(NUXT服务器init)功能
这样的东西。

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

i have same problem and it's come from ssr...
inside your store/index you should have a nsi (nuxt server init) function
something like this.

async nuxtServerInit(ssrContext: Context) 
{
    if (this.$i18n.locale==='ar') {
      ssrContext.redirect("/ar");
    }
    else if (this.$i18n.locale === 'fa')
    {
      ssrContext.redirect('/')
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文