Skip to content

Commit

Permalink
Merge pull request #47 from razor-1/getall
Browse files Browse the repository at this point in the history
Add GetTranslations methods to Locale and Domain
  • Loading branch information
leonelquinteros committed Oct 29, 2020
2 parents 327b393 + a1f059f commit 1b8a993
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
25 changes: 25 additions & 0 deletions domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 38 additions & 1 deletion domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ 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
po := NewPo()
po2 := NewPo()

// Parse file
po.ParseFile("fixtures/en_US/default.po")
po.ParseFile(enUSFixture)

buff, err := po.GetDomain().MarshalBinary()
if err != nil {
Expand All @@ -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))
}
}
}
15 changes: 15 additions & 0 deletions locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions locale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

0 comments on commit 1b8a993

Please sign in to comment.