element-plus 日历周国际化不生效问题解决


elementJs:

import { createApp, ref } from 'vue'
import ElementPlus from 'element-plus';
import zhLocale from 'element-plus/lib/locale/lang/zh-cn';
import 'dayjs/locale/zh-cn';

ElementPlus.useLang = (app, ref, locale) => {
  const template = (str, option) => {
    if (!str || !option) return str
    return str.replace(/\{(\w+)\}/g, (_, key) => {
      return option[key]
    })
  }

  // 注入全局属性,子组件都能通过inject获取
  app.provide('ElLocaleInjection', {
    lang: ref(locale.name),
    locale: ref(locale),
    t: (...args) => {
      const [path, option] = args
      let value
      const array = path.split('.')
      let current = locale
      for (let i = 0, j = array.length; i < j; i++) {
        const property = array[i]
        value = current[property]
        if (i === j - 1) return template(value, option)
        if (!value) return ''
        current = value
      }
    },
  })
}


const app = createApp(App);
ElementPlus.useLang(app, ref, zhLocale)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM