1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-12-24 21:26:42 +00:00
cchat-gtk/internal/humanize/locale.go

43 lines
774 B
Go
Raw Normal View History

2020-05-26 06:51:06 +00:00
package humanize
import (
"log"
"strings"
"sync"
"unicode"
"github.com/Xuanwo/go-locale"
"github.com/goodsign/monday"
)
var Locale monday.Locale = monday.LocaleEnUS // changed on init
var localeOnce sync.Once
func lettersOnly(str string) string {
return strings.Map(func(r rune) rune {
if unicode.IsLetter(r) {
return r
}
return -1
}, str)
}
func ensureLocale() {
localeOnce.Do(func() {
if tag, err := locale.Detect(); err == nil {
Locale = monday.Locale(lettersOnly(tag.String()))
}
// Check if locale is supported
for _, locale := range monday.ListLocales() {
if lettersOnly(string(locale)) == string(Locale) {
return
}
}
log.Println("Locale", Locale, "not found, defaulting to en_US")
Locale = monday.LocaleEnUS
})
}