Unify fmt.Sprintf behaviour on Po and Locale
This commit is contained in:
parent
50cdb4e058
commit
7d86bb66fe
3 changed files with 25 additions and 24 deletions
11
gotext.go
11
gotext.go
|
@ -22,6 +22,8 @@ For quick/simple translations you can use the package level functions directly.
|
|||
*/
|
||||
package gotext
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Global environment variables
|
||||
var (
|
||||
// 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
|
|||
// Return translation
|
||||
return storage.GetNDC(dom, str, plural, n, ctx, vars...)
|
||||
}
|
||||
|
||||
// printf applies text formatting only when needed to parse variables.
|
||||
func printf(str string, vars ...interface{}) string {
|
||||
if len(vars) > 0 {
|
||||
return fmt.Sprintf(str, vars...)
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package gotext
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"sync"
|
||||
|
@ -138,7 +137,7 @@ func (l *Locale) GetND(dom, str, plural string, n int, vars ...interface{}) stri
|
|||
}
|
||||
|
||||
// Return the same we received by default
|
||||
return fmt.Sprintf(plural, vars...)
|
||||
return printf(plural, vars...)
|
||||
}
|
||||
|
||||
// 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
|
|||
}
|
||||
|
||||
// Return the same we received by default
|
||||
return fmt.Sprintf(plural, vars...)
|
||||
return printf(plural, vars...)
|
||||
}
|
||||
|
|
33
po.go
33
po.go
|
@ -2,14 +2,14 @@ package gotext
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"github.com/mattn/kinako/vm"
|
||||
"io/ioutil"
|
||||
"net/textproto"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/mattn/kinako/vm"
|
||||
)
|
||||
|
||||
type translation struct {
|
||||
|
@ -427,12 +427,12 @@ func (po *Po) Get(str string, vars ...interface{}) string {
|
|||
|
||||
if po.translations != nil {
|
||||
if _, ok := po.translations[str]; ok {
|
||||
return po.printf(po.translations[str].get(), vars...)
|
||||
return printf(po.translations[str].get(), vars...)
|
||||
}
|
||||
}
|
||||
|
||||
// Return the same we received by default
|
||||
return po.printf(str, vars...)
|
||||
return printf(str, vars...)
|
||||
}
|
||||
|
||||
// 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 {
|
|||
|
||||
if po.translations != nil {
|
||||
if _, ok := po.translations[str]; ok {
|
||||
return po.printf(po.translations[str].getN(po.pluralForm(n)), vars...)
|
||||
return printf(po.translations[str].getN(po.pluralForm(n)), vars...)
|
||||
}
|
||||
}
|
||||
|
||||
if n == 1 {
|
||||
return po.printf(str, vars...)
|
||||
return printf(str, vars...)
|
||||
}
|
||||
return po.printf(plural, vars...)
|
||||
return printf(plural, vars...)
|
||||
}
|
||||
|
||||
// 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 {
|
|||
if _, ok := po.contexts[ctx]; ok {
|
||||
if po.contexts[ctx] != nil {
|
||||
if _, ok := po.contexts[ctx][str]; ok {
|
||||
return po.printf(po.contexts[ctx][str].get(), vars...)
|
||||
return printf(po.contexts[ctx][str].get(), vars...)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return the string we received by default
|
||||
return po.printf(str, vars...)
|
||||
return printf(str, vars...)
|
||||
}
|
||||
|
||||
// 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{})
|
|||
if _, ok := po.contexts[ctx]; ok {
|
||||
if po.contexts[ctx] != nil {
|
||||
if _, ok := po.contexts[ctx][str]; ok {
|
||||
return po.printf(po.contexts[ctx][str].getN(po.pluralForm(n)), vars...)
|
||||
return printf(po.contexts[ctx][str].getN(po.pluralForm(n)), vars...)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if n == 1 {
|
||||
return po.printf(str, vars...)
|
||||
return printf(str, vars...)
|
||||
}
|
||||
return po.printf(plural, vars...)
|
||||
}
|
||||
|
||||
// printf applies text formatting only when needed to parse variables.
|
||||
func (po *Po) printf(str string, vars ...interface{}) string {
|
||||
if len(vars) > 0 {
|
||||
return fmt.Sprintf(str, vars...)
|
||||
}
|
||||
|
||||
return str
|
||||
return printf(plural, vars...)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue