feat(i18n): add english and german message catalogs
Tests fail on catalogs with diverging keys and on message IDs used in code or templates that no catalog defines. Refs #42 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
db44fedc72
commit
7dd1f87ffe
6 files changed
+436
No files matched your search
@@ -0,0 +1,155 @@
|
||||
package i18n
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func catalogKeys(t *testing.T) map[string]map[string]bool {
|
||||
t.Helper()
|
||||
files, err := fs.Glob(locales, "locales/*.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out := map[string]map[string]bool{}
|
||||
for _, f := range files {
|
||||
data, err := locales.ReadFile(f)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var m map[string]any
|
||||
if err := json.Unmarshal(data, &m); err != nil {
|
||||
t.Fatalf("%s: %v", f, err)
|
||||
}
|
||||
keys := map[string]bool{}
|
||||
for k := range m {
|
||||
keys[k] = true
|
||||
}
|
||||
out[strings.TrimSuffix(filepath.Base(f), ".json")] = keys
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestCatalogsHaveSameKeys(t *testing.T) {
|
||||
cats := catalogKeys(t)
|
||||
en := cats["en"]
|
||||
for lang, keys := range cats {
|
||||
for k := range en {
|
||||
if !keys[k] {
|
||||
t.Errorf("%s.json is missing %q", lang, k)
|
||||
}
|
||||
}
|
||||
for k := range keys {
|
||||
if !en[k] {
|
||||
t.Errorf("%s.json has %q which en.json lacks", lang, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var usagePatterns = []struct {
|
||||
re *regexp.Regexp
|
||||
prefix string
|
||||
}{
|
||||
{regexp.MustCompile(`i18n\.T\(ctx, "([a-z0-9_.]+)"[,)]`), ""},
|
||||
{regexp.MustCompile(`\bfail\(c, [^,]+, "([a-z_]+)"`), "error."},
|
||||
{regexp.MustCompile(`\b(?:pageFor\(c|s\.page\()(?:, )?"([a-z0-9_.]+)"`), ""},
|
||||
}
|
||||
|
||||
// TestUsedKeysExist scans the source tree for literal message IDs so a typo
|
||||
// fails the build instead of showing a raw ID in production.
|
||||
func TestUsedKeysExist(t *testing.T) {
|
||||
en := catalogKeys(t)["en"]
|
||||
var missing []string
|
||||
err := filepath.WalkDir("..", func(p string, d fs.DirEntry, err error) error {
|
||||
if err != nil || d.IsDir() {
|
||||
return err
|
||||
}
|
||||
if !strings.HasSuffix(p, ".go") && !strings.HasSuffix(p, ".templ") {
|
||||
return nil
|
||||
}
|
||||
if strings.HasSuffix(p, "_templ.go") || strings.HasSuffix(p, "_test.go") {
|
||||
return nil
|
||||
}
|
||||
src, err := os.ReadFile(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, u := range usagePatterns {
|
||||
for _, m := range u.re.FindAllStringSubmatch(string(src), -1) {
|
||||
if id := u.prefix + m[1]; !en[id] {
|
||||
missing = append(missing, p+": "+id)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sort.Strings(missing)
|
||||
for _, m := range missing {
|
||||
t.Errorf("unknown message ID %s", m)
|
||||
}
|
||||
}
|
||||
|
||||
// The language switcher builds "lang.<code>" dynamically.
|
||||
func TestEveryLanguageHasAName(t *testing.T) {
|
||||
c, err := Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
en := catalogKeys(t)["en"]
|
||||
for _, l := range c.Supported() {
|
||||
if !en["lang."+l] {
|
||||
t.Errorf("en.json is missing %q", "lang."+l)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatch(t *testing.T) {
|
||||
c, err := Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tests := []struct {
|
||||
prefs []string
|
||||
want string
|
||||
}{
|
||||
{nil, "en"},
|
||||
{[]string{"de"}, "de"},
|
||||
{[]string{"", "de-AT,en;q=0.5"}, "de"},
|
||||
{[]string{"fr-FR,fr;q=0.9"}, "en"},
|
||||
{[]string{"en", "de"}, "en"},
|
||||
{[]string{"garbage", "de"}, "de"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := c.Match(tt.prefs...).Lang(); got != tt.want {
|
||||
t.Errorf("Match(%q) = %q, want %q", tt.prefs, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestT(t *testing.T) {
|
||||
c, err := Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := NewContext(context.Background(), c.Match("de"))
|
||||
if got, want := T(ctx, "error.password_too_short", "Min", 12), "Das Passwort braucht mindestens 12 Zeichen."; got != want {
|
||||
t.Errorf("T(de, password_too_short) = %q, want %q", got, want)
|
||||
}
|
||||
if got, want := T(ctx, "no.such.key"), "no.such.key"; got != want {
|
||||
t.Errorf("T(missing) = %q, want %q", got, want)
|
||||
}
|
||||
if got, want := T(context.Background(), "auth.login.title"), "auth.login.title"; got != want {
|
||||
t.Errorf("T(no localizer) = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user