Localization when using time.Format

≯℡__Kan透↙ 提交于 2020-02-13 09:54:42

问题


In the time package, when formatting a time.Time variable, the output will use the English names for weeks and months as defined in unexported []string slices.

How to localize the string, using a different language (hopefully still using Format())?

Example:

fmt.Println(time.Now().Format("Mon 2 January 2006"))

Output:

Tue 28 January 2014

Desired output:

Tis 28 Januari 2014

Playground


回答1:


As you can see in time package sourcecode that values are hardcoded in source. So, basically, Go doesn't support i18n right now. i18n is on Go roadmap, its even mentioned in the faq, but there were no comments on that topic recently.

Meanwhile, you could try to use Monday package:

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


来源:https://stackoverflow.com/questions/21400121/localization-when-using-time-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!