1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-11-30 09:16:47 +02:00
lazygit/pkg/i18n/i18n.go

85 lines
2.3 KiB
Go
Raw Normal View History

package i18n
import (
"github.com/Sirupsen/logrus"
"github.com/cloudfoundry/jibber_jabber"
"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{}
// 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-14 12:56:11 +02:00
// detect the user's language
userLang, err := jibber_jabber.DetectLanguage()
if err != nil {
return nil, err
}
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
i18nBundle := &i18n.Bundle{DefaultLanguage: language.English}
2018-08-13 20:46:53 +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
i18nLocalizer := i18n.NewLocalizer(i18nBundle, userLang)
localizer := &Localizer{
i18nLocalizer: i18nLocalizer,
language: userLang,
Log: log,
}
return localizer, nil
}
// Localize handels the translations
// expects i18n.LocalizeConfig as input: https://godoc.org/github.com/nicksnyder/go-i18n/v2/i18n#Localizer.MustLocalize
// output: translated string
func (l *Localizer) Localize(config *i18n.LocalizeConfig) string {
return l.i18nLocalizer.MustLocalize(config)
}
// SLocalize (short localize) is for 1 line localizations
// 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
func (l *Localizer) SLocalize(ID string) string {
return l.Localize(&i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: ID,
},
})
}
2018-08-14 21:06:50 +02:00
// TemplateLocalize allows the Other input to be dynamic
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{
ID: ID,
2018-08-14 21:06:50 +02:00
},
TemplateData: TemplateData,
})
}
// 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)
addEnglish(i18nBundle)
}