给Vue项目添加i18n支持(一)
安装
| 1 | $ vue add i18n | 
安装完成后,生成对应的插件文件i18n.js, 以及在main.js中注册了i18n插件。
plugins/i18n.js
| 1 | import Vue from "vue"; | 
main.js
| 1 | import i18n from "./plugins/i18n"; | 
使用
翻译
locales/en.json
| 1 | { | 
locales/zhcn.json
| 1 | { | 
模板中使用
| 1 | <v-text-field :label="$t('auth.password')"></v-text-field> | 
组件中使用
| 1 | this.$t("common.help") | 
改变locale
| 1 | this.$i18n.locale = "zhcn" | 
集成Vuetify的翻译
locales/en.json
| 1 | "$vuetify": { | 
Vuetify中的全部翻译项在node_modules/vuetify/es5/locale下可以找到。
然后再按如下修改vuetify插件。
plugins/vuetify.js
| 1 | import Vue from "vue"; | 
常见问题
- data方法中的数组在改变locale后不会更新 - 例如: - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35- <template> 
 <v-menu left offset-y transition="slide-x-transition">
 <v-list dense min-width="48" nav width="140">
 <template v-for="(item, index) in actions">
 <v-list-item :key="item.name" link>
 <v-list-item-content>
 <v-list-item-title>{{ item.title }}</v-list-item-title>
 </v-list-item-content>
 </v-list-item>
 </template>
 </v-list>
 </v-menu>
 </template>
 <script>
 export default {
 data() {
 return {
 actions: [
 { icon: "mdi-help", name: "help", title: this.$t("common.help") },
 {
 icon: "mdi-account-box-outline",
 name: "settings",
 title: this.$t("common.settings")
 },
 {
 divider: true,
 icon: "mdi-logout",
 name: "sign_out",
 title: this.$t("auth.logout")
 }
 ]
 };
 }
 };
 </script>
解决方法:
- 添加到计算属性中 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18- computed: { 
 actions: function() {
 return [
 { icon: "mdi-help", name: "help", title: this.$t("common.help") },
 {
 icon: "mdi-account-box-outline",
 name: "settings",
 title: this.$t("common.settings")
 },
 {
 divider: true,
 icon: "mdi-logout",
 name: "sign_out",
 title: this.$t("auth.logout")
 }
 ];
 }
 }
- 或者在模板中使用$t - 1 
 2
 3- <v-list-item-content> 
 <v-list-item-title>{{ $t(item.title) }}</v-list-item-title>
 </v-list-item-content>