-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_test.go
More file actions
77 lines (65 loc) · 2.18 KB
/
file_test.go
File metadata and controls
77 lines (65 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package i18n
import (
"os"
"path/filepath"
"testing"
)
func writeTempFile(t *testing.T, pattern string, content string) string {
t.Helper()
dir := t.TempDir()
path := filepath.Join(dir, pattern)
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatalf("failed writing temp file: %v", err)
}
return path
}
func TestFileJSON(t *testing.T) {
jsonContent := `{
"hello_world_b10a": "Hello World",
"hello_name_a696": "Hello {name}",
"bag_count_0d3e": {"singular": "{qty} bag", "plural": "{qty} bags"}
}`
path := writeTempFile(t, "en.json", jsonContent)
tr, err := NewFileFrom(path)
if err != nil {
t.Fatalf("failed to load json file: %v", err)
}
if val := tr.Translate("hello_world_b10a"); val != "Hello World" {
t.Error("Basic JSON translation failed, got " + val)
}
if val := tr.TranslateWith("hello_name_a696", Args("name", "John")); val != "Hello John" {
t.Error("Arg JSON translation failed, got " + val)
}
if val := tr.TranslatePlural("bag_count_0d3e", 1, nil); val != "1 bag" {
t.Error("Plural JSON translation (singular) failed, got " + val)
}
if val := tr.TranslatePlural("bag_count_0d3e", 5, nil); val != "5 bags" {
t.Error("Plural JSON translation (plural) failed, got " + val)
}
}
func TestFileYAML(t *testing.T) {
// Simple flat YAML map
yamlContent := "" +
"hello_world_b10a: Hello World\n" +
"hello_name_a696: 'Hello {name}'\n" +
"bag_count_0d3e:\n" +
" singular: '{qty} bag'\n" +
" plural: '{qty} bags'\n"
path := writeTempFile(t, "en.yaml", yamlContent)
tr, err := NewFileFrom(path)
if err != nil {
t.Fatalf("failed to load yaml file: %v", err)
}
if val := tr.Translate("hello_world_b10a"); val != "Hello World" {
t.Error("Basic YAML translation failed, got " + val)
}
if val := tr.TranslateWith("hello_name_a696", Args("name", "Jane")); val != "Hello Jane" {
t.Error("Arg YAML translation failed, got " + val)
}
if val := tr.TranslatePlural("bag_count_0d3e", 1, nil); val != "1 bag" {
t.Error("Plural YAML translation (singular) failed, got " + val)
}
if val := tr.TranslatePlural("bag_count_0d3e", 2, nil); val != "2 bags" {
t.Error("Plural YAML translation (plural) failed, got " + val)
}
}