2017-11-20 00:10:04 -05:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
package locale // import "miniflux.app/locale"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
2017-11-25 18:56:44 -05:00
|
|
|
// Language represents a language in the system.
|
2017-11-20 00:10:04 -05:00
|
|
|
type Language struct {
|
|
|
|
language string
|
|
|
|
translations Translation
|
|
|
|
}
|
|
|
|
|
2017-11-25 18:56:44 -05:00
|
|
|
// Get fetch the translation for the given key.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (l *Language) Get(key string, args ...interface{}) string {
|
|
|
|
var translation string
|
|
|
|
|
|
|
|
str, found := l.translations[key]
|
|
|
|
if !found {
|
|
|
|
translation = key
|
|
|
|
} else {
|
|
|
|
translation = str.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(translation, args...)
|
|
|
|
}
|
|
|
|
|
2017-11-25 18:56:44 -05:00
|
|
|
// Plural returns the translation of the given key by using the language plural form.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (l *Language) Plural(key string, n int, args ...interface{}) string {
|
|
|
|
translation := key
|
|
|
|
slices, found := l.translations[key]
|
|
|
|
|
2017-11-25 18:56:44 -05:00
|
|
|
if found {
|
2017-11-20 00:10:04 -05:00
|
|
|
pluralForm, found := pluralForms[l.language]
|
|
|
|
if !found {
|
|
|
|
pluralForm = pluralForms["default"]
|
|
|
|
}
|
|
|
|
|
|
|
|
index := pluralForm(n)
|
|
|
|
translations := slices.([]interface{})
|
|
|
|
translation = key
|
|
|
|
|
|
|
|
if len(translations) > index {
|
|
|
|
translation = translations[index].(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(translation, args...)
|
|
|
|
}
|