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-16 13:35:04 +02:00
|
|
|
// Teml is short for template used to make the required map[string]interface{} shorter when using gui.Tr.SLocalize and gui.Tr.TemplateLocalize
|
|
|
|
type Teml map[string]interface{}
|
|
|
|
|
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-18 14:03:20 +02:00
|
|
|
addBundles(log, 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-16 07:16:32 +02:00
|
|
|
func (l *Localizer) SLocalize(ID string) string {
|
2018-08-14 14:12:07 +02:00
|
|
|
return l.Localize(&i18n.LocalizeConfig{
|
2018-08-13 15:33:45 +02:00
|
|
|
DefaultMessage: &i18n.Message{
|
2018-08-16 07:16:32 +02:00
|
|
|
ID: ID,
|
2018-08-13 15:33:45 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2018-08-14 14:12:07 +02:00
|
|
|
|
2018-08-14 21:06:50 +02:00
|
|
|
// TemplateLocalize allows the Other input to be dynamic
|
2018-08-16 07:16:32 +02:00
|
|
|
func (l *Localizer) TemplateLocalize(ID string, TemplateData map[string]interface{}) string {
|
2018-08-14 21:06:50 +02:00
|
|
|
return l.Localize(&i18n.LocalizeConfig{
|
|
|
|
DefaultMessage: &i18n.Message{
|
2018-08-16 07:16:32 +02:00
|
|
|
ID: ID,
|
2018-08-14 21:06:50 +02:00
|
|
|
},
|
|
|
|
TemplateData: TemplateData,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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)
|
2018-08-18 14:03:20 +02:00
|
|
|
func addBundles(log *logrus.Logger, i18nBundle *i18n.Bundle) {
|
2018-08-17 17:22:30 +02:00
|
|
|
err := addPolish(i18nBundle)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = addDutch(i18nBundle)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = addEnglish(i18nBundle)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-08-14 14:12:07 +02:00
|
|
|
}
|