国际化 LocaleProvider

默认语言包

  1. <za-locale-provider lang="zh_CN">
  2. <za-search-bar shape="round" :showCancel="true" ref="searchRef"></za-search-bar>
  3. </za-locale-provider>

自定义语言包

  1. <za-cell title="切换语言包">
  2. <za-select v-model="v1" :data-source="langData" @ok="handleOk">
  3. </za-select></za-cell>
  4. <za-locale-provider :lang="lang" :locale="locale">
  5. <za-search-bar shape="round" :showCancel="true" ref="searchRef"></za-search-bar>
  6. <za-cell>
  7. <za-button slot="description" size="xs" @click="visible6 = true" theme="warning">开启</za-button>
  8. 警告框 Alert
  9. </za-cell>
  10. <za-cell>
  11. <za-button slot="description" size="xs" @click="visible7 = true" theme="warning">开启</za-button>
  12. 确认框 Confirm
  13. </za-cell>
  14. <za-alert :visible.sync="visible6" radius title="警告" message="这里是警告信息"></za-alert>
  15. <za-confirm :visible.sync="visible7" title="确认信息" message="你确定要这样做吗?" :ok="handleOk2" :cancel="handleCancel"></za-confirm>
  16. </za-locale-provider>

Vue Script

<script name="vue">
const Locale = {
  'en_US': {
    SearchBar: {
      placeholder: 'Search',
      cancelText: 'Cancel',
    },
    Alert: {
      cancelText: 'Cancel',
    },
    Confirm: {
      okText: 'Ok',
      cancelText: 'Cancel',
    }
  },
  'zh_CN': {
    SearchBar: {
      placeholder: '请输入搜索词...',
      cancelText: '关闭',
    },
    Alert: {
      cancelText: '知道了',
    },
    Confirm: {
      okText: '确定',
      cancelText: '关闭',
    }
  }
}

export default {
  data() {
    return {
      visible6: false,
      visible7: false,
      locale: Locale,
      lang: 'zh_CN',
      v1: 'zh_CN',
      langData:[
        { value: 'zh_CN', label: '中文' },
        { value: 'en_US', label: '英文' },
      ]
    }
  },
  watch: {
    lang() {
      this.$forceUpdate();
    },
  },
  methods: {
    handleOk(val){
      console.log(val);
      this.lang = val.value;
    },
    handleOk2(){
      this.visible7 = false
    },
    handleCancel(){
      this.visible7 = false
    }
  },
};
</script>

API

属性类型默认值说明
localeObject-语言包配置
langString-设置语言包类型,对应 locale 的 key 值

LocaleProvider 国际化 - 图1