Skip to content

Commit 1876ab2

Browse files
committed
Fix IsTranslated* functions using n directly rather than using the nth plural form; fixes #92
1 parent c7c2135 commit 1876ab2

File tree

3 files changed

+27
-31
lines changed

3 files changed

+27
-31
lines changed

domain.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ func (do *Domain) GetNC(str, plural string, n int, ctx string, vars ...interface
401401

402402
// IsTranslated reports whether a string is translated
403403
func (do *Domain) IsTranslated(str string) bool {
404-
return do.IsTranslatedN(str, 0)
404+
return do.IsTranslatedN(str, 1)
405405
}
406406

407407
// IsTranslatedN reports whether a plural string is translated
@@ -416,12 +416,12 @@ func (do *Domain) IsTranslatedN(str string, n int) bool {
416416
if !ok {
417417
return false
418418
}
419-
return tr.IsTranslatedN(n)
419+
return tr.IsTranslatedN(do.pluralForm(n))
420420
}
421421

422422
// IsTranslatedC reports whether a context string is translated
423423
func (do *Domain) IsTranslatedC(str, ctx string) bool {
424-
return do.IsTranslatedNC(str, 0, ctx)
424+
return do.IsTranslatedNC(str, 1, ctx)
425425
}
426426

427427
// IsTranslatedNC reports whether a plural context string is translated
@@ -440,7 +440,7 @@ func (do *Domain) IsTranslatedNC(str string, n int, ctx string) bool {
440440
if !ok {
441441
return false
442442
}
443-
return tr.IsTranslatedN(n)
443+
return tr.IsTranslatedN(do.pluralForm(n))
444444
}
445445

446446
// GetTranslations returns a copy of every translation in the domain. It does not support contexts.

domain_test.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const (
99
arFixture = "fixtures/ar/categories.po"
1010
)
1111

12-
//since both Po and Mo just pass-through to Domain for MarshalBinary and UnmarshalBinary, test it here
12+
// since both Po and Mo just pass-through to Domain for MarshalBinary and UnmarshalBinary, test it here
1313
func TestBinaryEncoding(t *testing.T) {
1414
// Create po objects
1515
po := NewPo()
@@ -85,11 +85,11 @@ func TestDomain_IsTranslated(t *testing.T) {
8585
if english.IsTranslated("Another string") {
8686
t.Error("'Another string' should be reported as not translated.")
8787
}
88-
if !english.IsTranslatedN("Empty plural form singular", 0) {
89-
t.Error("'Empty plural form singular' should be reported as translated for n=0.")
88+
if !english.IsTranslatedN("Empty plural form singular", 1) {
89+
t.Error("'Empty plural form singular' should be reported as translated for n=1.")
9090
}
91-
if english.IsTranslatedN("Empty plural form singular", 1) {
92-
t.Error("'Empty plural form singular' should be reported as not translated for n=1.")
91+
if english.IsTranslatedN("Empty plural form singular", 0) {
92+
t.Error("'Empty plural form singular' should be reported as not translated for n=0.")
9393
}
9494

9595
arabicPo := NewPo()
@@ -106,11 +106,8 @@ func TestDomain_IsTranslated(t *testing.T) {
106106
if !arabic.IsTranslatedN("Load %d more document", 1) {
107107
t.Error("Arabic plural should be reported as translated for n=1.")
108108
}
109-
if !arabic.IsTranslatedN("Load %d more document", 5) {
110-
t.Error("Arabic plural should be reported as translated for n=5.")
111-
}
112-
if arabic.IsTranslatedN("Load %d more document", 6) {
113-
t.Error("Arabic plural should be reported as not translated for n=6.")
109+
if !arabic.IsTranslatedN("Load %d more document", 100) {
110+
t.Error("Arabic plural should be reported as translated for n=100.")
114111
}
115112

116113
// context
@@ -120,7 +117,7 @@ func TestDomain_IsTranslated(t *testing.T) {
120117
if !english.IsTranslatedNC("One with var: %s", 0, "Ctx") {
121118
t.Error("Context plural should be reported as translated for n=0")
122119
}
123-
if english.IsTranslatedNC("One with var: %s", 2, "Ctx") {
120+
if !english.IsTranslatedNC("One with var: %s", 2, "Ctx") {
124121
t.Error("Context plural should be reported as translated for n=2")
125122
}
126123
}

gotext.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@ Package gotext implements GNU gettext utilities.
33
44
For quick/simple translations you can use the package level functions directly.
55
6-
import (
7-
"fmt"
8-
"github.com/leonelquinteros/gotext"
9-
)
6+
import (
7+
"fmt"
8+
"github.com/leonelquinteros/gotext"
9+
)
1010
11-
func main() {
12-
// Configure package
13-
gotext.Configure("/path/to/locales/root/dir", "en_UK", "domain-name")
11+
func main() {
12+
// Configure package
13+
gotext.Configure("/path/to/locales/root/dir", "en_UK", "domain-name")
1414
15-
// Translate text from default domain
16-
fmt.Println(gotext.Get("My text on 'domain-name' domain"))
17-
18-
// Translate text from a different domain without reconfigure
19-
fmt.Println(gotext.GetD("domain2", "Another text on a different domain"))
20-
}
15+
// Translate text from default domain
16+
fmt.Println(gotext.Get("My text on 'domain-name' domain"))
2117
18+
// Translate text from a different domain without reconfigure
19+
fmt.Println(gotext.GetD("domain2", "Another text on a different domain"))
20+
}
2221
*/
2322
package gotext
2423

@@ -342,7 +341,7 @@ func GetNDC(dom, str, plural string, n int, ctx string, vars ...interface{}) str
342341
// IsTranslated reports whether a string is translated in given languages.
343342
// When the langs argument is omitted, the output of GetLanguages is used.
344343
func IsTranslated(str string, langs ...string) bool {
345-
return IsTranslatedND(GetDomain(), str, 0, langs...)
344+
return IsTranslatedND(GetDomain(), str, 1, langs...)
346345
}
347346

348347
// IsTranslatedN reports whether a plural string is translated in given languages.
@@ -354,7 +353,7 @@ func IsTranslatedN(str string, n int, langs ...string) bool {
354353
// IsTranslatedD reports whether a domain string is translated in given languages.
355354
// When the langs argument is omitted, the output of GetLanguages is used.
356355
func IsTranslatedD(dom, str string, langs ...string) bool {
357-
return IsTranslatedND(dom, str, 0, langs...)
356+
return IsTranslatedND(dom, str, 1, langs...)
358357
}
359358

360359
// IsTranslatedND reports whether a plural domain string is translated in any of given languages.
@@ -385,7 +384,7 @@ func IsTranslatedND(dom, str string, n int, langs ...string) bool {
385384
// IsTranslatedC reports whether a context string is translated in given languages.
386385
// When the langs argument is omitted, the output of GetLanguages is used.
387386
func IsTranslatedC(str, ctx string, langs ...string) bool {
388-
return IsTranslatedNDC(GetDomain(), str, 0, ctx, langs...)
387+
return IsTranslatedNDC(GetDomain(), str, 1, ctx, langs...)
389388
}
390389

391390
// IsTranslatedNC reports whether a plural context string is translated in given languages.

0 commit comments

Comments
 (0)