105 lines
2.1 KiB
Go
105 lines
2.1 KiB
Go
/*
|
|
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
package locale
|
|
|
|
import (
|
|
"context"
|
|
"github.com/leonelquinteros/gotext"
|
|
"golang.org/x/text/language"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/database"
|
|
)
|
|
|
|
type Locale struct {
|
|
*gotext.Locale
|
|
CurrencyPattern string
|
|
Language language.Tag
|
|
}
|
|
|
|
type Locales map[language.Tag]*Locale
|
|
|
|
func (m Locales) Tags() []language.Tag {
|
|
keys := make([]language.Tag, len(m))
|
|
i := 0
|
|
for k := range m {
|
|
keys[i] = k
|
|
i++
|
|
}
|
|
return keys
|
|
}
|
|
|
|
func MustGetAll(db *database.DB) Locales {
|
|
availableLanguages := mustGetAvailableLanguages(db)
|
|
locales := map[language.Tag]*Locale{}
|
|
for _, lang := range availableLanguages {
|
|
locale := newLocale(lang)
|
|
locale.AddDomain("camper")
|
|
locales[lang.tag] = locale
|
|
}
|
|
return locales
|
|
}
|
|
|
|
func newLocale(lang availableLanguage) *Locale {
|
|
return &Locale{
|
|
gotext.NewLocale("locale", lang.tag.String()),
|
|
lang.currencyPattern,
|
|
lang.tag,
|
|
}
|
|
}
|
|
|
|
func (l *Locale) Gettext(str string) string {
|
|
return l.GetD(l.GetDomain(), str)
|
|
}
|
|
|
|
func (l *Locale) GettextNoop(str string) string {
|
|
return str
|
|
}
|
|
|
|
func Match(acceptLanguage string, locales Locales, matcher language.Matcher) *Locale {
|
|
t, _, err := language.ParseAcceptLanguage(acceptLanguage)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
var locale *Locale
|
|
tag, _, _ := matcher.Match(t...)
|
|
var ok bool
|
|
locale, ok = locales[tag]
|
|
for !ok && !tag.IsRoot() {
|
|
tag = tag.Parent()
|
|
locale, ok = locales[tag]
|
|
}
|
|
return locale
|
|
}
|
|
|
|
type availableLanguage struct {
|
|
tag language.Tag
|
|
currencyPattern string
|
|
}
|
|
|
|
func mustGetAvailableLanguages(db *database.DB) []availableLanguage {
|
|
rows, err := db.Query(context.Background(), "select lang_tag, currency_pattern from language where selectable")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var languages []availableLanguage
|
|
for rows.Next() {
|
|
var langTag string
|
|
var currencyPattern string
|
|
err = rows.Scan(&langTag, ¤cyPattern)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
languages = append(languages, availableLanguage{language.MustParse(langTag), currencyPattern})
|
|
}
|
|
if rows.Err() != nil {
|
|
panic(rows.Err())
|
|
}
|
|
|
|
return languages
|
|
}
|