Skip to content

Commit

Permalink
Unify fmt.Sprintf behaviour on Po and Locale
Browse files Browse the repository at this point in the history
  • Loading branch information
leonelquinteros committed Nov 2, 2017
1 parent 50cdb4e commit 7d86bb6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
11 changes: 11 additions & 0 deletions gotext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
5 changes: 2 additions & 3 deletions locale.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gotext

import (
"fmt"
"os"
"path"
"sync"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 changes: 12 additions & 21 deletions po.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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...)
}

0 comments on commit 7d86bb6

Please sign in to comment.