|
| 1 | +package i18n |
| 2 | + |
| 3 | +import ( |
| 4 | + "embed" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +//go:embed testdata |
| 9 | +var testFS embed.FS |
| 10 | + |
| 11 | +func TestTranslatorLoadFromFS(t *testing.T) { |
| 12 | + tr := New() |
| 13 | + err := tr.LoadFromFS(testFS, "testdata") |
| 14 | + if err != nil { |
| 15 | + t.Fatalf("LoadFromFS failed: %v", err) |
| 16 | + } |
| 17 | + |
| 18 | + if !tr.HasLocale("en") { |
| 19 | + t.Error("expected locale 'en' to be loaded") |
| 20 | + } |
| 21 | + if !tr.HasLocale("es") { |
| 22 | + t.Error("expected locale 'es' to be loaded") |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestTranslatorGet(t *testing.T) { |
| 27 | + tr := New() |
| 28 | + if err := tr.LoadFromFS(testFS, "testdata"); err != nil { |
| 29 | + t.Fatalf("LoadFromFS failed: %v", err) |
| 30 | + } |
| 31 | + |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + locale string |
| 35 | + key string |
| 36 | + expected string |
| 37 | + }{ |
| 38 | + { |
| 39 | + name: "simple key in english", |
| 40 | + locale: "en", |
| 41 | + key: "common.search", |
| 42 | + expected: "Search", |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "simple key in spanish", |
| 46 | + locale: "es", |
| 47 | + key: "common.search", |
| 48 | + expected: "Buscar", |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "nested key", |
| 52 | + locale: "en", |
| 53 | + key: "user.name", |
| 54 | + expected: "Name", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "fallback to english when key missing in locale", |
| 58 | + locale: "es", |
| 59 | + key: "user.only_english", |
| 60 | + expected: "Only in English", |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "missing key returns key itself", |
| 64 | + locale: "en", |
| 65 | + key: "missing.key.path", |
| 66 | + expected: "missing.key.path", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "unknown locale falls back to english", |
| 70 | + locale: "fr", |
| 71 | + key: "common.search", |
| 72 | + expected: "Search", |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for _, tt := range tests { |
| 77 | + t.Run(tt.name, func(t *testing.T) { |
| 78 | + result := tr.Get(tt.locale, tt.key) |
| 79 | + if result != tt.expected { |
| 80 | + t.Errorf("Get(%q, %q) = %q, want %q", tt.locale, tt.key, result, tt.expected) |
| 81 | + } |
| 82 | + }) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestTranslatorAvailableLocales(t *testing.T) { |
| 87 | + tr := New() |
| 88 | + if err := tr.LoadFromFS(testFS, "testdata"); err != nil { |
| 89 | + t.Fatalf("LoadFromFS failed: %v", err) |
| 90 | + } |
| 91 | + |
| 92 | + locales := tr.AvailableLocales() |
| 93 | + if len(locales) < 2 { |
| 94 | + t.Errorf("expected at least 2 locales, got %d", len(locales)) |
| 95 | + } |
| 96 | + |
| 97 | + found := make(map[string]bool) |
| 98 | + for _, loc := range locales { |
| 99 | + found[loc] = true |
| 100 | + } |
| 101 | + |
| 102 | + if !found["en"] { |
| 103 | + t.Error("expected 'en' in available locales") |
| 104 | + } |
| 105 | + if !found["es"] { |
| 106 | + t.Error("expected 'es' in available locales") |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestTranslatorSetDefaultLocale(t *testing.T) { |
| 111 | + tr := New() |
| 112 | + if err := tr.LoadFromFS(testFS, "testdata"); err != nil { |
| 113 | + t.Fatalf("LoadFromFS failed: %v", err) |
| 114 | + } |
| 115 | + |
| 116 | + tr.SetDefaultLocale("es") |
| 117 | + if tr.DefaultLocaleValue() != "es" { |
| 118 | + t.Errorf("expected default locale 'es', got %q", tr.DefaultLocaleValue()) |
| 119 | + } |
| 120 | + |
| 121 | + result := tr.Get("fr", "common.search") |
| 122 | + if result != "Buscar" { |
| 123 | + t.Errorf("expected Spanish fallback 'Buscar', got %q", result) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func TestTranslatorTranslateFunc(t *testing.T) { |
| 128 | + tr := New() |
| 129 | + if err := tr.LoadFromFS(testFS, "testdata"); err != nil { |
| 130 | + t.Fatalf("LoadFromFS failed: %v", err) |
| 131 | + } |
| 132 | + |
| 133 | + fn := tr.TranslateFunc("es") |
| 134 | + result := fn("common.search") |
| 135 | + if result != "Buscar" { |
| 136 | + t.Errorf("TranslateFunc(es)(common.search) = %q, want 'Buscar'", result) |
| 137 | + } |
| 138 | +} |
0 commit comments