2018-08-14 14:12:07 +02:00
|
|
|
package i18n
|
2018-08-13 15:33:45 +02:00
|
|
|
|
|
|
|
import (
|
2018-08-14 14:12:07 +02:00
|
|
|
"github.com/Sirupsen/logrus"
|
2018-08-14 12:52:26 +02:00
|
|
|
"github.com/cloudfoundry/jibber_jabber"
|
2018-08-13 15:33:45 +02:00
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
|
|
|
"golang.org/x/text/language"
|
|
|
|
)
|
|
|
|
|
2018-08-14 14:12:07 +02:00
|
|
|
// Localizer will translate a message into the user's language
|
|
|
|
type Localizer struct {
|
|
|
|
i18nLocalizer *i18n.Localizer
|
|
|
|
language string
|
|
|
|
Log *logrus.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLocalizer creates a new Localizer
|
|
|
|
func NewLocalizer(log *logrus.Logger) (*Localizer, error) {
|
2018-08-13 15:33:45 +02:00
|
|
|
|
2018-08-14 12:56:11 +02:00
|
|
|
// detect the user's language
|
2018-08-14 15:26:25 +02:00
|
|
|
userLang, err := jibber_jabber.DetectLanguage()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-14 14:12:07 +02:00
|
|
|
log.Info("language: " + userLang)
|
2018-08-14 12:56:11 +02:00
|
|
|
|
|
|
|
// create a i18n bundle that can be used to add translations and other things
|
2018-08-14 14:12:07 +02:00
|
|
|
i18nBundle := &i18n.Bundle{DefaultLanguage: language.English}
|
2018-08-13 20:46:53 +02:00
|
|
|
|
2018-08-14 14:12:07 +02:00
|
|
|
addBundles(i18nBundle)
|
2018-08-13 20:46:53 +02:00
|
|
|
|
2018-08-14 12:56:11 +02:00
|
|
|
// return the new localizer that can be used to translate text
|
2018-08-14 14:12:07 +02:00
|
|
|
i18nLocalizer := i18n.NewLocalizer(i18nBundle, userLang)
|
2018-08-13 15:33:45 +02:00
|
|
|
|
2018-08-14 14:12:07 +02:00
|
|
|
localizer := &Localizer{
|
|
|
|
i18nLocalizer: i18nLocalizer,
|
|
|
|
language: userLang,
|
|
|
|
Log: log,
|
|
|
|
}
|
|
|
|
|
|
|
|
return localizer, nil
|
|
|
|
}
|
2018-08-13 15:33:45 +02:00
|
|
|
|
2018-08-14 11:27:46 +02:00
|
|
|
// Localize handels the translations
|
2018-08-13 15:33:45 +02:00
|
|
|
// expects i18n.LocalizeConfig as input: https://godoc.org/github.com/nicksnyder/go-i18n/v2/i18n#Localizer.MustLocalize
|
|
|
|
// output: translated string
|
2018-08-14 14:12:07 +02:00
|
|
|
func (l *Localizer) Localize(config *i18n.LocalizeConfig) string {
|
|
|
|
return l.i18nLocalizer.MustLocalize(config)
|
2018-08-13 15:33:45 +02:00
|
|
|
}
|
|
|
|
|
2018-08-14 11:27:46 +02:00
|
|
|
// SLocalize (short localize) is for 1 line localizations
|
2018-08-13 15:33:45 +02:00
|
|
|
// ID: The id that is used in the .toml translation files
|
|
|
|
// Other: the default message it needs to return if there is no translation found or the system is english
|
2018-08-14 14:12:07 +02:00
|
|
|
func (l *Localizer) SLocalize(ID string, Other string) string {
|
|
|
|
return l.Localize(&i18n.LocalizeConfig{
|
2018-08-13 15:33:45 +02:00
|
|
|
DefaultMessage: &i18n.Message{
|
|
|
|
ID: ID,
|
|
|
|
Other: Other,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2018-08-14 14:12:07 +02:00
|
|
|
|
|
|
|
// GetLanguage returns the currently selected language, e.g 'en'
|
|
|
|
func (l *Localizer) GetLanguage() string {
|
|
|
|
return l.language
|
|
|
|
}
|
|
|
|
|
|
|
|
// add translation file(s)
|
|
|
|
func addBundles(i18nBundle *i18n.Bundle) {
|
|
|
|
addDutch(i18nBundle)
|
|
|
|
}
|