如何在 nuxt.config 文件中获取 i18n 语言环境(技术:Nuxt 3、Vue 3、Vite)

问题描述 投票:0回答:2

如何在 Nuxt 3 中的 nuxt.config.ts 文件中获得相当于 context.app.i18n.locale 的内容?

我正在使用“@nuxtjs/i18n”:“^8.0.0-rc.5”(@nuxtjs/i18n@next)。

提前谢谢您,

vuejs3 internationalization locale nuxt3 nuxt.config.js
2个回答
0
投票

我意识到我做不到,所以我使用了不同的策略。


0
投票

使用 Nuxt 3,我在项目中创建了一个 Pinia 商店,并将其命名为 localeStore。这是商店的 locale.ts 文件代码:

import { defineStore } from "pinia";

export const useLocaleStore = defineStore("locale", {
  state: () => ({    
  }),
  getters: {
    currentLocale: () => {
      return useNuxtApp().$i18n.locale.value;
    }
  },
  actions: {
    translate(key: string) {
      return useNuxtApp().$i18n.t(key);
    }
  }
});

然后,在可组合项或实用程序或其他任何内容中的其他 .ts 文件中,您可以导入商店并使用其 currentLocale getter 或翻译函数:

import { useLocaleStore } from "~~/store/locale";

const localeStore = useLocaleStore();

function t(key: string) {
  return localeStore.translate(key);
}
© www.soinside.com 2019 - 2024. All rights reserved.