Skip to content

Commit 7d86bb6

Browse files
Unify fmt.Sprintf behaviour on Po and Locale
1 parent 50cdb4e commit 7d86bb6

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

gotext.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ For quick/simple translations you can use the package level functions directly.
2222
*/
2323
package gotext
2424

25+
import "fmt"
26+
2527
// Global environment variables
2628
var (
2729
// Default domain to look at when no domain is specified. Used by package level functions.
@@ -152,3 +154,12 @@ func GetNDC(dom, str, plural string, n int, ctx string, vars ...interface{}) str
152154
// Return translation
153155
return storage.GetNDC(dom, str, plural, n, ctx, vars...)
154156
}
157+
158+
// printf applies text formatting only when needed to parse variables.
159+
func printf(str string, vars ...interface{}) string {
160+
if len(vars) > 0 {
161+
return fmt.Sprintf(str, vars...)
162+
}
163+
164+
return str
165+
}

locale.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package gotext
22

33
import (
4-
"fmt"
54
"os"
65
"path"
76
"sync"
@@ -138,7 +137,7 @@ func (l *Locale) GetND(dom, str, plural string, n int, vars ...interface{}) stri
138137
}
139138

140139
// Return the same we received by default
141-
return fmt.Sprintf(plural, vars...)
140+
return printf(plural, vars...)
142141
}
143142

144143
// GetC uses a domain "default" to return the corresponding translation of the given string in the given context.
@@ -175,5 +174,5 @@ func (l *Locale) GetNDC(dom, str, plural string, n int, ctx string, vars ...inte
175174
}
176175

177176
// Return the same we received by default
178-
return fmt.Sprintf(plural, vars...)
177+
return printf(plural, vars...)
179178
}

po.go

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package gotext
22

33
import (
44
"bufio"
5-
"fmt"
6-
"github.com/mattn/kinako/vm"
75
"io/ioutil"
86
"net/textproto"
97
"os"
108
"strconv"
119
"strings"
1210
"sync"
11+
12+
"github.com/mattn/kinako/vm"
1313
)
1414

1515
type translation struct {
@@ -427,12 +427,12 @@ func (po *Po) Get(str string, vars ...interface{}) string {
427427

428428
if po.translations != nil {
429429
if _, ok := po.translations[str]; ok {
430-
return po.printf(po.translations[str].get(), vars...)
430+
return printf(po.translations[str].get(), vars...)
431431
}
432432
}
433433

434434
// Return the same we received by default
435-
return po.printf(str, vars...)
435+
return printf(str, vars...)
436436
}
437437

438438
// GetN retrieves the (N)th plural form of translation for the given string.
@@ -444,14 +444,14 @@ func (po *Po) GetN(str, plural string, n int, vars ...interface{}) string {
444444

445445
if po.translations != nil {
446446
if _, ok := po.translations[str]; ok {
447-
return po.printf(po.translations[str].getN(po.pluralForm(n)), vars...)
447+
return printf(po.translations[str].getN(po.pluralForm(n)), vars...)
448448
}
449449
}
450450

451451
if n == 1 {
452-
return po.printf(str, vars...)
452+
return printf(str, vars...)
453453
}
454-
return po.printf(plural, vars...)
454+
return printf(plural, vars...)
455455
}
456456

457457
// GetC retrieves the corresponding translation for a given string in the given context.
@@ -465,14 +465,14 @@ func (po *Po) GetC(str, ctx string, vars ...interface{}) string {
465465
if _, ok := po.contexts[ctx]; ok {
466466
if po.contexts[ctx] != nil {
467467
if _, ok := po.contexts[ctx][str]; ok {
468-
return po.printf(po.contexts[ctx][str].get(), vars...)
468+
return printf(po.contexts[ctx][str].get(), vars...)
469469
}
470470
}
471471
}
472472
}
473473

474474
// Return the string we received by default
475-
return po.printf(str, vars...)
475+
return printf(str, vars...)
476476
}
477477

478478
// GetNC retrieves the (N)th plural form of translation for the given string in the given context.
@@ -486,23 +486,14 @@ func (po *Po) GetNC(str, plural string, n int, ctx string, vars ...interface{})
486486
if _, ok := po.contexts[ctx]; ok {
487487
if po.contexts[ctx] != nil {
488488
if _, ok := po.contexts[ctx][str]; ok {
489-
return po.printf(po.contexts[ctx][str].getN(po.pluralForm(n)), vars...)
489+
return printf(po.contexts[ctx][str].getN(po.pluralForm(n)), vars...)
490490
}
491491
}
492492
}
493493
}
494494

495495
if n == 1 {
496-
return po.printf(str, vars...)
496+
return printf(str, vars...)
497497
}
498-
return po.printf(plural, vars...)
499-
}
500-
501-
// printf applies text formatting only when needed to parse variables.
502-
func (po *Po) printf(str string, vars ...interface{}) string {
503-
if len(vars) > 0 {
504-
return fmt.Sprintf(str, vars...)
505-
}
506-
507-
return str
498+
return printf(plural, vars...)
508499
}

0 commit comments

Comments
 (0)