英译汉插件
- i18n.ts
export default {install: (app: any, options: any) => {app.config.globalProperties.$translate = (key: string) => {return key.split(".").reduce((o, i) => {console.log(o, i)if (o) return o[i]}, options)}}
}
- main.ts全局注入插件
app.use(i18n, {hello: {world: "你好世界"}
})
- vue模版中使用
<span>{{ $translate("hello.world") }}</span>
- ts类型声明文件
tsconfig.json/env.d.ts
import { ComponentCustomProperties } from "vue"declare module "@vue/runtime-core" {interface ComponentCustomProperties {$translate: (key: string) => string}
}