diff --git a/gotext.go b/gotext.go index d6dc200..213dba6 100644 --- a/gotext.go +++ b/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 +} diff --git a/locale.go b/locale.go index 66015f4..0123686 100644 --- a/locale.go +++ b/locale.go @@ -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...) } diff --git a/po.go b/po.go index 858119a..a3fbb42 100644 --- a/po.go +++ b/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...) }