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:
@@ -50,3 +50,29 @@ func (t *Translation) GetN(n int) string {
|
||||
// Return untranslated plural by default
|
||||
return t.PluralID
|
||||
}
|
||||
|
||||
// Get returns the string of the translation. The second return value is true
|
||||
// iff the string was found.
|
||||
func (t *Translation) GetE() (string, bool) {
|
||||
// Look for Translation index 0
|
||||
if _, ok := t.Trs[0]; ok {
|
||||
if t.Trs[0] != "" {
|
||||
return t.Trs[0], true
|
||||
}
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
|
||||
// GetN returns the string of the plural translation. The second return value
|
||||
// is true iff the string was found.
|
||||
func (t *Translation) GetNE(n int) (string, bool) {
|
||||
// Look for Translation index
|
||||
if _, ok := t.Trs[n]; ok {
|
||||
if t.Trs[n] != "" {
|
||||
return t.Trs[n], true
|
||||
}
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user