-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmsgcat_cldr_test.go
More file actions
115 lines (109 loc) · 3.19 KB
/
msgcat_cldr_test.go
File metadata and controls
115 lines (109 loc) · 3.19 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package msgcat
import (
"context"
"os"
"path/filepath"
"testing"
)
func TestGetMessageWithCtx_CLDRForms(t *testing.T) {
dir := t.TempDir()
en := []byte(`default:
short: Unexpected error
long: Unexpected message
set:
person.cats:
short_forms:
one: "{{name}} has {{count}} cat."
other: "{{name}} has {{count}} cats."
long_forms:
one: "{{name}} has one cat."
other: "{{name}} has {{count}} cats."
plural_param: count
`)
if err := os.WriteFile(filepath.Join(dir, "en.yaml"), en, 0644); err != nil {
t.Fatal(err)
}
catalog, err := NewMessageCatalog(Config{ResourcePath: dir})
if err != nil {
t.Fatal(err)
}
ctx := context.WithValue(context.Background(), "language", "en")
// count=1 -> one
msg1 := catalog.GetMessageWithCtx(ctx, "person.cats", Params{"name": "Nick", "count": 1})
if msg1.ShortText != "Nick has 1 cat." {
t.Errorf("count=1 short: got %q", msg1.ShortText)
}
if msg1.LongText != "Nick has one cat." {
t.Errorf("count=1 long: got %q", msg1.LongText)
}
// count=2 -> other
msg2 := catalog.GetMessageWithCtx(ctx, "person.cats", Params{"name": "Nick", "count": 2})
if msg2.ShortText != "Nick has 2 cats." {
t.Errorf("count=2 short: got %q", msg2.ShortText)
}
if msg2.LongText != "Nick has 2 cats." {
t.Errorf("count=2 long: got %q", msg2.LongText)
}
}
func TestGetMessageWithCtx_CLDRForms_fallbackToOther(t *testing.T) {
dir := t.TempDir()
en := []byte(`default:
short: Err
long: Err
set:
items:
short_forms:
other: "{{count}} items"
long_forms:
other: "{{count}} items total"
`)
if err := os.WriteFile(filepath.Join(dir, "en.yaml"), en, 0644); err != nil {
t.Fatal(err)
}
catalog, err := NewMessageCatalog(Config{ResourcePath: dir})
if err != nil {
t.Fatal(err)
}
ctx := context.WithValue(context.Background(), "language", "en")
msg := catalog.GetMessageWithCtx(ctx, "items", Params{"count": 1})
// only "other" form defined; Form(en,1)=one but we fall back to other
if msg.ShortText != "1 items" {
t.Errorf("short: got %q", msg.ShortText)
}
}
func TestLoadMessages_preservesShortFormsLongFormsPluralParam(t *testing.T) {
dir := t.TempDir()
en := []byte(`default:
short: Err
long: Err
set: {}
`)
if err := os.WriteFile(filepath.Join(dir, "en.yaml"), en, 0644); err != nil {
t.Fatal(err)
}
catalog, err := NewMessageCatalog(Config{ResourcePath: dir})
if err != nil {
t.Fatal(err)
}
err = catalog.LoadMessages("en", []RawMessage{{
Key: "sys.cats",
ShortForms: map[string]string{"one": "{{name}} has {{n}} cat.", "other": "{{name}} has {{n}} cats."},
LongForms: map[string]string{"one": "One cat.", "other": "{{n}} cats."},
PluralParam: "n",
}})
if err != nil {
t.Fatal(err)
}
ctx := context.WithValue(context.Background(), "language", "en")
msg1 := catalog.GetMessageWithCtx(ctx, "sys.cats", Params{"name": "Alice", "n": 1})
if msg1.ShortText != "Alice has 1 cat." {
t.Errorf("count=1 short: got %q", msg1.ShortText)
}
if msg1.LongText != "One cat." {
t.Errorf("count=1 long: got %q", msg1.LongText)
}
msg2 := catalog.GetMessageWithCtx(ctx, "sys.cats", Params{"name": "Alice", "n": 2})
if msg2.ShortText != "Alice has 2 cats." {
t.Errorf("count=2 short: got %q", msg2.ShortText)
}
}