| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "embed" |
| 5 | "fmt" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | |
| 9 | "amadan.net/rastrillo/rastrillo" |
| 10 | |
| 11 | "amadan.net/rastrillo/abono" |
| 12 | ) |
| 13 | |
| 14 | //go:embed locales/en.toml |
| 15 | var localesFS embed.FS |
| 16 | |
| 17 | // Catalog is the base message catalog the example runs on: abono's |
| 18 | // English, then the example's own (locales/en.toml, the "example.*" keys |
| 19 | // its layout and pages use). A key belongs to the module that renders it, |
| 20 | // so abono's catalog has none of these. |
| 21 | func Catalog() rastrillo.Catalog { |
| 22 | cat := abono.Catalog() |
| 23 | own, err := ownCatalog() |
| 24 | if err != nil { |
| 25 | panic(fmt.Sprintf("example: locales/en.toml: %v", err)) // a build bug |
| 26 | } |
| 27 | for k, v := range own { |
| 28 | cat[k] = v |
| 29 | } |
| 30 | return cat |
| 31 | } |
| 32 | |
| 33 | // ownCatalog is locales/en.toml: one key = "value" per line, with # |
| 34 | // comments and blank lines, the flat format rastrillo's catalogs use. |
| 35 | func ownCatalog() (rastrillo.Catalog, error) { |
| 36 | data, err := localesFS.ReadFile("locales/en.toml") |
| 37 | if err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | out := rastrillo.Catalog{} |
| 41 | for i, raw := range strings.Split(string(data), "\n") { |
| 42 | line := strings.TrimSpace(raw) |
| 43 | if line == "" || strings.HasPrefix(line, "#") { |
| 44 | continue |
| 45 | } |
| 46 | key, val, ok := strings.Cut(line, " = ") |
| 47 | text, err := strconv.Unquote(strings.TrimSpace(val)) |
| 48 | if !ok || key == "" || err != nil { |
| 49 | return nil, fmt.Errorf("line %d: want key = \"value\"", i+1) |
| 50 | } |
| 51 | if _, dup := out[key]; dup { |
| 52 | return nil, fmt.Errorf("line %d: duplicate key %q", i+1, key) |
| 53 | } |
| 54 | out[key] = text |
| 55 | } |
| 56 | return out, nil |
| 57 | } |
| 58 | |