js 文件使用 i18n
2025-04-30 14:45 法子 阅读(335) 评论(0) 收藏 举报1. 创建并导出i18n实例
import messages from '@/locale/index' // 自己写的语言包路径 let i18nConfig = { locale: uni.getLocale(),// 获取已设置的语言 messages } // #ifndef VUE3 import Vue from 'vue' import VueI18n from 'vue-i18n' Vue.use(VueI18n) const i18n = new VueI18n(i18nConfig) // #endif // #ifdef VUE3 const i18n = createI18n(i18nConfig) // #endif export default i18n;
2. 在普通JS文件中使用i18n
import i18n from '@/utils/i18n.js'; // 根据项目路径调整 console.log(i18n.t('app.name'));
另外在Vue项目中这样注入i18n
// main.js import App from './App' import i18n from '@/utils/i18n.js'; // #ifndef VUE3 const app = new Vue({ i18n, ...App }) app.$mount() // #endif // #ifdef VUE3 import { createSSRApp } from 'vue' export function createApp() { const app = createSSRApp(App) app.use(i18n) return { app } } // #endif
浙公网安备 33010602011771号