Skip to content

Commit

Permalink
Honor package domain in Locale methods. Increase test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
leonelquinteros committed Feb 14, 2018
1 parent c583d09 commit 92b69ff
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 4 additions & 4 deletions locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ func (l *Locale) AddDomain(dom string) {
// Get uses a domain "default" to return the corresponding translation of a given string.
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (l *Locale) Get(str string, vars ...interface{}) string {
return l.GetD("default", str, vars...)
return l.GetD(GetDomain(), str, vars...)
}

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

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

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

// GetDC returns the corresponding translation in the given domain for the given string in the given context.
Expand Down
16 changes: 16 additions & 0 deletions locale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ msgstr "More translation"
// Add domain
l.AddDomain("my_domain")

// Set global domain
SetDomain("my_domain")

// Test translations
tr := l.GetD("my_domain", "My text")
if tr != "Translated text" {
Expand All @@ -105,7 +108,17 @@ msgstr "More translation"
}

// Test context translations
tr = l.GetC("Some random in a context", "Ctx")
if tr != "Some random translation in a context" {
t.Errorf("Expected 'Some random translation in a context'. Got '%s'", tr)
}

v = "Test"
tr = l.GetNC("One with var: %s", "Several with vars: %s", 23, "Ctx", v)
if tr != "This one is the plural in a Ctx context: Test" {
t.Errorf("Expected 'This one is the plural in a Ctx context: Test'. Got '%s'", tr)
}

tr = l.GetDC("my_domain", "One with var: %s", "Ctx", v)
if tr != "This one is the singular in a Ctx context: Test" {
t.Errorf("Expected 'This one is the singular in a Ctx context: Test' but got '%s'", tr)
Expand Down Expand Up @@ -206,6 +219,9 @@ msgstr "More translation"
// Add domain
l.AddDomain("my_domain")

// Set default domain to make it fail
SetDomain("default")

// Test non-existent "deafult" domain responses
tr := l.Get("My text")
if tr != "My text" {
Expand Down

0 comments on commit 92b69ff

Please sign in to comment.