// Code generated by @hebcal/po2catalog. DO NOT EDIT.
//
// Source of truth is the .po files in these npm packages:
//   @hebcal/hdate@0.22.5
//   @hebcal/core@6.6.0
//   @hebcal/locales@6.5.1
//   @hebcal/leyning@9.5.5
//   @hebcal/rest-api@6.5.1
//   @hebcal/learning@6.9.5
//
// Regenerate with: npm run generate

package locales

import (
	"strings"
	"sync"

	"golang.org/x/text/language"
	"golang.org/x/text/message"
	"golang.org/x/text/message/catalog"
)

// Catalog holds every Hebcal translation, keyed by BCP-47 language.Tag.
var Catalog = catalog.NewBuilder()

// Per-locale tags. Non-standard Hebcal locales use private-use subtags so they
// round-trip cleanly through golang.org/x/text/language.
var (
	tag_en                 = language.MustParse("en")
	tag_ashkenazi          = language.MustParse("und-x-ashkenaz")
	tag_he                 = language.MustParse("he")
	tag_he_x_NoNikud       = language.MustParse("he-x-nonikud")
	tag_ashkenazi_komatz   = language.MustParse("und-x-ashk-komatz")
	tag_ashkenazi_litvish  = language.MustParse("und-x-ashk-litvish")
	tag_ashkenazi_poylish  = language.MustParse("und-x-ashk-poylish")
	tag_ashkenazi_romanian = language.MustParse("und-x-ashk-romanian")
	tag_ashkenazi_standard = language.MustParse("und-x-ashk-standard")
	tag_de                 = language.MustParse("de")
	tag_es                 = language.MustParse("es")
	tag_fi                 = language.MustParse("fi")
	tag_fr                 = language.MustParse("fr")
	tag_hu                 = language.MustParse("hu")
	tag_nl                 = language.MustParse("nl")
	tag_pl                 = language.MustParse("pl")
	tag_pt                 = language.MustParse("pt")
	tag_ro                 = language.MustParse("ro")
	tag_ru                 = language.MustParse("ru")
	tag_uk                 = language.MustParse("uk")
	tag_yi                 = language.MustParse("yi")
)

// localeTags maps a lowercased Hebcal locale name to its catalogue tag.
var localeTags = map[string]language.Tag{
	"en":                 tag_en,
	"ashkenazi":          tag_ashkenazi,
	"he":                 tag_he,
	"he-x-nonikud":       tag_he_x_NoNikud,
	"ashkenazi_komatz":   tag_ashkenazi_komatz,
	"ashkenazi_litvish":  tag_ashkenazi_litvish,
	"ashkenazi_poylish":  tag_ashkenazi_poylish,
	"ashkenazi_romanian": tag_ashkenazi_romanian,
	"ashkenazi_standard": tag_ashkenazi_standard,
	"de":                 tag_de,
	"es":                 tag_es,
	"fi":                 tag_fi,
	"fr":                 tag_fr,
	"hu":                 tag_hu,
	"nl":                 tag_nl,
	"pl":                 tag_pl,
	"pt":                 tag_pt,
	"ro":                 tag_ro,
	"ru":                 tag_ru,
	"uk":                 tag_uk,
	"yi":                 tag_yi,
}

// AllLocales lists every supported Hebcal locale name, in priority order.
var AllLocales = []string{
	"en",
	"ashkenazi",
	"he",
	"he-x-NoNikud",
	"ashkenazi_komatz",
	"ashkenazi_litvish",
	"ashkenazi_poylish",
	"ashkenazi_romanian",
	"ashkenazi_standard",
	"de",
	"es",
	"fi",
	"fr",
	"hu",
	"nl",
	"pl",
	"pt",
	"ro",
	"ru",
	"uk",
	"yi",
}

var (
	printerOnce sync.Once
	printers    map[string]*message.Printer
)

func getPrinter(locale string) (*message.Printer, bool) {
	printerOnce.Do(func() {
		printers = make(map[string]*message.Printer, len(localeTags))
		for name, tag := range localeTags {
			printers[name] = message.NewPrinter(tag, message.Catalog(Catalog))
		}
	})
	p, ok := printers[locale]
	return p, ok
}

// LookupTranslation returns the translated message for key in the given locale.
// It returns ok=false when no translation exists (the key is returned as-is).
//
// "sephardic" and "" are aliases for the "en" locale. For dictionary locales,
// the catalogue never stores identity translations, so a Sprintf result equal
// to the key reliably means "not found".
func LookupTranslation(key string, locale string) (string, bool) {
	lang := strings.ToLower(locale)
	switch lang {
	case "", "sephardic":
		lang = "en"
	}
	p, ok := getPrinter(lang)
	if !ok {
		return key, false
	}
	s := p.Sprintf(key)
	if s == key && lang == "en" && !strings.HasPrefix(key, "MEMO:") {
		return key, true
	}
	if s == key {
		return key, false
	}
	return s, true
}
