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

51 lines
1.2 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"
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
}
2020-10-04 02:00:48 +02:00
// NewTranslationSet creates a new Localizer
func NewTranslationSet(log *logrus.Entry) *TranslationSet {
userLang := detectLanguage(jibber_jabber.DetectLanguage)
log.Info("language: " + userLang)
2018-08-14 12:56:11 +02:00
2020-10-04 02:00:48 +02:00
baseSet := englishTranslationSet()
2020-10-04 02:00:48 +02:00
for languageCode, translationSet := range GetTranslationSets() {
if strings.HasPrefix(userLang, languageCode) {
_ = 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(),
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"
}