Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localization when using time.Format

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

like image 836
ANisus Avatar asked Jan 28 '14 08:01

ANisus


People also ask

What is date and time localization?

Date and time localization is quite an important topic when it comes to developing applications and software systems in multiple languages. Date and time are written and read in different ways in different countries.

How do I localize date and time in PHP?

These are the basics of date and time localization in PHP. You can refer to this documentation article to get a more complete understanding of this function. To properly localize date and time in Ruby on Rails, use the localize method aliased as l.

How do I format the time using the default browser locale?

After the appropriate locale has been set, you can easily format the time using FormatDateTime, a locale-aware function. Suppose you have retrieved the Chinese (Taiwan) locale as the primary browser locale ("zh-TW" is the default value for this locale).

How to set and get time zones in Java?

TimeZone is another class for handling time zones in Java. Let’s see how to set and get time zones in Java with the TimeZone Java class. First, you need to import the time zone package as follows: Obtained the current date and time with time zone. Got the current time in different time zones by providing the actual time zone.


1 Answers

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) 
like image 161
Alvivi Avatar answered Oct 17 '22 14:10

Alvivi