1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-04 10:34:55 +02:00
lazygit/pkg/i18n/i18n.go

66 lines
1.7 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" {
language := detectLanguage(jibber_jabber.DetectLanguage)
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{
"pl": polishTranslationSet(),
"nl": dutchTranslationSet(),
"en": EnglishTranslationSet(),
2021-05-19 11:55:26 +02:00
"zh": chineseTranslationSet(),
2022-05-04 18:00:36 +02:00
"ja": japaneseTranslationSet(),
2022-06-07 16:31:56 +02:00
"ko": koreanTranslationSet(),
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"
}