diff options
author | Dwarven YANG <prison.yang@gmail.com> | 2021-08-27 15:00:53 +0800 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2021-08-30 09:12:29 +1000 |
commit | 63072af5bc4f93207b4ff1f49c45772a828fa13f (patch) | |
tree | 5a519f346585a0f2fb3095e31fe2d20253da5629 | |
parent | 44d08edfb02686c3dd22429356a9e4b327191f91 (diff) |
allow user to configure the gui language
-rw-r--r-- | docs/Config.md | 1 | ||||
-rw-r--r-- | pkg/app/app.go | 5 | ||||
-rw-r--r-- | pkg/commands/dummies.go | 5 | ||||
-rw-r--r-- | pkg/commands/git_test.go | 3 | ||||
-rw-r--r-- | pkg/commands/loading_commits_test.go | 2 | ||||
-rw-r--r-- | pkg/config/user_config.go | 2 | ||||
-rw-r--r-- | pkg/gui/dummies.go | 6 | ||||
-rw-r--r-- | pkg/i18n/i18n.go | 24 |
8 files changed, 35 insertions, 13 deletions
diff --git a/docs/Config.md b/docs/Config.md index 724f35334..920d83ebf 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -22,6 +22,7 @@ gui: sidePanelWidth: 0.3333 # number from 0 to 1 expandFocusedSidePanel: false mainPanelSplitMode: 'flexible' # one of 'horizontal' | 'flexible' | 'vertical' + language: 'auto' # one of 'auto' | 'en' | 'zh' | 'pl' | 'nl' theme: lightTheme: false # For terminals with a light background activeBorderColor: diff --git a/pkg/app/app.go b/pkg/app/app.go index 52a168796..e1d30d841 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -102,7 +102,10 @@ func NewApp(config config.AppConfigurer, filterPath string) (*App, error) { } var err error app.Log = newLogger(config) - app.Tr = i18n.NewTranslationSet(app.Log) + app.Tr, err = i18n.NewTranslationSetFromConfig(app.Log, config.GetUserConfig().Gui.Language) + if err != nil { + return app, err + } // if we are being called in 'demon' mode, we can just return here app.ClientContext = os.Getenv("LAZYGIT_CLIENT_COMMAND") diff --git a/pkg/commands/dummies.go b/pkg/commands/dummies.go index 6feb908e3..b1b6e9441 100644 --- a/pkg/commands/dummies.go +++ b/pkg/commands/dummies.go @@ -14,11 +14,12 @@ func NewDummyGitCommand() *GitCommand { // NewDummyGitCommandWithOSCommand creates a new dummy GitCommand for testing func NewDummyGitCommandWithOSCommand(osCommand *oscommands.OSCommand) *GitCommand { + newAppConfig := config.NewDummyAppConfig() return &GitCommand{ Log: utils.NewDummyLog(), OSCommand: osCommand, - Tr: i18n.NewTranslationSet(utils.NewDummyLog()), - Config: config.NewDummyAppConfig(), + Tr: i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language), + Config: newAppConfig, getGitConfigValue: func(string) (string, error) { return "", nil }, } } diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index e31fe8910..6891fdc53 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -209,7 +209,8 @@ func TestNewGitCommand(t *testing.T) { for _, s := range scenarios { t.Run(s.testName, func(t *testing.T) { s.setup() - s.test(NewGitCommand(utils.NewDummyLog(), oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog()), config.NewDummyAppConfig())) + newAppConfig := config.NewDummyAppConfig() + s.test(NewGitCommand(utils.NewDummyLog(), oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language), newAppConfig)) }) } } diff --git a/pkg/commands/loading_commits_test.go b/pkg/commands/loading_commits_test.go index a637f5bd5..bd35556d4 100644 --- a/pkg/commands/loading_commits_test.go +++ b/pkg/commands/loading_commits_test.go @@ -19,7 +19,7 @@ func NewDummyCommitListBuilder() *CommitListBuilder { Log: utils.NewDummyLog(), GitCommand: NewDummyGitCommandWithOSCommand(osCommand), OSCommand: osCommand, - Tr: i18n.NewTranslationSet(utils.NewDummyLog()), + Tr: i18n.NewTranslationSet(utils.NewDummyLog(), "auto"), } } diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 9d61cb68f..7b5145a47 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -32,6 +32,7 @@ type GuiConfig struct { SidePanelWidth float64 `yaml:"sidePanelWidth"` ExpandFocusedSidePanel bool `yaml:"expandFocusedSidePanel"` MainPanelSplitMode string `yaml:"mainPanelSplitMode"` + Language string `yaml:"language"` Theme ThemeConfig `yaml:"theme"` CommitLength CommitLengthConfig `yaml:"commitLength"` SkipNoStagedFilesWarning bool `yaml:"skipNoStagedFilesWarning"` @@ -310,6 +311,7 @@ func GetDefaultConfig() *UserConfig { SidePanelWidth: 0.3333, ExpandFocusedSidePanel: false, MainPanelSplitMode: "flexible", + Language: "auto", Theme: ThemeConfig{ LightTheme: false, ActiveBorderColor: []string{"green", "bold"}, diff --git a/pkg/gui/dummies.go b/pkg/gui/dummies.go index d8a7cba29..0e37bbbc8 100644 --- a/pkg/gui/dummies.go +++ b/pkg/gui/dummies.go @@ -11,11 +11,13 @@ import ( // NewDummyGui creates a new dummy GUI for testing func NewDummyUpdater() *updates.Updater { - DummyUpdater, _ := updates.NewUpdater(utils.NewDummyLog(), config.NewDummyAppConfig(), oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog())) + newAppConfig := config.NewDummyAppConfig() + DummyUpdater, _ := updates.NewUpdater(utils.NewDummyLog(), newAppConfig, oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language)) return DummyUpdater } func NewDummyGui() *Gui { - DummyGui, _ := NewGui(utils.NewDummyLog(), commands.NewDummyGitCommand(), oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog()), config.NewDummyAppConfig(), NewDummyUpdater(), "", false) + newAppConfig := config.NewDummyAppConfig() + DummyGui, _ := NewGui(utils.NewDummyLog(), commands.NewDummyGitCommand(), oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language), newAppConfig, NewDummyUpdater(), "", false) return DummyGui } diff --git a/pkg/i18n/i18n.go b/pkg/i18n/i18n.go index dece062f4..8f6452c54 100644 --- a/pkg/i18n/i18n.go +++ b/pkg/i18n/i18n.go @@ -4,6 +4,7 @@ import ( "strings" "github.com/cloudfoundry/jibber_jabber" + "github.com/go-errors/errors" "github.com/imdario/mergo" "github.com/sirupsen/logrus" ) @@ -14,20 +15,31 @@ type Localizer struct { S TranslationSet } -// NewTranslationSet creates a new Localizer -func NewTranslationSet(log *logrus.Entry) *TranslationSet { - userLang := detectLanguage(jibber_jabber.DetectLanguage) +func NewTranslationSetFromConfig(log *logrus.Entry, configLanguage string) (*TranslationSet, error) { + if configLanguage == "auto" { + language := detectLanguage(jibber_jabber.DetectLanguage) + return NewTranslationSet(log, language), nil + } - log.Info("language: " + userLang) + 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(userLang, languageCode) { + if strings.HasPrefix(language, languageCode) { _ = mergo.Merge(&baseSet, translationSet, mergo.WithOverride) } } - return &baseSet } |