-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n_test.go
114 lines (95 loc) · 3.1 KB
/
i18n_test.go
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
package i18n_test
import (
"fmt"
"io/fs"
"os"
"testing"
"github.com/leonelquinteros/gotext"
"github.com/stretchr/testify/require"
"github.com/ubuntu/go-i18n"
"github.com/ubuntu/go-i18n/testdata/po"
)
func ExampleInitI18nDomain() {
i18n.InitI18nDomain("domain", nil)
fmt.Println(gotext.Get("some strings"))
}
func ExampleInitI18nDomain_embeddedFiles() {
i18n.InitI18nDomain("apt", po.Files)
fmt.Println(gotext.Get("unknown"))
}
func TestLanguageEnVariables(t *testing.T) {
tests := map[string]struct {
// default is singular/translated singular
envs map[string]string
want string
}{
"LANGUAGE has precedence over all": {
envs: map[string]string{"LANGUAGE": "fr_FR", "LC_ALL": "de_DE", "LC_MESSAGES": "en_US", "LANG": "dk"},
want: "fr_FR",
},
"LC_ALL has precedence over others": {
envs: map[string]string{"LANGUAGE": "", "LC_ALL": "de_DE", "LC_MESSAGES": "en_US", "LANG": "dk"},
want: "de_DE",
},
"LC_MESSAGES has precedence over others": {
envs: map[string]string{"LANGUAGE": "", "LC_ALL": "", "LC_MESSAGES": "en_US", "LANG": "dk"},
want: "en_US",
},
"LANG has precedence over others": {
envs: map[string]string{"LANGUAGE": "", "LC_ALL": "", "LC_MESSAGES": "", "LANG": "dk"},
want: "dk",
},
"Defaults to C": {
envs: map[string]string{"LANGUAGE": "", "LC_ALL": "", "LC_MESSAGES": "", "LANG": ""},
want: "C",
},
"Locale is simplified": {
envs: map[string]string{"LANGUAGE": "fr_FR@UTF-8"},
want: "fr_FR",
},
}
for name, tc := range tests {
tc := tc
t.Run(name, func(t *testing.T) {
for k, v := range tc.envs {
t.Setenv(k, v)
}
i18n.InitI18nDomain("domain", nil)
require.Equal(t, tc.want, gotext.GetLanguage(), "Should select desired language")
})
}
}
func TestTranslations(t *testing.T) {
tests := map[string]struct {
msg string
lang string
localFS fs.FS
want string
}{
"Load system translations": {lang: "fr", want: "inconnu"},
"System translation fallbacks to simplified": {lang: "fr_FR", want: "inconnu"},
// Local translations.
"Local translation wins over system": {localFS: po.Files, lang: "fr", want: "inconnu from local"},
"Local translation is simplified too": {lang: "fr_FR", localFS: po.Files, want: "inconnu from local"},
"Local translation with string not in system": {msg: "translation not in system apt", localFS: po.Files, lang: "fr", want: "traduction pas dans system apt"},
"Complex domain wins over simplified one": {lang: "aa_BB", localFS: po.Files, want: "nekonata from aa_BB"},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Setenv("LANGUAGE", tc.lang)
// Check that the system has the system translation available for running the tests.
if tc.localFS == nil {
if _, err := os.Lstat("/usr/share/locale/fr/LC_MESSAGES/apt.mo"); err != nil {
t.Skipf("apt translation is not available on the system: %v", err)
}
}
i18n.InitI18nDomain("apt", tc.localFS)
msg := "unknown"
if tc.msg != "" {
msg = tc.msg
}
got := gotext.Get(msg)
require.Equal(t, tc.want, got, "Should be translated")
})
}
}