![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
github.com/klauspost/lctime
Finally, simple, familiar, locale-based datetime formatting.
go get -u github.com/klauspost/lctime
// Default locale is based on env vars or en_US if none are set.
fmt.Println(lctime.Strftime("%c", time.Now()))
// prints: Mon 14 Dec 2015 10:31:56 PM PST
lctime.SetLocale("es_MX")
fmt.Println(lctime.Strftime("%c", time.Now()))
// prints: lun 14 dic 2015 22:31:56 PST
This is very easy if your application only has to translate to a single language.
To do translation to multiple languages without having collisions, you can use
the StrftimeLoc
function. It allows you to specify a locale along your time to be translated.
t := time.Date(2015, 12, 25, 3, 2, 1, 0, time.UTC)
txt, err := StrftimeLoc("es_MX", "%A, %d de %B de %Y", t)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(txt)
// Prints viernes, 25 de diciembre de 2015
If you need to do several translations or need to pass your localizer as a
parameter, you can use the
NewLocalizer
function. It allows you to localize several strings to the same language without
having to specify it every time.
t := time.Date(2015, 12, 25, 3, 2, 1, 0, time.UTC)
l, err := NewLocalizer("da_DK")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(l.Strftime("%A den %d. %B %Y", t))
// Prints: fredag den 25. december 2015
Go's standard library time
is fine most of the time. However, it's currently
impossible for the Go standard library to format dates based on a user's locale
or langauge setting. Unfortunately, that means code like this, doesn't work.
d := time.Date(2015, 12, 25, 3, 2, 1, 0, time.UTC)
fmt.Println(d.Format("lunes, 02 de enero de 2006"))
// got: lunes, 25 de enero de 2015
// want: viernes, 25 de diciembre de 2015
More importantly, it also means that non-English speakers will either be stuck with purely numeric dates or dates in a language they don't prefer.
lctime
brings the familiar setlocale
and strftime
functions found in other
programming languages like C, Python, or PHP. The formats and translations used
for this package are loosely based on glibc locale files, with close to 300
locales.
Locale data is just Go code, generated by go-bindata. This means you don't
have to worry about shipping anything extra. Just import and use lctime
like
any other package and go build
like normal. Everything will just work.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.