1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-02 09:21:40 +02:00
lazygit/pkg/i18n/i18n.go
2023-06-28 00:34:59 +06:00

68 lines
1.8 KiB
Go

package i18n
import (
"strings"
"github.com/cloudfoundry/jibber_jabber"
"github.com/go-errors/errors"
"github.com/imdario/mergo"
"github.com/sirupsen/logrus"
)
// Localizer will translate a message into the user's language
type Localizer struct {
Log *logrus.Entry
S TranslationSet
}
func NewTranslationSetFromConfig(log *logrus.Entry, configLanguage string) (*TranslationSet, error) {
if configLanguage == "auto" {
language := detectLanguage(jibber_jabber.DetectIETF)
return NewTranslationSet(log, language), nil
}
for key := range GetTranslationSets() {
if key == configLanguage {
return NewTranslationSet(log, configLanguage), nil
}
}
return NewTranslationSet(log, "en"), errors.New("Language not found: " + configLanguage)
}
func NewTranslationSet(log *logrus.Entry, language string) *TranslationSet {
log.Info("language: " + language)
baseSet := EnglishTranslationSet()
for languageCode, translationSet := range GetTranslationSets() {
if strings.HasPrefix(language, languageCode) {
_ = mergo.Merge(&baseSet, translationSet, mergo.WithOverride)
}
}
return &baseSet
}
// GetTranslationSets gets all the translation sets, keyed by language code
func GetTranslationSets() map[string]TranslationSet {
return map[string]TranslationSet{
"pl": polishTranslationSet(),
"nl": dutchTranslationSet(),
"en": EnglishTranslationSet(),
"zh-CN": chineseTranslationSet(), // Simplified Chinese
"zh-TW": traditionalChineseTranslationSet(),
"ja": japaneseTranslationSet(),
"ko": koreanTranslationSet(),
"ru": RussianTranslationSet(),
}
}
// detectLanguage extracts user language from environment
func detectLanguage(langDetector func() (string, error)) string {
if userLang, err := langDetector(); err == nil {
return userLang
}
return "C"
}