英文:
Localization when using time.Format
问题
在time
包中,当格式化一个time.Time
变量时,输出将使用未导出的[]string
切片中定义的英文星期和月份名称。
如何本地化字符串,使用不同的语言(希望仍然使用Format()
)?
示例:
fmt.Println(time.Now().Format("Mon 2 January 2006"))
输出:
>Tue 28 January 2014
期望的输出:
>Tis 28 Januari 2014
英文:
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
答案1
得分: 8
如您在时间包的源代码中所见,值是硬编码在源代码中的。所以,基本上,Go语言目前不支持国际化(i18n)。国际化在Go语言的路线图上,甚至在常见问题解答中也有提到,但最近对这个主题没有任何评论。
与此同时,您可以尝试使用Monday包:
// 将LocaleEnUS更改为您想要用于翻译的区域设置
monday.Format(time.Now(), "Mon 2 January 2006", monday.LocaleEnUS)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论