如何在 Nuxt 3 中的 nuxt.config.ts 文件中获得相当于 context.app.i18n.locale 的内容?
我正在使用“@nuxtjs/i18n”:“^8.0.0-rc.5”(@nuxtjs/i18n@next)。
提前谢谢您,
我意识到我做不到,所以我使用了不同的策略。
使用 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);
}