How do I convert date to the string in go using locale?

huangapple go评论87阅读模式
英文:

How do I convert date to the string in go using locale?

问题

我这样将日期转换为字符串:

d.Format("Mon 02. Jan")

然后我得到了类似的结果:

Fri 27. Jan

我该如何切换语言环境并以其他语言获取字符串?

英文:

I convert date to a string this way:

d.Format("Mon 02. Jan")

and I get something like

Fri 27. Jan

How can I switch the locale and get the string in other language?

答案1

得分: 14

你无法直接使用Go标准库来获取本地化的月份、日期和时区名称。这些名称是硬编码在time包中的。

例如,Month.String()返回的月份名称存储在未导出的time.month全局变量中:

var months = [...]string{
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December",
}

func (m Month) String() string { return months[m-1] }

类似地,星期几的名称来自Weekday.String(),存储在未导出的time.days变量中。

尽管如此,可能有第三方库可以满足你的需求。这里有一个不完整的库可能对你有所帮助:https://github.com/mattbaird/go-i18n-formats

如下所示,Igor Chubin分享的这个第三方库更加完整:https://github.com/klauspost/lctime

还要注意,尽管提供一个通用的、多语言的时间格式化包并不容易,但如果你真的需要,你可以将time包复制到你的项目中,并将名称翻译成你需要的语言。

此外,如果支持的语言数量较少且布局数量较少,你也可以自己创建格式化方式。

例如,下面的代码以匈牙利语格式化给定的time.Time值,使用你在问题中提到的布局:

func Format(t time.Time) string {
    return fmt.Sprintf("%s %02d. %s",
        days[t.Weekday()][:3], t.Day(), months[t.Month()-1][:3],
    )
}

var days = [...]string{
    "Vasárnap", "Hétfő", "Kedd", "Szerda", "Csütörtök", "Péntek", "Szombat",
}

var months = [...]string{
    "Január", "Február", "Március", "Április", "Május", "Június",
    "Július", "Augusztus", "Szeptember", "Október", "November", "December",
}

测试一下:

fmt.Println(Format(time.Now()))

Go Playground上的输出:

Ked 10. Nov

在我的本地机器上的输出:

Pén 27. Jan
英文:

You can't. The Go standard library does not contain localized month, day and zone names. The names are wired into the time package.

For example, the name of the months returned by Month.String() are stored in the unexported time.month global variable:

var months = [...]string{
	"January",
	"February",
	"March",
	"April",
	"May",
	"June",
	"July",
	"August",
	"September",
	"October",
	"November",
	"December",
}

func (m Month) String() string { return months[m-1] }

Similarly, names of weekdays come from Weekday.String(), stored in unexported variable time.days.

Having said that, there may be 3rd party libraries supporting your needs. Here's an incomplete one which might be of some help to you: https://github.com/mattbaird/go-i18n-formats

As shared by Igor Chubin below, this 3rd party lib is much more complete: https://github.com/klauspost/lctime

Also note that while providing a general, multilingual time formatting package is not an easy task, if you really need it, you could take the time package, copy it to your project and just translate the names to the language you need.

Also note that supporting a low number of languages and low number of layouts, it's easy to create the formatting yourself.

For example, the code below formats a given time.Time value in Hungarian, using the layout you used in your question:

func Format(t time.Time) string {
	return fmt.Sprintf("%s %02d. %s",
		days[t.Weekday()][:3], t.Day(), months[t.Month()-1][:3],
	)
}

var days = [...]string{
	"Vasárnap", "Hétfő", "Kedd", "Szerda", "Csütörtök", "Péntek", "Szombat"}

var months = [...]string{
	"Január", "Február", "Március", "Április", "Május", "Június",
	"Július", "Augusztus", "Szeptember", "Október", "November", "December",
}

Testing it:

fmt.Println(Format(time.Now()))

Output on the Go Playground:

Ked 10. Nov

Output on my local machine:

Pén 27. Jan

答案2

得分: 7

你可以使用替换器作为问题的快速修复方法。
德语示例:

r := strings.NewReplacer(
            "January", "Januar",
			"February", "Februar",
			"March", "März",
			"April", "April",
			"May", "Mai",
			"June", "Juni",
			"July", "Juli",
			"August", "August",
			"September", "September",
			"October", "Oktober",
			"November", "November",
			"December", "Dezember", )

	
time := inputTime.Format("2 January 2006")

outputString := r.Replace(time)
英文:

You can use a replacer as quickfix for the problem.
Example in german:

r := strings.NewReplacer(
            "January", "Januar",
			"February", "Februar",
			"March", "März",
			"April", "April",
			"May", "Mai",
			"June", "Juni",
			"July", "Juli",
			"August", "August",
			"September", "September",
			"October", "Oktober",
			"November", "November",
			"December", "Dezember", )

	
time := inputTime.Format("2 January 2006")

outputString := r.Replace(time)

huangapple
  • 本文由 发表于 2017年1月27日 23:18:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/41897265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定