使用time.Format时的本地化

ANi*_*sus 6 time localization go

time包中,格式化time.Time变量时,输出将使用未导出的[]字符串切片中定义的周和月的英文名称.

如何使用不同的语言本地化字符串(希望仍然使用Format())?

例:

fmt.Println(time.Now().Format("Mon 2 January 2006"))
Run Code Online (Sandbox Code Playgroud)

输出:

2014年1月28日星期二

期望的输出:

2014年1月28日,Januari

操场

Alv*_*ivi 5

正如您在时间包源代码中看到的那样,值是在源代码中硬编码的。因此,基本上,Go目前不支持i18n。i18n在Go路线图中,甚至在常见问题中也提到过,但最近没有关于该主题的评论。

同时,您可以尝试使用星期一套餐:

  // Change LocaleEnUS to the locale you want to use for translation
  monday.Format(time.Now(), "Mon 2 January 2006", monday.LocaleEnUS) 
Run Code Online (Sandbox Code Playgroud)

  • 2022年还是这样吗?星期一仍然是推荐的方式吗?或者这已经进入标准库了吗? (2认同)