0


vue项目实现中英文切换

需求:vue项目实现中英文切换

效果图如下:

步骤一:安装vue-i18n

注意:如果使用vue3,则不用指定版本安装,如果是vue2,就要指定版本如下:

  1. npm i vue-i18n@8.23.0

步骤二:创建基本目录(language文件夹是翻译的数据文件,views文件夹下的是页面,part1和part2代表项目的不同模块,每个模块下都有对应的页面)

步骤三:简单编写下基本界面

App.vue

  1. <template>
  2. <div id="app">
  3. <!-- 111 -->
  4. <!-- 中英文切换 -->
  5. 按我切换中英文
  6. <button @click="chagelanguage">{{ language }}</button>
  7. <router-view />
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. language: "en",//中英文切换
  15. }
  16. },
  17. methods: {
  18. // 中英文切换
  19. chagelanguage() {
  20. this.$i18n.locale == 'zh' ? this.$i18n.locale = 'en' : this.$i18n.locale = 'zh' //设置中英文模式
  21. if (this.$i18n.locale === 'zh') {
  22. this.language = "en"
  23. } else {
  24. this.language = "zh"
  25. }
  26. },
  27. }
  28. }
  29. </script>
  30. <style lang="less" scoped></style>

views/part1/page1.vue

  1. <template>
  2. <div>
  3. <h1 style="color:black">part1 page1</h1>
  4. <p><span style="color:blue;font-weight:bold;font-size: 20px;">测试文本:</span>{{ $t("part1.page1.content") }}</p>
  5. <br>
  6. <br>
  7. <a style="color: red;" @click="gopart1page2">点我跳转part1 page2</a>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. methods: {
  13. gopart1page2() {
  14. // 跳转part1 page2(同一模块下的不同页面)
  15. this.$router.push('/part1page2Router')
  16. }
  17. }
  18. }
  19. </script>
  20. <style>
  21. </style>

views/part1/page2.vue

  1. <template>
  2. <div>
  3. <h1>part1 page2</h1>
  4. <p><span style="color:blue;font-weight:bold;font-size: 20px;">测试文本:</span>{{ $t("part1.page2.content") }}</p>
  5. <br>
  6. <br>
  7. <a style="color: red;" @click="gopart2page1">点我跳转part2 page1</a>
  8. <br>
  9. <a style="color: red;" @click="goback">返回</a>
  10. </div>
  11. </template>
  12. <script>
  13. export default {
  14. methods: {
  15. gopart2page1() {
  16. // 跳转part1 page2(不同模块下的不同页面)
  17. this.$router.push('/part2page1Router')
  18. },
  19. goback() {
  20. // 返回上一页面
  21. this.$router.go(-1)
  22. }
  23. }
  24. }
  25. </script>
  26. <style></style>

views/part2/page1.vue

  1. <template>
  2. <div>
  3. <h1>part2 page1</h1>
  4. <p><span style="color:blue;font-weight:bold;font-size: 20px;">测试文本:</span>{{ $t("part2.page1.content") }}</p>
  5. <br>
  6. <br>
  7. <a style="color: red;" @click="goback">返回</a>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. methods: {
  13. goback() {
  14. // 返回上一页面
  15. this.$router.go(-1)
  16. }
  17. }
  18. }
  19. </script>
  20. <style></style>

router/index.js

  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. import part1page1 from '@/views/part1/page1'
  4. import part1page2 from '@/views/part1/page2'
  5. import part2page1 from '@/views/part2/page1'
  6. Vue.use(VueRouter)
  7. const routes = [
  8. {
  9. path: '/',
  10. component: part1page1
  11. }, {
  12. path: '/part1page2Router',
  13. component: part1page2
  14. }, {
  15. path: '/part2page1Router',
  16. component: part2page1
  17. }
  18. ]
  19. const router = new VueRouter({
  20. routes
  21. })
  22. export default router

步骤四:编写翻译文件

language/data/part1/page1.js

  1. const data = {
  2. ZH: {
  3. // part1 page1
  4. content: "我是part1page1,喜欢吃苹果!"
  5. },
  6. EN: {
  7. content: "I am Part1Page1 and enjoy eating apples!"
  8. }
  9. }
  10. export default data

language/data/part1/page2.js

  1. const data = {
  2. ZH: {
  3. // part1 page1
  4. content: "我是part1page2,喜欢吃草莓!"
  5. },
  6. EN: {
  7. content: "I am Part1Page1 and enjoy eating strawberries!"
  8. }
  9. }
  10. export default data

language/data/part1/index.js

  1. import page1 from "./page1"
  2. import page2 from "./page2"
  3. export default {
  4. page1,
  5. page2
  6. }

language/data/part2/page1.js

  1. const data = {
  2. ZH: {
  3. // part1 page1
  4. content: "我是part2page1,喜欢吃芒果!"
  5. },
  6. EN: {
  7. content: "I am Part1Page1 and enjoy eating mangoes!"
  8. }
  9. }
  10. export default data

language/data/part2/index.js

  1. import page1 from "./page1"
  2. export default{
  3. page1
  4. }

language/en.js

  1. import part1 from './data/part1/index'
  2. import part2 from './data/part2/index'
  3. const en = {
  4. language: {
  5. name: "english"
  6. },
  7. // part1
  8. part1: {
  9. page1: {
  10. content: part1.page1.EN.content
  11. },
  12. page2: {
  13. content: part1.page2.EN.content
  14. }
  15. },
  16. // part2
  17. part2: {
  18. page1: {
  19. content: part2.page1.EN.content
  20. }
  21. },
  22. }
  23. export default en

language/zh.js

  1. import part1 from './data/part1/index'
  2. import part2 from './data/part2/index'
  3. const zh = {
  4. language: {
  5. name: "中文"
  6. },
  7. // part1
  8. part1: {
  9. page1: {
  10. content: part1.page1.ZH.content
  11. },
  12. page2: {
  13. content: part1.page2.ZH.content
  14. }
  15. },
  16. // part2
  17. part2: {
  18. page1: {
  19. content: part2.page1.ZH.content
  20. }
  21. },
  22. }
  23. export default zh

步骤五:全局配置

main.js

  1. import Vue from 'vue'
  2. import App from './App.vue'
  3. import router from './router'
  4. import store from './store'
  5. //中英文切换
  6. import ZH from '@/language/zh.js' //中文最终汇总暴露的信息
  7. import EN from '@/language/en.js' //英文
  8. import VueI18n from 'vue-i18n'
  9. Vue.use(VueI18n)
  10. const i18n = new VueI18n({
  11. // localStorage.getItem('languageSet') || 'zh'
  12. locale: 'zh', //从localStorage里获取用户中英文选择,没有则默认中文
  13. messages: {
  14. 'zh': ZH,
  15. 'en': EN
  16. }
  17. })
  18. new Vue({
  19. i18n,
  20. router,
  21. store,
  22. render: h => h(App)
  23. }).$mount('#app')

另外:

如果要在js中引用如下

  1. this.$t("part1.page1.content")

在属性中引用如下

  1. <el-tooltip :content="$t('part1.page1.content')" placement="top"></el-tooltip>

本文转载自: https://blog.csdn.net/weixin_45711931/article/details/130482693
版权归原作者 洛千瑶吖 所有, 如有侵权,请联系我们删除。

“vue项目实现中英文切换”的评论:

还没有评论