2014-01-28 35 views
4

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

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

实施例:

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

输出:

星期二2014年1月28日

希望的输出:

提斯28 Januari 2014

Playground

回答

3

正如你可以在时间包sourcecode,价值观是硬编码源看到。所以,基本上,Go现在不支持i18n。 i18n在Go的路线图中,甚至在faq中提到过,但最近没有关于该主题的评论。

同时,你可以尝试使用Monday包:

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

我怕是这种情况。感谢您指出一个解决方案包。节省我的时间! – ANisus