使用time.Format时的本地化 [英] Localization when using time.Format
问题描述
在time
包中,格式化time.Time
变量时,输出将使用未导出的[]字符串片段中定义的几周和几个月的英文名称.
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.
如何使用另一种语言(希望仍使用Format()
)对字符串进行本地化?
How to localize the string, using a different language (hopefully still using Format()
)?
示例:
fmt.Println(time.Now().Format("Mon 2 January 2006"))
输出:
2014年1月28日星期二
Tue 28 January 2014
所需的输出:
2014年1月28日Tis 28
Tis 28 Januari 2014
推荐答案
您可以在时间包中看到源代码,该值在源中进行了硬编码.因此,基本上,Go目前不支持i18n. i18n处于Go路线图上,甚至在常见问题解答中提到过,但对该主题没有任何评论最近.
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)
这篇关于使用time.Format时的本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!