Esempio n. 1
0
func newPoTranslator(name string, data []byte) (*translator, error) {
	var (
		f   *po.File
		err error
	)
	if len(data) != 0 {
		f, err = po.LoadData(data)
	} else {
		f, err = po.Load(name)
	}
	if err != nil {
		return nil, err
	}
	var tr = &translator{
		MessageMap: make(map[string]mo.Message),
	}
	for _, v := range f.Messages {
		tr.MessageMap[tr.makeMapKey(v.MsgContext, v.MsgId)] = mo.Message{
			MsgContext:   v.MsgContext,
			MsgId:        v.MsgId,
			MsgIdPlural:  v.MsgIdPlural,
			MsgStr:       v.MsgStr,
			MsgStrPlural: v.MsgStrPlural,
		}
	}
	if lang := f.MimeHeader.Language; lang != "" {
		tr.PluralFormula = plural.Formula(lang)
	} else {
		tr.PluralFormula = plural.Formula("??")
	}
	return tr, nil
}
Esempio n. 2
0
// Copyright 2013 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gettext

import (
	"github.com/hnakamur/gettext-go/gettext/mo"
	"github.com/hnakamur/gettext-go/gettext/plural"
	"github.com/hnakamur/gettext-go/gettext/po"
)

var nilTranslator = &translator{
	MessageMap:    make(map[string]mo.Message),
	PluralFormula: plural.Formula("??"),
}

type translator struct {
	MessageMap    map[string]mo.Message
	PluralFormula func(n int) int
}

func newMoTranslator(name string, data []byte) (*translator, error) {
	var (
		f   *mo.File
		err error
	)
	if len(data) != 0 {
		f, err = mo.LoadData(data)
	} else {
		f, err = mo.Load(name)