1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/i18n/i18n.go

68 lines
1.8 KiB
Go
Raw Normal View History

package i18n
import (
2020-10-04 02:00:48 +02:00
"strings"
2020-09-30 13:12:03 +02:00
"github.com/cloudfoundry/jibber_jabber"
"github.com/go-errors/errors"
2020-10-04 02:00:48 +02:00
"github.com/imdario/mergo"
2018-08-26 07:46:18 +02:00
"github.com/sirupsen/logrus"
)
// Localizer will translate a message into the user's language
type Localizer struct {
2020-10-04 02:00:48 +02:00
Log *logrus.Entry
S TranslationSet
}
func NewTranslationSetFromConfig(log *logrus.Entry, configLanguage string) (*TranslationSet, error) {
if configLanguage == "auto" {
2023-05-30 01:27:20 +02:00
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)
2018-08-14 12:56:11 +02:00
baseSet := EnglishTranslationSet()
2020-10-04 02:00:48 +02:00
for languageCode, translationSet := range GetTranslationSets() {
if strings.HasPrefix(language, languageCode) {
2020-10-04 02:00:48 +02:00
_ = mergo.Merge(&baseSet, translationSet, mergo.WithOverride)
}
}
2020-10-04 02:00:48 +02:00
return &baseSet
}
2018-08-20 21:18:15 +02:00
2020-10-04 02:00:48 +02:00
// GetTranslationSets gets all the translation sets, keyed by language code
func GetTranslationSets() map[string]TranslationSet {
return map[string]TranslationSet{
2023-05-30 01:27:20 +02:00
"pl": polishTranslationSet(),
"nl": dutchTranslationSet(),
"en": EnglishTranslationSet(),
"zh-CN": chineseTranslationSet(), // Simplified Chinese
"zh-TW": traditionalChineseTranslationSet(),
"ja": japaneseTranslationSet(),
"ko": koreanTranslationSet(),
2023-05-19 14:45:51 +02:00
"ru": RussianTranslationSet(),
2018-08-20 21:18:15 +02:00
}
}
// detectLanguage extracts user language from environment
func detectLanguage(langDetector func() (string, error)) string {
if userLang, err := langDetector(); err == nil {
return userLang
}
return "C"
}