Skip to content

Commit 92b69ff

Browse files
Honor package domain in Locale methods. Increase test coverage.
1 parent c583d09 commit 92b69ff

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

locale.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ func (l *Locale) AddDomain(dom string) {
106106
// Get uses a domain "default" to return the corresponding translation of a given string.
107107
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
108108
func (l *Locale) Get(str string, vars ...interface{}) string {
109-
return l.GetD("default", str, vars...)
109+
return l.GetD(GetDomain(), str, vars...)
110110
}
111111

112112
// GetN retrieves the (N)th plural form of translation for the given string in the "default" domain.
113113
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
114114
func (l *Locale) GetN(str, plural string, n int, vars ...interface{}) string {
115-
return l.GetND("default", str, plural, n, vars...)
115+
return l.GetND(GetDomain(), str, plural, n, vars...)
116116
}
117117

118118
// GetD returns the corresponding translation in the given domain for the given string.
@@ -143,13 +143,13 @@ func (l *Locale) GetND(dom, str, plural string, n int, vars ...interface{}) stri
143143
// GetC uses a domain "default" to return the corresponding translation of the given string in the given context.
144144
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
145145
func (l *Locale) GetC(str, ctx string, vars ...interface{}) string {
146-
return l.GetDC("default", str, ctx, vars...)
146+
return l.GetDC(GetDomain(), str, ctx, vars...)
147147
}
148148

149149
// GetNC retrieves the (N)th plural form of translation for the given string in the given context in the "default" domain.
150150
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
151151
func (l *Locale) GetNC(str, plural string, n int, ctx string, vars ...interface{}) string {
152-
return l.GetNDC("default", str, plural, n, ctx, vars...)
152+
return l.GetNDC(GetDomain(), str, plural, n, ctx, vars...)
153153
}
154154

155155
// GetDC returns the corresponding translation in the given domain for the given string in the given context.

locale_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ msgstr "More translation"
8686
// Add domain
8787
l.AddDomain("my_domain")
8888

89+
// Set global domain
90+
SetDomain("my_domain")
91+
8992
// Test translations
9093
tr := l.GetD("my_domain", "My text")
9194
if tr != "Translated text" {
@@ -105,7 +108,17 @@ msgstr "More translation"
105108
}
106109

107110
// Test context translations
111+
tr = l.GetC("Some random in a context", "Ctx")
112+
if tr != "Some random translation in a context" {
113+
t.Errorf("Expected 'Some random translation in a context'. Got '%s'", tr)
114+
}
115+
108116
v = "Test"
117+
tr = l.GetNC("One with var: %s", "Several with vars: %s", 23, "Ctx", v)
118+
if tr != "This one is the plural in a Ctx context: Test" {
119+
t.Errorf("Expected 'This one is the plural in a Ctx context: Test'. Got '%s'", tr)
120+
}
121+
109122
tr = l.GetDC("my_domain", "One with var: %s", "Ctx", v)
110123
if tr != "This one is the singular in a Ctx context: Test" {
111124
t.Errorf("Expected 'This one is the singular in a Ctx context: Test' but got '%s'", tr)
@@ -206,6 +219,9 @@ msgstr "More translation"
206219
// Add domain
207220
l.AddDomain("my_domain")
208221

222+
// Set default domain to make it fail
223+
SetDomain("default")
224+
209225
// Test non-existent "deafult" domain responses
210226
tr := l.Get("My text")
211227
if tr != "My text" {

0 commit comments

Comments
 (0)