allow user to configure the gui language

This commit is contained in:
Dwarven YANG 2021-08-30 09:54:55 +08:00
parent c88fba546f
commit 43f83f64ec
5 changed files with 30 additions and 8 deletions

View file

@ -16,6 +16,7 @@ Changes to the user config will only take place after closing and re-opening laz
```yml ```yml
gui: gui:
scrollHeight: 2 scrollHeight: 2
language: 'auto' # one of 'auto' | 'en' | 'pl' | 'nl' | 'de' | 'tr'
theme: theme:
activeBorderColor: activeBorderColor:
- green - green

View file

@ -34,7 +34,10 @@ func NewApp(config *config.AppConfig) (*App, error) {
} }
var err error var err error
app.Log = log.NewLogger(config, "23432119147a4367abf7c0de2aa99a2d") app.Log = log.NewLogger(config, "23432119147a4367abf7c0de2aa99a2d")
app.Tr = i18n.NewTranslationSet(app.Log) app.Tr, err = i18n.NewTranslationSetFromConfig(app.Log, config.UserConfig.Gui.Language)
if err != nil {
return app, err
}
app.OSCommand = commands.NewOSCommand(app.Log, config) app.OSCommand = commands.NewOSCommand(app.Log, config)
// here is the place to make use of the docker-compose.yml file in the current directory // here is the place to make use of the docker-compose.yml file in the current directory

View file

@ -42,10 +42,11 @@ func NewDummyDockerCommand() *DockerCommand {
// NewDummyDockerCommandWithOSCommand creates a new dummy DockerCommand for testing // NewDummyDockerCommandWithOSCommand creates a new dummy DockerCommand for testing
func NewDummyDockerCommandWithOSCommand(osCommand *OSCommand) *DockerCommand { func NewDummyDockerCommandWithOSCommand(osCommand *OSCommand) *DockerCommand {
newAppConfig := NewDummyAppConfig()
return &DockerCommand{ return &DockerCommand{
Log: NewDummyLog(), Log: NewDummyLog(),
OSCommand: osCommand, OSCommand: osCommand,
Tr: i18n.NewTranslationSet(NewDummyLog()), Tr: i18n.NewTranslationSet(NewDummyLog(), newAppConfig.UserConfig.Gui.Language),
Config: NewDummyAppConfig(), Config: newAppConfig,
} }
} }

View file

@ -79,6 +79,9 @@ type GuiConfig struct {
// scrolling the main panel // scrolling the main panel
ScrollHeight int `yaml:"scrollHeight,omitempty"` ScrollHeight int `yaml:"scrollHeight,omitempty"`
// Language determines which language the GUI displayed.
Language string `yaml:"language,omitempty"`
// ScrollPastBottom determines whether you can scroll past the bottom of the // ScrollPastBottom determines whether you can scroll past the bottom of the
// main view // main view
ScrollPastBottom bool `yaml:"scrollPastBottom,omitempty"` ScrollPastBottom bool `yaml:"scrollPastBottom,omitempty"`
@ -311,6 +314,7 @@ func GetDefaultConfig() UserConfig {
return UserConfig{ return UserConfig{
Gui: GuiConfig{ Gui: GuiConfig{
ScrollHeight: 2, ScrollHeight: 2,
Language: "auto",
ScrollPastBottom: false, ScrollPastBottom: false,
IgnoreMouseEvents: false, IgnoreMouseEvents: false,
Theme: ThemeConfig{ Theme: ThemeConfig{

View file

@ -6,6 +6,7 @@ import (
"github.com/imdario/mergo" "github.com/imdario/mergo"
"github.com/cloudfoundry/jibber_jabber" "github.com/cloudfoundry/jibber_jabber"
"github.com/go-errors/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -16,16 +17,28 @@ type Localizer struct {
S TranslationSet S TranslationSet
} }
// NewTranslationSet creates a new Localizer func NewTranslationSetFromConfig(log *logrus.Entry, configLanguage string) (*TranslationSet, error) {
func NewTranslationSet(log *logrus.Entry) *TranslationSet { if configLanguage == "auto" {
userLang := detectLanguage(jibber_jabber.DetectLanguage) 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 := englishSet() baseSet := englishSet()
for languageCode, translationSet := range GetTranslationSets() { for languageCode, translationSet := range GetTranslationSets() {
if strings.HasPrefix(userLang, languageCode) { if strings.HasPrefix(language, languageCode) {
_ = mergo.Merge(&baseSet, translationSet, mergo.WithOverride) _ = mergo.Merge(&baseSet, translationSet, mergo.WithOverride)
} }
} }