Rename contexts to contextTranslations

This will keep the names consistent across the three members
This commit is contained in:
Matyas Horky 2023-04-04 16:28:57 +02:00
parent 71a59c05b2
commit 4ce6b50c65
4 changed files with 30 additions and 30 deletions

View file

@ -35,9 +35,9 @@ type Domain struct {
pluralforms plurals.Expression
// Storage
translations map[string]*Translation
contexts map[string]map[string]*Translation
pluralTranslations map[string]*Translation
translations map[string]*Translation
contextTranslations map[string]map[string]*Translation
pluralTranslations map[string]*Translation
// Sync Mutex
trMutex sync.RWMutex
@ -84,7 +84,7 @@ func NewDomain() *Domain {
domain.Headers = make(HeaderMap)
domain.headerComments = make([]string, 0)
domain.translations = make(map[string]*Translation)
domain.contexts = make(map[string]map[string]*Translation)
domain.contextTranslations = make(map[string]map[string]*Translation)
domain.pluralTranslations = make(map[string]*Translation)
return domain
@ -183,14 +183,14 @@ func (do *Domain) DropStaleTranslations() {
defer do.trMutex.Unlock()
defer do.pluralMutex.Unlock()
for name, ctx := range do.contexts {
for name, ctx := range do.contextTranslations {
for id, trans := range ctx {
if trans.IsStale() {
delete(ctx, id)
}
}
if len(ctx) == 0 {
delete(do.contexts, name)
delete(do.contextTranslations, name)
}
}
@ -312,7 +312,7 @@ func (do *Domain) SetC(id, ctx, str string) {
defer do.trMutex.Unlock()
defer do.pluralMutex.Unlock()
if context, ok := do.contexts[ctx]; ok {
if context, ok := do.contextTranslations[ctx]; ok {
if trans, hasTrans := context[id]; hasTrans {
trans.Set(str)
} else {
@ -325,7 +325,7 @@ func (do *Domain) SetC(id, ctx, str string) {
trans := NewTranslation()
trans.ID = id
trans.Set(str)
do.contexts[ctx] = map[string]*Translation{
do.contextTranslations[ctx] = map[string]*Translation{
id: trans,
}
}
@ -337,11 +337,11 @@ func (do *Domain) GetC(str, ctx string, vars ...interface{}) string {
do.trMutex.RLock()
defer do.trMutex.RUnlock()
if do.contexts != nil {
if _, ok := do.contexts[ctx]; ok {
if do.contexts[ctx] != nil {
if _, ok := do.contexts[ctx][str]; ok {
return Printf(do.contexts[ctx][str].Get(), vars...)
if do.contextTranslations != nil {
if _, ok := do.contextTranslations[ctx]; ok {
if do.contextTranslations[ctx] != nil {
if _, ok := do.contextTranslations[ctx][str]; ok {
return Printf(do.contextTranslations[ctx][str].Get(), vars...)
}
}
}
@ -361,7 +361,7 @@ func (do *Domain) SetNC(id, plural, ctx string, n int, str string) {
defer do.trMutex.Unlock()
defer do.pluralMutex.Unlock()
if context, ok := do.contexts[ctx]; ok {
if context, ok := do.contextTranslations[ctx]; ok {
if trans, hasTrans := context[id]; hasTrans {
trans.SetN(pluralForm, str)
} else {
@ -374,7 +374,7 @@ func (do *Domain) SetNC(id, plural, ctx string, n int, str string) {
trans := NewTranslation()
trans.ID = id
trans.SetN(pluralForm, str)
do.contexts[ctx] = map[string]*Translation{
do.contextTranslations[ctx] = map[string]*Translation{
id: trans,
}
}
@ -386,11 +386,11 @@ func (do *Domain) GetNC(str, plural string, n int, ctx string, vars ...interface
do.trMutex.RLock()
defer do.trMutex.RUnlock()
if do.contexts != nil {
if _, ok := do.contexts[ctx]; ok {
if do.contexts[ctx] != nil {
if _, ok := do.contexts[ctx][str]; ok {
return Printf(do.contexts[ctx][str].GetN(do.pluralForm(n)), vars...)
if do.contextTranslations != nil {
if _, ok := do.contextTranslations[ctx]; ok {
if do.contextTranslations[ctx] != nil {
if _, ok := do.contextTranslations[ctx][str]; ok {
return Printf(do.contextTranslations[ctx][str].GetN(do.pluralForm(n)), vars...)
}
}
}
@ -511,7 +511,7 @@ func (do *Domain) MarshalText() ([]byte, error) {
// Just as with headers, output translations in consistent order (to minimise diffs between round-trips), with (first) source reference taking priority, followed by context and finally ID
references := make([]SourceReference, 0)
for name, ctx := range do.contexts {
for name, ctx := range do.contextTranslations {
for id, trans := range ctx {
if id == "" {
continue
@ -623,7 +623,7 @@ func (do *Domain) MarshalBinary() ([]byte, error) {
obj.Nplurals = do.nplurals
obj.Plural = do.plural
obj.Translations = do.translations
obj.Contexts = do.contexts
obj.Contexts = do.contextTranslations
var buff bytes.Buffer
encoder := gob.NewEncoder(&buff)
@ -649,7 +649,7 @@ func (do *Domain) UnmarshalBinary(data []byte) error {
do.nplurals = obj.Nplurals
do.plural = obj.Plural
do.translations = obj.Translations
do.contexts = obj.Contexts
do.contextTranslations = obj.Contexts
if expr, err := plurals.Compile(do.plural); err == nil {
do.pluralforms = expr

6
mo.go
View file

@ -263,10 +263,10 @@ func (mo *Mo) addTranslation(msgid, msgstr []byte) {
if len(msgctxt) > 0 {
// With context...
if _, ok := mo.domain.contexts[string(msgctxt)]; !ok {
mo.domain.contexts[string(msgctxt)] = make(map[string]*Translation)
if _, ok := mo.domain.contextTranslations[string(msgctxt)]; !ok {
mo.domain.contextTranslations[string(msgctxt)] = make(map[string]*Translation)
}
mo.domain.contexts[string(msgctxt)][translation.ID] = translation
mo.domain.contextTranslations[string(msgctxt)][translation.ID] = translation
} else {
mo.domain.translations[translation.ID] = translation
}

6
po.go
View file

@ -223,10 +223,10 @@ func (po *Po) saveBuffer() {
po.domain.translations[po.domain.trBuffer.ID] = po.domain.trBuffer
} else {
// With context...
if _, ok := po.domain.contexts[po.domain.ctxBuffer]; !ok {
po.domain.contexts[po.domain.ctxBuffer] = make(map[string]*Translation)
if _, ok := po.domain.contextTranslations[po.domain.ctxBuffer]; !ok {
po.domain.contextTranslations[po.domain.ctxBuffer] = make(map[string]*Translation)
}
po.domain.contexts[po.domain.ctxBuffer][po.domain.trBuffer.ID] = po.domain.trBuffer
po.domain.contextTranslations[po.domain.ctxBuffer][po.domain.trBuffer.ID] = po.domain.trBuffer
// Cleanup current context buffer if needed
if po.domain.trBuffer.ID != "" {

View file

@ -61,7 +61,7 @@ func (te *TranslatorEncoding) GetTranslator() Translator {
po.domain.nplurals = te.Nplurals
po.domain.plural = te.Plural
po.domain.translations = te.Translations
po.domain.contexts = te.Contexts
po.domain.contextTranslations = te.Contexts
return po
}