package main import ( "embed" "fmt" "strconv" "strings" "amadan.net/rastrillo/rastrillo" "amadan.net/rastrillo/abono" ) //go:embed locales/en.toml var localesFS embed.FS // Catalog is the base message catalog the example runs on: abono's // English, then the example's own (locales/en.toml, the "example.*" keys // its layout and pages use). A key belongs to the module that renders it, // so abono's catalog has none of these. func Catalog() rastrillo.Catalog { cat := abono.Catalog() own, err := ownCatalog() if err != nil { panic(fmt.Sprintf("example: locales/en.toml: %v", err)) // a build bug } for k, v := range own { cat[k] = v } return cat } // ownCatalog is locales/en.toml: one key = "value" per line, with # // comments and blank lines, the flat format rastrillo's catalogs use. func ownCatalog() (rastrillo.Catalog, error) { data, err := localesFS.ReadFile("locales/en.toml") if err != nil { return nil, err } out := rastrillo.Catalog{} for i, raw := range strings.Split(string(data), "\n") { line := strings.TrimSpace(raw) if line == "" || strings.HasPrefix(line, "#") { continue } key, val, ok := strings.Cut(line, " = ") text, err := strconv.Unquote(strings.TrimSpace(val)) if !ok || key == "" || err != nil { return nil, fmt.Errorf("line %d: want key = \"value\"", i+1) } if _, dup := out[key]; dup { return nil, fmt.Errorf("line %d: duplicate key %q", i+1, key) } out[key] = text } return out, nil }