【问题标题】:Change dayjs locale in Nuxt3在 Nuxt3 中更改 dayjs 语言环境
【发布时间】:2023-01-30 16:27:01
【问题描述】:

更改语言时无法更改 dayjs 语言显示。

在插件/dayjs.js

import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime.js'
import utc from 'dayjs/plugin/utc.js'
import timezone from 'dayjs/plugin/timezone.js'
import updateLocale from 'dayjs/plugin/updateLocale.js'
import 'dayjs/locale/th'
import 'dayjs/locale/en'

export default defineNuxtPlugin((nuxtApp) => {
  dayjs.extend(relativeTime)
  dayjs.extend(utc)
  dayjs.extend(timezone)
  dayjs.extend(updateLocale)

  dayjs.locale('th')
  nuxtApp.provide('dayjs', dayjs)
}

在可组合的

import * as dayjs from 'dayjs'
...
const setLocale = (l) => {
    dayjs.locale(l)
    //dayjs.updateLocale(l)  <- Try this already
   ...
}
...

在页面上,我检查了当前的语言环境,但它可以在“th”和“en”之间切换,但 dayjs 显示英语。

$dayjs().locale()

附言我尝试使用 @nuxt/dayjs@1.4.1 但它不起作用,所以我改用 dayjs。

P.S.2 relativeTime 插件已更改,但显示时间格式未更改。

参考。 https://github.com/nuxt-community/dayjs-module/issues/376

【问题讨论】:

    标签: nuxt.js nuxtjs3 dayjs


    【解决方案1】:

    我设法让它在 app.vue 中使用它

    import 'dayjs/locale/nl'
    
    dayjs.locale('nl')
    

    【讨论】:

      猜你喜欢
      • 2022-07-14
      • 2022-08-18
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多