/* * SPDX-FileCopyrightText: 2023 jordi fita mas * 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 GetAll(ctx context.Context, db *database.DB) (Locales, error) { availableLanguages, err := getAvailableLanguages(ctx, db) if err != nil { return nil, err } locales := map[language.Tag]*Locale{} for _, lang := range availableLanguages { locale := newLocale(lang) locale.AddDomain("camper") locales[lang.tag] = locale } return locales, nil } 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) Pgettext(str string, ctx string) string { return l.GetDC(l.GetDomain(), str, ctx) } 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, _, confidence := matcher.Match(t...) if confidence > language.No { 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 getAvailableLanguages(ctx context.Context, db *database.DB) ([]availableLanguage, error) { rows, err := db.Query(ctx, "select lang_tag, currency_pattern from language where selectable") if err != nil { return nil, err } defer rows.Close() var languages []availableLanguage for rows.Next() { var langTag string var currencyPattern string err = rows.Scan(&langTag, ¤cyPattern) if err != nil { return nil, err } languages = append(languages, availableLanguage{language.MustParse(langTag), currencyPattern}) } if rows.Err() != nil { return nil, rows.Err() } return languages, nil }