Unify fmt.Sprintf behaviour on Po and Locale
This commit is contained in:
11
gotext.go
11
gotext.go
@@ -22,6 +22,8 @@ For quick/simple translations you can use the package level functions directly.
|
|||||||
*/
|
*/
|
||||||
package gotext
|
package gotext
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
// Global environment variables
|
// Global environment variables
|
||||||
var (
|
var (
|
||||||
// Default domain to look at when no domain is specified. Used by package level functions.
|
// 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 translation
|
||||||
return storage.GetNDC(dom, str, plural, n, ctx, vars...)
|
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
|
package gotext
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"sync"
|
"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 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.
|
// 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 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 (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
|
||||||
"github.com/mattn/kinako/vm"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/textproto"
|
"net/textproto"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/mattn/kinako/vm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type translation struct {
|
type translation struct {
|
||||||
@@ -427,12 +427,12 @@ func (po *Po) Get(str string, vars ...interface{}) string {
|
|||||||
|
|
||||||
if po.translations != nil {
|
if po.translations != nil {
|
||||||
if _, ok := po.translations[str]; ok {
|
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 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.
|
// 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 po.translations != nil {
|
||||||
if _, ok := po.translations[str]; ok {
|
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 {
|
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.
|
// 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 _, ok := po.contexts[ctx]; ok {
|
||||||
if po.contexts[ctx] != nil {
|
if po.contexts[ctx] != nil {
|
||||||
if _, ok := po.contexts[ctx][str]; ok {
|
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 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.
|
// 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 _, ok := po.contexts[ctx]; ok {
|
||||||
if po.contexts[ctx] != nil {
|
if po.contexts[ctx] != nil {
|
||||||
if _, ok := po.contexts[ctx][str]; ok {
|
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 {
|
if n == 1 {
|
||||||
return po.printf(str, vars...)
|
return printf(str, vars...)
|
||||||
}
|
}
|
||||||
return po.printf(plural, vars...)
|
return 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
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user