Add variants of Get functions with existence checks

Currently, a translation that doesn't exist just defaults to the
passed message ID.

It can be helpful to be able to catch these missing cases e.g. to
save to a log.

This PR implements variants ending in 'E' like GetE, GetNE.

This wasn't implemented at the package-level scope, because for
quick translations like that the extra flexibility probably isn't
needed.
This commit is contained in:
Ben Sarah Golightly
2020-07-23 00:28:56 +01:00
parent 3bb41424ff
commit 75a1fb9b04
6 changed files with 326 additions and 4 deletions

84
mo.go
View File

@@ -427,6 +427,90 @@ func (mo *Mo) GetNC(str, plural string, n int, ctx string, vars ...interface{})
return Printf(plural, vars...)
}
// GetE retrieves the corresponding Translation for the given string.
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
// The second return value is true iff the string was found.
func (mo *Mo) GetE(str string, vars ...interface{}) (string, bool) {
// Sync read
mo.RLock()
defer mo.RUnlock()
if mo.translations != nil {
if _, ok := mo.translations[str]; ok {
if fmt, ok := mo.translations[str].GetE(); ok {
return Printf(fmt, vars...), true
}
}
}
return "", false
}
// GetNE retrieves the (N)th plural form of Translation for the given string.
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
// The second return value is true iff the string was found.
func (mo *Mo) GetNE(str, plural string, n int, vars ...interface{}) (string, bool) {
// Sync read
mo.RLock()
defer mo.RUnlock()
if mo.translations != nil {
if _, ok := mo.translations[str]; ok {
if fmt, ok := mo.translations[str].GetNE(mo.pluralForm(n)); ok {
return Printf(fmt, vars...), true
}
}
}
return "", false
}
// GetCE retrieves the corresponding Translation for a given string in the given context.
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
// The second return value is true iff the string was found.
func (mo *Mo) GetCE(str, ctx string, vars ...interface{}) (string, bool) {
// Sync read
mo.RLock()
defer mo.RUnlock()
if mo.contexts != nil {
if _, ok := mo.contexts[ctx]; ok {
if mo.contexts[ctx] != nil {
if _, ok := mo.contexts[ctx][str]; ok {
if fmt, ok := mo.contexts[ctx][str].GetE(); ok {
return Printf(fmt, vars...), true
}
}
}
}
}
return "", false
}
// GetNCE retrieves the (N)th plural form of Translation for the given string in the given context.
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
// The second return value is true iff the string was found.
func (mo *Mo) GetNCE(str, plural string, n int, ctx string, vars ...interface{}) (string, bool) {
// Sync read
mo.RLock()
defer mo.RUnlock()
if mo.contexts != nil {
if _, ok := mo.contexts[ctx]; ok {
if mo.contexts[ctx] != nil {
if _, ok := mo.contexts[ctx][str]; ok {
if fmt, ok := mo.contexts[ctx][str].GetNE(mo.pluralForm(n)); ok {
return Printf(fmt, vars...), true
}
}
}
}
}
return "", false
}
// MarshalBinary implements encoding.BinaryMarshaler interface
func (mo *Mo) MarshalBinary() ([]byte, error) {
obj := new(TranslatorEncoding)