中英文切换

https://kazupon.github.io/vue-i18n/zh/introduction.html
1 先安装 npm i vue-i18n -S
2 在src目录下面创建i18n文件夹
在这里插入图片描述
en.js文件放置英文标题

module.exports = {
    message: {
      title: 'english'
    }
  }

zh.js文件放置中文标题

export default {
    message: {
      title: '中文'
    }
  }

在i18n.js文件放置

import Vue from 'vue'
import VueI18n from 'vue-i18n'
import zh from './langs/zh'
import en from './langs/en'

Vue.use(VueI18n)

const messages = {
  zh,
  en
}

const i18n = new VueI18n({
  locale: localStorage.getItem('locale') || 'zh', // 语言标识
  messages
})

export default i18n

在main.js文件中导入i18n

import i18n from './i18n/i18n'
new Vue({
  render: h => h(App),
  i18n
}).$mount('#app')

主页面

<template>
  <div>
    <h1>{{ $t("message.title") }}</h1>
    <el-button type="primary" @click="handleClick">点击切换语言</el-button>
  </div>
</template>

<script>
export default {
  data () {
    return {}
  },
  methods: {
    handleClick () {
      this.$i18n.locale = (this.$i18n.locale === 'zh' ? 'en' : 'zh')
      localStorage.setItem('locale', this.$i18n.locale)
    }
  }
}
</script>
  • 在各个页面使用插值语法{{ $t标题 }} 就可以直接显示对应的中英文

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值