You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

nuxt-i18n日期时间本地化功能无法正常生效该如何解决?

Nuxt/i18n日期时间本地化失效排查解决方法
  • 修正配置键名不匹配问题(当前配置核心错误)
    你配置的locales中英文语言的标识code为en,但dateTimeFormats下的配置键使用了en-US,二者未对应导致i18n无法读取到日期格式配置,直接修改dateTimeFormats的键名为en即可,修改后对应配置片段如下:
    dateTimeFormats: {
      'en': {
        short: {
          year: 'numeric',
          month: 'long',
          day: 'numeric',
          weekday: 'short',
        },
        long: {
          year: 'numeric',
          month: 'short',
          day: 'numeric',
          weekday: 'short',
          hour: 'numeric',
          minute: 'numeric',
        },
      },
    }
    
  • 检查格式化方法调用是否正确
    你需要在项目中使用正确的方法调用日期格式化:
    组合式API调用示例:
    const { $d } = useNuxtApp()
    // 格式化当前日期为短格式
    const nowShort = $d(new Date(), 'short')
    
    模板内直接调用示例:
    <template>
      <div>
        <!-- 长格式日期 -->
        <span>{{ $d(new Date(), 'long') }}</span>
      </div>
    </template>
    
  • 校验懒加载相关配置
    你开启了lazy: true懒加载语言包,需要确认langDir指定的locales/目录下存在en.js文件,且文件内没有语法错误,也没有覆盖全局的dateTimeFormats配置。
  • 多语言扩展适配
    如果后续新增其他语言,需要保证每个语言的code参数和dateTimeFormats下的配置键一一对应,避免出现匹配失败问题。

内容的提问来源于stack exchange,提问作者morteza mortezaie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 14:15:00