• i18n的使用(1)


    既然有用到 i18n 那么,肯定需要先安装啦!

    1:安装方式

    npm install vue-i18n
    

      

    2:使用方式

    1:在main.js引入

    import VueI18n from 'vue-i18n'
    Vue.use(VueI18n)
     
    2:准备本地的翻译信息
    const messages = {
      zh: {
        message: {
          hello: '好好学习,天天向上!'
        }
      },
      en: {
        message: {
          hello: 'good good study, day day up!'
        }
      }
    }
    3:创建带有选项的 i18n实例
    const i18n = new VueI18n({
      locale: 'zh', // 语言标识
      messages
    })
    4:将i18n挂载在vue上
    new Vue({
      el: '#app',
      router,
      i18n,
      components: { App },
      template: '<App/>'
    })

    5:使用方式

        <div id="app">
          <h1 style="font-size: 16px; text-align: center;">{{ $t("message.hello") }}</h1>
        </div>

    效果

     将第三步(创建带有选项的 i18n实例)中的  ‘ locale ’ ,换成 ” en “ ,

    const i18n = new VueI18n({
      locale: 'en', // 语言标识
      messages
    })

    后的效果图

    全部代码

    //main.js
    
    import Vue from 'vue'
    import App from './App'
    import router from './router'
    import 'iview/dist/styles/iview.css' // 引入iview css样式
    import VueI18n from 'vue-i18n'
    
    Vue.config.productionTip = false
    Vue.use(iView) //使用iview组件
    Vue.use(VueI18n)
    
    const messages = {
      zh: {
        message: {
          hello: '共产主义接班人'
        }
      },
      en: {
        message: {
          hello: 'successors to the communist cause!'
        }
      }
    }
    
    const i18n = new VueI18n({
      locale: 'en', // 语言标识
      messages
    })
    
    /* eslint-disable no-new */
    new Vue({
      el: '#app',
      router,
      i18n,
      components: { App },
      template: '<App/>'
    })


    //vue文件
    
    <template>
      <div>
        <div id="app">
          <h1 style="font-size: 16px; text-align: center;">{{ $t("message.hello") }}</h1>
        </div>
      </div>
    </template>
    <style lang="">
      h1{
        color: skyblue;
      }
    </style>
    <script>
    export default {
      data() {
        return {
        };
      }
    };
    </script>
  • 相关阅读:
    Gremlin基本使用
    SpringData JdbcTemplate Jdbc使用简介
    DOS命令行使用pscp实现远程文件和文件夹传输(转)
    vscode:让文件支持右键vscode打开
    vue-webpack项目本地开发环境设置代理解决跨域问题
    VueJS中学习使用Vuex详解
    Object.create()和new 创建对象的区别
    vue组件和插件的区别
    创建vue组件与自定义一个vue组件时的区别
    [Vue] : 自定义指令
  • 原文地址:https://www.cnblogs.com/yixiongqiang/p/12217018.html
Copyright © 2020-2023  润新知