Browse Source

Update commits

Signed-off-by: Josef Fröhle <froehle@b1-systems.de>
tags/v1.4.0
8 changed files with 57 additions and 57 deletions
  1. +5
    -4
      helper.go
  2. +4
    -4
      locale.go
  3. +20
    -18
      mo.go
  4. +13
    -15
      plurals/compiler.go
  5. +4
    -5
      plurals/expression.go
  6. +5
    -10
      plurals/tests.go
  7. +2
    -1
      po.go
  8. +4
    -0
      translation.go

+ 5
- 4
helper.go View File

@@ -11,8 +11,9 @@ import (
"strings"
)

var re = regexp.MustCompile(`%\(([a-zA-Z0-9_]+)\)[.0-9]*[xsvTtbcdoqXxUeEfFgGp]`)
var re = regexp.MustCompile(`%\(([a-zA-Z0-9_]+)\)[.0-9]*[svTtbcdoqXxUeEfFgGp]`)

// SimplifiedLocale simplified locale like " en_US"/"de_DE "/en_US.UTF-8/zh_CN/zh_TW/el_GR@euro/... to en_US, de_DE, zh_CN, el_GR...
func SimplifiedLocale(lang string) string {
// en_US/en_US.UTF-8/zh_CN/zh_TW/el_GR@euro/...
if idx := strings.Index(lang, ":"); idx != -1 {
@@ -27,7 +28,7 @@ func SimplifiedLocale(lang string) string {
return strings.TrimSpace(lang)
}

// printf applies text formatting only when needed to parse variables.
// 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...)
@@ -36,8 +37,8 @@ func Printf(str string, vars ...interface{}) string {
return str
}


// NPrintf support named format
// NPrintf("%(name)s is Type %(type)s", map[string]interface{}{"name": "Gotext", "type": "struct"})
func NPrintf(format string, params map[string]interface{}) {
f, p := parseSprintf(format, params)
fmt.Printf(f, p...)
@@ -82,4 +83,4 @@ func reformatSprintf(f string) (string, []string) {
}

return out, ord
}
}

+ 4
- 4
locale.go View File

@@ -152,13 +152,13 @@ func (l *Locale) SetDomain(dom string) {
// Get uses a domain "default" to return the corresponding Translation of a given string.
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (l *Locale) Get(str string, vars ...interface{}) string {
return l.GetD(l.defaultDomain, str, vars...)
return l.GetD(l.GetDomain(), str, vars...)
}

// GetN retrieves the (N)th plural form of Translation for the given string in the "default" domain.
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (l *Locale) GetN(str, plural string, n int, vars ...interface{}) string {
return l.GetND(l.defaultDomain, str, plural, n, vars...)
return l.GetND(l.GetDomain(), str, plural, n, vars...)
}

// GetD returns the corresponding Translation in the given domain for the given string.
@@ -189,13 +189,13 @@ func (l *Locale) GetND(dom, str, plural string, n int, vars ...interface{}) stri
// GetC uses a domain "default" to return the corresponding Translation of the given string in the given context.
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (l *Locale) GetC(str, ctx string, vars ...interface{}) string {
return l.GetDC(l.defaultDomain, str, ctx, vars...)
return l.GetDC(l.GetDomain(), str, ctx, vars...)
}

// GetNC retrieves the (N)th plural form of Translation for the given string in the given context in the "default" domain.
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (l *Locale) GetNC(str, plural string, n int, ctx string, vars ...interface{}) string {
return l.GetNDC(l.defaultDomain, str, plural, n, ctx, vars...)
return l.GetNDC(l.GetDomain(), str, plural, n, ctx, vars...)
}

// GetDC returns the corresponding Translation in the given domain for the given string in the given context.


+ 20
- 18
mo.go View File

@@ -20,12 +20,13 @@ import (
)

const (
MoMagicLittleEndian = 0x950412de
MoMagicBigEndian = 0xde120495
MoMagicLittleEndian = 0x950412de // MoMagicLittleEndian encoding
MoMagicBigEndian = 0xde120495 // MoMagicBigEndian encoding

EotSeparator = "\x04" // msgctxt and msgid separator
NulSeparator = "\x00" // msgid and msgstr separator
)

/*
Mo parses the content of any MO file and provides all the Translation functions needed.
It's the base object used by all package methods.
@@ -77,6 +78,7 @@ type Mo struct {
ctxBuffer string
}

// NewMoTranslator creates a new Mo object with the Translator interface
func NewMoTranslator() Translator {
return new(Mo)
}
@@ -135,8 +137,8 @@ func (mo *Mo) Parse(buf []byte) {
var header struct {
MajorVersion uint16
MinorVersion uint16
MsgIdCount uint32
MsgIdOffset uint32
MsgIDCount uint32
MsgIDOffset uint32
MsgStrOffset uint32
HashSize uint32
HashOffset uint32
@@ -154,30 +156,30 @@ func (mo *Mo) Parse(buf []byte) {
// return fmt.Errorf("gettext: %v", "invalid version number")
}

msgIdStart := make([]uint32, header.MsgIdCount)
msgIdLen := make([]uint32, header.MsgIdCount)
if _, err := r.Seek(int64(header.MsgIdOffset), 0); err != nil {
msgIDStart := make([]uint32, header.MsgIDCount)
msgIDLen := make([]uint32, header.MsgIDCount)
if _, err := r.Seek(int64(header.MsgIDOffset), 0); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
}
for i := 0; i < int(header.MsgIdCount); i++ {
if err := binary.Read(r, bo, &msgIdLen[i]); err != nil {
for i := 0; i < int(header.MsgIDCount); i++ {
if err := binary.Read(r, bo, &msgIDLen[i]); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
}
if err := binary.Read(r, bo, &msgIdStart[i]); err != nil {
if err := binary.Read(r, bo, &msgIDStart[i]); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
}
}

msgStrStart := make([]int32, header.MsgIdCount)
msgStrLen := make([]int32, header.MsgIdCount)
msgStrStart := make([]int32, header.MsgIDCount)
msgStrLen := make([]int32, header.MsgIDCount)
if _, err := r.Seek(int64(header.MsgStrOffset), 0); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
}
for i := 0; i < int(header.MsgIdCount); i++ {
for i := 0; i < int(header.MsgIDCount); i++ {
if err := binary.Read(r, bo, &msgStrLen[i]); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
@@ -188,12 +190,12 @@ func (mo *Mo) Parse(buf []byte) {
}
}

for i := 0; i < int(header.MsgIdCount); i++ {
if _, err := r.Seek(int64(msgIdStart[i]), 0); err != nil {
for i := 0; i < int(header.MsgIDCount); i++ {
if _, err := r.Seek(int64(msgIDStart[i]), 0); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
}
msgIdData := make([]byte, msgIdLen[i])
msgIdData := make([]byte, msgIDLen[i])
if _, err := r.Read(msgIdData); err != nil {
return
// return fmt.Errorf("gettext: %v", err)
@@ -333,9 +335,9 @@ func (mo *Mo) pluralForm(n int) int {
/* Use the Germanic plural rule. */
if n == 1 {
return 0
} else {
return 1
}
return 1

}
return mo.pluralforms.Eval(uint32(n))
}


+ 13
- 15
plurals/compiler.go View File

@@ -2,6 +2,10 @@
* Copyright (c) 2018 DeineAgentur UG https://www.deineagentur.com. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for full license information.
*/

/*
Package plurals is the pluralform compiler to get the correct translation id of the plural string
*/
package plurals

import (
@@ -19,10 +23,6 @@ type match struct {

var pat = regexp.MustCompile(`(\?|:|\|\||&&|==|!=|>=|>|<=|<|%|\d+|n)`)

type exprToken interface {
compile(tokens []string) (expr Expression, err error)
}

type testToken interface {
compile(tokens []string) (test test, err error)
}
@@ -47,18 +47,18 @@ func (ternaryStruct) compile(tokens []string) (expr Expression, err error) {
if err != nil {
return expr, err
}
true_action, err := compileExpression(strings.Join(actions.Left, ""))
trueAction, err := compileExpression(strings.Join(actions.Left, ""))
if err != nil {
return expr, err
}
false_action, err := compileExpression(strings.Join(actions.Right, ""))
falseAction, err := compileExpression(strings.Join(actions.Right, ""))
if err != nil {
return expr, nil
}
return ternary{
test: test,
trueExpr: true_action,
falseExpr: false_action,
trueExpr: trueAction,
falseExpr: falseAction,
}, nil
}

@@ -180,9 +180,9 @@ func compileEquality(tokens []string, sep string, builder cmpTestBuilder) (test
return builder(i, true), nil
} else if contains(split.Left, "n") && contains(split.Left, "%") {
return subPipe(split.Left, split.Right, builder, false)
} else {
return test, errors.New("equality test must have 'n' as one of the two tests")
}
return test, errors.New("equality test must have 'n' as one of the two tests")

}

var eqToken eqStruct
@@ -325,7 +325,7 @@ func scan(s string) <-chan match {
}

// Split the string into tokens
func split(s string) <- chan string {
func split(s string) <-chan string {
ch := make(chan string)
go func() {
s = strings.Replace(s, " ", "", -1)
@@ -405,9 +405,8 @@ func compileExpression(s string) (expr Expression, err error) {
tokens := tokenize(s)
if contains(tokens, "?") {
return ternaryToken.compile(tokens)
} else {
return constToken.compile(tokens)
}
return constToken.compile(tokens)
}

// Compiles a test (comparison)
@@ -425,7 +424,6 @@ func parseUint32(s string) (ui uint32, err error) {
i, err := strconv.ParseUint(s, 10, 32)
if err != nil {
return ui, err
} else {
return uint32(i), nil
}
return uint32(i), nil
}

+ 4
- 5
plurals/expression.go View File

@@ -35,10 +35,9 @@ func (t ternary) Eval(n uint32) int {
return -1
}
return t.trueExpr.Eval(n)
} else {
if t.falseExpr == nil {
return -1
}
return t.falseExpr.Eval(n)
}
if t.falseExpr == nil {
return -1
}
return t.falseExpr.Eval(n)
}

+ 5
- 10
plurals/tests.go View File

@@ -42,9 +42,8 @@ type lt struct {
func (e lt) test(n uint32) bool {
if e.flipped {
return e.value < n
} else {
return n < e.value
}
return n < e.value
}

type gte struct {
@@ -55,9 +54,8 @@ type gte struct {
func (e gte) test(n uint32) bool {
if e.flipped {
return e.value >= n
} else {
return n >= e.value
}
return n >= e.value
}

type lte struct {
@@ -68,9 +66,8 @@ type lte struct {
func (e lte) test(n uint32) bool {
if e.flipped {
return e.value <= n
} else {
return n <= e.value
}
return n <= e.value
}

type and struct {
@@ -81,9 +78,8 @@ type and struct {
func (e and) test(n uint32) bool {
if !e.left.test(n) {
return false
} else {
return e.right.test(n)
}
return e.right.test(n)
}

type or struct {
@@ -94,9 +90,8 @@ type or struct {
func (e or) test(n uint32) bool {
if e.left.test(n) {
return true
} else {
return e.right.test(n)
}
return e.right.test(n)
}

type pipe struct {


+ 2
- 1
po.go View File

@@ -71,13 +71,14 @@ type Po struct {
type parseState int

const (
head parseState = iota
head parseState = iota
msgCtxt
msgID
msgIDPlural
msgStr
)

// NewPoTranslator creates a new Po object with the Translator interface
func NewPoTranslator() Translator {
return new(Po)
}


+ 4
- 0
translation.go View File

@@ -5,12 +5,14 @@

package gotext

// Translation is the struct for the Translations parsed via Po or Mo files and all coming parsers
type Translation struct {
ID string
PluralID string
Trs map[int]string
}

// NewTranslation returns the Translation object and initialized it.
func NewTranslation() *Translation {
tr := new(Translation)
tr.Trs = make(map[int]string)
@@ -18,6 +20,7 @@ func NewTranslation() *Translation {
return tr
}

// Get returns the string of the translation
func (t *Translation) Get() string {
// Look for Translation index 0
if _, ok := t.Trs[0]; ok {
@@ -30,6 +33,7 @@ func (t *Translation) Get() string {
return t.ID
}

// Get returns the string of the plural translation
func (t *Translation) GetN(n int) string {
// Look for Translation index
if _, ok := t.Trs[n]; ok {


Loading…
Cancel
Save