Merge pull request #47 from razor-1/getall

Add GetTranslations methods to Locale and Domain
This commit is contained in:
Leonel Quinteros 2020-10-29 10:51:54 -03:00 committed by GitHub
commit 1b8a993ae4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 98 additions and 1 deletions

View file

@ -401,6 +401,31 @@ func (do *Domain) GetNC(str, plural string, n int, ctx string, vars ...interface
return Printf(plural, vars...)
}
//GetTranslations returns a copy of every translation in the domain. It does not support contexts.
func (do *Domain) GetTranslations() map[string]*Translation {
all := make(map[string]*Translation, len(do.translations))
do.trMutex.RLock()
defer do.trMutex.RUnlock()
for msgID, trans := range do.translations {
newTrans := NewTranslation()
newTrans.ID = trans.ID
newTrans.PluralID = trans.PluralID
newTrans.dirty = trans.dirty
if len(trans.Refs) > 0 {
newTrans.Refs = make([]string, len(trans.Refs))
copy(newTrans.Refs, trans.Refs)
}
for k, v := range trans.Trs {
newTrans.Trs[k] = v
}
all[msgID] = newTrans
}
return all
}
type SourceReference struct {
path string
line int

View file

@ -2,6 +2,10 @@ package gotext
import "testing"
const (
enUSFixture = "fixtures/en_US/default.po"
)
//since both Po and Mo just pass-through to Domain for MarshalBinary and UnmarshalBinary, test it here
func TestBinaryEncoding(t *testing.T) {
// Create po objects
@ -9,7 +13,7 @@ func TestBinaryEncoding(t *testing.T) {
po2 := NewPo()
// Parse file
po.ParseFile("fixtures/en_US/default.po")
po.ParseFile(enUSFixture)
buff, err := po.GetDomain().MarshalBinary()
if err != nil {
@ -32,3 +36,36 @@ func TestBinaryEncoding(t *testing.T) {
t.Errorf("Expected 'en_US' but got '%s'", tr)
}
}
func TestDomain_GetTranslations(t *testing.T) {
po := NewPo()
po.ParseFile(enUSFixture)
domain := po.GetDomain()
all := domain.GetTranslations()
if len(all) != len(domain.translations) {
t.Error("lengths should match")
}
for k, v := range domain.translations {
if all[k] == v {
t.Error("GetTranslations should be returning a copy, but pointers are equal")
}
if all[k].ID != v.ID {
t.Error("IDs should match")
}
if all[k].PluralID != v.PluralID {
t.Error("PluralIDs should match")
}
if all[k].dirty != v.dirty {
t.Error("dirty flag should match")
}
if len(all[k].Trs) != len(v.Trs) {
t.Errorf("Trs length does not match: %d != %d", len(all[k].Trs), len(v.Trs))
}
if len(all[k].Refs) != len(v.Refs) {
t.Errorf("Refs length does not match: %d != %d", len(all[k].Refs), len(v.Refs))
}
}
}

View file

@ -271,6 +271,21 @@ func (l *Locale) GetNDC(dom, str, plural string, n int, ctx string, vars ...inte
return Printf(plural, vars...)
}
//GetTranslations returns a copy of all translations in all domains of this locale. It does not support contexts.
func (l *Locale) GetTranslations() map[string]*Translation {
all := make(map[string]*Translation)
l.RLock()
defer l.RUnlock()
for _, translator := range l.Domains {
for msgID, trans := range translator.GetDomain().GetTranslations() {
all[msgID] = trans
}
}
return all
}
// LocaleEncoding is used as intermediary storage to encode Locale objects to Gob.
type LocaleEncoding struct {
Path string

View file

@ -601,3 +601,23 @@ func TestLocaleBinaryEncoding(t *testing.T) {
t.Errorf("'%s' is different from '%s", l.GetN("One with var: %s", "Several with vars: %s", 3, "VALUE"), l2.GetN("One with var: %s", "Several with vars: %s", 3, "VALUE"))
}
}
func TestLocale_GetTranslations(t *testing.T) {
l := NewLocale("fixtures/", "en_US")
l.AddDomain("default")
all := l.GetTranslations()
if len(all) < 5 {
t.Errorf("length of all translations is too few: %d", len(all))
}
const moreMsgID = "More"
more, ok := all[moreMsgID]
if !ok {
t.Error("missing expected translation")
}
if more.Get() != l.Get(moreMsgID) {
t.Errorf("translations of msgid %s do not match: \"%s\" != \"%s\"", moreMsgID, more.Get(), l.Get(moreMsgID))
}
}