2021-10-14 04:16:45 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/text/collate"
|
|
|
|
"golang.org/x/text/language"
|
|
|
|
)
|
|
|
|
|
|
|
|
// matcher defines a matcher for the languages we support
|
|
|
|
var matcher = language.NewMatcher([]language.Tag{
|
|
|
|
language.MustParse("en-US"), // The first language is used as fallback.
|
|
|
|
language.MustParse("en-GB"),
|
|
|
|
language.MustParse("en-AU"),
|
2021-10-25 02:07:16 +00:00
|
|
|
language.MustParse("es-ES"),
|
2021-10-14 04:16:45 +00:00
|
|
|
language.MustParse("de-DE"),
|
2021-10-24 23:41:29 +00:00
|
|
|
language.MustParse("it-IT"),
|
2021-11-08 00:41:05 +00:00
|
|
|
language.MustParse("fr-FR"),
|
2021-11-29 03:43:18 +00:00
|
|
|
language.MustParse("fi-FI"),
|
2021-10-14 04:16:45 +00:00
|
|
|
language.MustParse("pt-BR"),
|
|
|
|
language.MustParse("sv-SE"),
|
|
|
|
language.MustParse("zh-CN"),
|
|
|
|
language.MustParse("zh-TW"),
|
2022-01-05 03:21:15 +00:00
|
|
|
language.MustParse("hr-HR"),
|
|
|
|
language.MustParse("nl-NL"),
|
|
|
|
language.MustParse("ru-RU"),
|
|
|
|
language.MustParse("tr-TR"),
|
2022-05-13 19:45:05 +00:00
|
|
|
language.MustParse("da-DK"),
|
|
|
|
language.MustParse("pl-PL"),
|
2021-10-14 04:16:45 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// newCollator parses a locale into a collator
|
|
|
|
// Go through the available matches and return a valid match, in practice the first is a fallback
|
|
|
|
// Optionally pass collation options through for creation.
|
|
|
|
// If passed a nil-locale string, return nil
|
|
|
|
func newCollator(locale *string, opts ...collate.Option) *collate.Collator {
|
|
|
|
if locale == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tag, _ := language.MatchStrings(matcher, *locale)
|
|
|
|
return collate.New(tag, opts...)
|
|
|
|
}
|