Add support for plural forms

This commit is contained in:
Leonel Quinteros
2016-06-23 11:41:38 -03:00
parent a7ccd4bbdb
commit 982d029c78
6 changed files with 178 additions and 29 deletions

126
po.go
View File

@@ -9,10 +9,44 @@ import (
"sync"
)
type translation struct {
id string
pluralId string
trs map[int]string
}
func newTranslation() *translation {
tr := new(translation)
tr.trs = make(map[int]string)
return tr
}
func (t *translation) get() string {
// Look for translation index 0
if _, ok := t.trs[0]; ok {
return t.trs[0]
}
// Return unstranlated id by default
return t.id
}
func (t *translation) getN(n int) string {
// Look for translation index
if _, ok := t.trs[n]; ok {
return t.trs[n]
}
// Return unstranlated plural by default
return t.pluralId
}
// Po type handles the content of any PO file.
type Po struct {
// Storage
translations map[string]string
// Storage
translations map[string]*translation
// Sync Mutex
sync.RWMutex
}
@@ -41,16 +75,16 @@ func (po *Po) ParseFile(f string) {
// Parse loads the translations specified in the provided string (str)
func (po *Po) Parse(str string) {
po.Lock()
po.Lock()
defer po.Unlock()
if po.translations == nil {
po.translations = make(map[string]string)
po.translations = make(map[string]*translation)
}
lines := strings.Split(str, "\n")
var msgid, msgstr string
tr := newTranslation()
for _, l := range lines {
// Trim spaces
@@ -62,35 +96,93 @@ func (po *Po) Parse(str string) {
}
// Skip invalid lines
if !strings.HasPrefix(l, "msgid") && !strings.HasPrefix(l, "msgstr") {
if !strings.HasPrefix(l, "msgid") && !strings.HasPrefix(l, "msgid_plural") && !strings.HasPrefix(l, "msgstr") {
continue
}
// Buffer msgid and continue
if strings.HasPrefix(l, "msgid") {
msgid = strings.TrimSpace(strings.TrimPrefix(l, "msgid"))
msgid, _ = strconv.Unquote(msgid)
if strings.HasPrefix(l, "msgid") && !strings.HasPrefix(l, "msgid_plural") {
// Save current translation buffer.
po.translations[tr.id] = tr
// Flush buffer
tr = newTranslation()
// Set id
tr.id, _ = strconv.Unquote(strings.TrimSpace(strings.TrimPrefix(l, "msgid")))
// Loop
continue
}
// Save translation for buffered msgid
if strings.HasPrefix(l, "msgstr") {
msgstr = strings.TrimSpace(strings.TrimPrefix(l, "msgstr"))
msgstr, _ = strconv.Unquote(msgstr)
// Check for plural form
if strings.HasPrefix(l, "msgid_plural") {
tr.pluralId, _ = strconv.Unquote(strings.TrimSpace(strings.TrimPrefix(l, "msgid_plural")))
po.translations[msgid] = msgstr
// Loop
continue
}
// Save translation
if strings.HasPrefix(l, "msgstr") {
l = strings.TrimSpace(strings.TrimPrefix(l, "msgstr"))
// Check for indexed translation forms
if strings.HasPrefix(l, "[") {
in := strings.Index(l, "]")
if in == -1 {
// Skip wrong index formatting
continue
}
// Parse index
i, err := strconv.Atoi(l[1:in])
if err != nil {
// Skip wrong index formatting
continue
}
// Parse translation string
tr.trs[i], _ = strconv.Unquote(strings.TrimSpace(l[in+1:]))
// Loop
continue
}
// Save single translation form under 0 index
tr.trs[0], _ = strconv.Unquote(l)
}
}
// Save last translation buffer.
if tr.id != "" {
po.translations[tr.id] = tr
}
}
// Get retrieves the corresponding translation for the given string.
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (po *Po) Get(str string, vars ...interface{}) string {
if po.translations != nil {
if _, ok := po.translations[str]; ok {
return fmt.Sprintf(po.translations[str], vars...)
return fmt.Sprintf(po.translations[str].get(), vars...)
}
}
// Return the same we received by default
return fmt.Sprintf(str, vars...)
}
// GetN retrieves the (N)th plural form translation for the given string.
// If n == 0, usually the singular form of the string is returned as defined in the PO file.
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (po *Po) GetN(str, plural string, n int, vars ...interface{}) string {
if po.translations != nil {
if _, ok := po.translations[str]; ok {
return fmt.Sprintf(po.translations[str].getN(n), vars...)
}
}
// Return the plural string we received by default
return fmt.Sprintf(plural, vars...)
}