github.com/andrewrech/lazygit@v0.8.1/pkg/i18n/i18n.go (about) 1 package i18n 2 3 import ( 4 "github.com/cloudfoundry/jibber_jabber" 5 "github.com/nicksnyder/go-i18n/v2/i18n" 6 "github.com/sirupsen/logrus" 7 "golang.org/x/text/language" 8 ) 9 10 // Teml is short for template used to make the required map[string]interface{} shorter when using gui.Tr.SLocalize and gui.Tr.TemplateLocalize 11 type Teml map[string]interface{} 12 13 // Localizer will translate a message into the user's language 14 type Localizer struct { 15 i18nLocalizer *i18n.Localizer 16 language string 17 Log *logrus.Entry 18 } 19 20 // NewLocalizer creates a new Localizer 21 func NewLocalizer(log *logrus.Entry) *Localizer { 22 userLang := detectLanguage(jibber_jabber.DetectLanguage) 23 24 log.Info("language: " + userLang) 25 26 return setupLocalizer(log, userLang) 27 } 28 29 // Localize handels the translations 30 // expects i18n.LocalizeConfig as input: https://godoc.org/github.com/nicksnyder/go-i18n/v2/i18n#Localizer.MustLocalize 31 // output: translated string 32 func (l *Localizer) Localize(config *i18n.LocalizeConfig) string { 33 return l.i18nLocalizer.MustLocalize(config) 34 } 35 36 // SLocalize (short localize) is for 1 line localizations 37 // ID: The id that is used in the .toml translation files 38 // Other: the default message it needs to return if there is no translation found or the system is english 39 func (l *Localizer) SLocalize(ID string) string { 40 return l.Localize(&i18n.LocalizeConfig{ 41 DefaultMessage: &i18n.Message{ 42 ID: ID, 43 }, 44 }) 45 } 46 47 // TemplateLocalize allows the Other input to be dynamic 48 func (l *Localizer) TemplateLocalize(ID string, TemplateData map[string]interface{}) string { 49 return l.Localize(&i18n.LocalizeConfig{ 50 DefaultMessage: &i18n.Message{ 51 ID: ID, 52 }, 53 TemplateData: TemplateData, 54 }) 55 } 56 57 // GetLanguage returns the currently selected language, e.g 'en' 58 func (l *Localizer) GetLanguage() string { 59 return l.language 60 } 61 62 // add translation file(s) 63 func addBundles(log *logrus.Entry, i18nBundle *i18n.Bundle) { 64 fs := []func(*i18n.Bundle) error{ 65 addPolish, 66 addDutch, 67 addEnglish, 68 } 69 70 for _, f := range fs { 71 if err := f(i18nBundle); err != nil { 72 log.Fatal(err) 73 74 } 75 } 76 } 77 78 // detectLanguage extracts user language from environment 79 func detectLanguage(langDetector func() (string, error)) string { 80 if userLang, err := langDetector(); err == nil { 81 return userLang 82 } 83 84 return "C" 85 } 86 87 // setupLocalizer creates a new localizer using given userLang 88 func setupLocalizer(log *logrus.Entry, userLang string) *Localizer { 89 // create a i18n bundle that can be used to add translations and other things 90 i18nBundle := &i18n.Bundle{DefaultLanguage: language.English} 91 92 addBundles(log, i18nBundle) 93 94 // return the new localizer that can be used to translate text 95 i18nLocalizer := i18n.NewLocalizer(i18nBundle, userLang) 96 97 return &Localizer{ 98 i18nLocalizer: i18nLocalizer, 99 language: userLang, 100 Log: log, 101 } 102 }