diff --git a/pkg/app/app.go b/pkg/app/app.go index d558ed250..f07757f98 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -9,6 +9,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/gui" + "github.com/mjarkk/lazygit/pkg/i18n" ) // App struct @@ -20,6 +21,7 @@ type App struct { OSCommand *commands.OSCommand GitCommand *commands.GitCommand Gui *gui.Gui + Tr *i18n.Localizer } func newLogger(config config.AppConfigurer) *logrus.Logger { @@ -48,11 +50,17 @@ func NewApp(config config.AppConfigurer) (*App, error) { if err != nil { return nil, err } + + app.Tr, err = i18n.NewLocalizer() + if err != nil { + return nil, err + } + app.GitCommand, err = commands.NewGitCommand(app.Log, app.OSCommand) if err != nil { return nil, err } - app.Gui, err = gui.NewGui(app.Log, app.GitCommand, app.OSCommand, config.GetVersion()) + app.Gui, err = gui.NewGui(app.Log, app.GitCommand, app.OSCommand, app.Tr, config.GetVersion()) if err != nil { return nil, err } diff --git a/pkg/gui/commit_message_panel.go b/pkg/gui/commit_message_panel.go index f765ab308..854732817 100644 --- a/pkg/gui/commit_message_panel.go +++ b/pkg/gui/commit_message_panel.go @@ -1,6 +1,8 @@ package gui -import "github.com/jesseduffield/gocui" +import ( + "github.com/jesseduffield/gocui" +) func (gui *Gui) handleCommitConfirm(g *gocui.Gui, v *gocui.View) error { message := gui.trimmedContent(v) diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go index ba60a3278..b99de1606 100644 --- a/pkg/gui/files_panel.go +++ b/pkg/gui/files_panel.go @@ -1,7 +1,6 @@ package gui import ( - "github.com/jesseduffield/lazygit/pkg/i18n" // "io" // "io/ioutil" @@ -148,27 +147,27 @@ func (gui *Gui) handleIgnoreFile(g *gocui.Gui, v *gocui.View) error { func (gui *Gui) renderfilesOptions(g *gocui.Gui, file *commands.File) error { optionsMap := map[string]string{ - "← → ↑ ↓": lang.SLocalize("navigate", "navigate"), - "S": lang.SLocalize("stashFiles", "stash files"), - "c": lang.SLocalize("CommitChanges", "commit changes"), - "o": lang.SLocalize("open", "open"), - "i": lang.SLocalize("ignore", "ignore"), - "d": lang.SLocalize("delete", "delete"), - "space": lang.SLocalize("toggleStaged", "toggle staged"), - "R": lang.SLocalize("refresh", "refresh"), - "t": lang.SLocalize("addPatch", "add patch"), - "e": lang.SLocalize("edit", "edit"), - "PgUp/PgDn": lang.SLocalize("scroll", "scroll"), + "← → ↑ ↓": gui.Tr.SLocalize("navigate", "navigate"), + "S": gui.Tr.SLocalize("stashFiles", "stash files"), + "c": gui.Tr.SLocalize("CommitChanges", "commit changes"), + "o": gui.Tr.SLocalize("open", "open"), + "i": gui.Tr.SLocalize("ignore", "ignore"), + "d": gui.Tr.SLocalize("delete", "delete"), + "space": gui.Tr.SLocalize("toggleStaged", "toggle staged"), + "R": gui.Tr.SLocalize("refresh", "refresh"), + "t": gui.Tr.SLocalize("addPatch", "add patch"), + "e": gui.Tr.SLocalize("edit", "edit"), + "PgUp/PgDn": gui.Tr.SLocalize("scroll", "scroll"), } if gui.State.HasMergeConflicts { - optionsMap["a"] = lang.SLocalize("abortMerge", "abort merge") - optionsMap["m"] = lang.SLocalize("resolveMergeConflicts", "resolve merge conflicts") + optionsMap["a"] = gui.Tr.SLocalize("abortMerge", "abort merge") + optionsMap["m"] = gui.Tr.SLocalize("resolveMergeConflicts", "resolve merge conflicts") } if file == nil { return gui.renderOptionsMap(g, optionsMap) } if file.Tracked { - optionsMap["d"] = lang.SLocalize("checkout", "checkout") + optionsMap["d"] = gui.Tr.SLocalize("checkout", "checkout") } return gui.renderOptionsMap(g, optionsMap) } diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index afe17ff79..bf8360f72 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -19,7 +19,7 @@ import ( "github.com/golang-collections/collections/stack" "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands" - "github.com/jesseduffield/lazygit/pkg/i18n" + "github.com/mjarkk/lazygit/pkg/i18n" ) // OverlappingEdges determines if panel edges overlap @@ -40,6 +40,7 @@ type Gui struct { Version string SubProcess *exec.Cmd State guiState + Tr *i18n.Localizer } type guiState struct { @@ -58,7 +59,7 @@ type guiState struct { } // NewGui builds a new gui handler -func NewGui(log *logrus.Logger, gitCommand *commands.GitCommand, oSCommand *commands.OSCommand, version string) (*Gui, error) { +func NewGui(log *logrus.Logger, gitCommand *commands.GitCommand, oSCommand *commands.OSCommand, tr *lang.Localizer, version string) (*Gui, error) { initialState := guiState{ Files: make([]commands.File, 0), PreviousView: "files", @@ -78,6 +79,7 @@ func NewGui(log *logrus.Logger, gitCommand *commands.GitCommand, oSCommand *comm OSCommand: oSCommand, Version: version, State: initialState, + Tr: tr, }, nil } @@ -134,7 +136,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { if err != gocui.ErrUnknownView { return err } - v.Title = lang.SLocalize("NotEnoughSpace", "Not enough space to render panels") + v.Title = gui.Tr.SLocalize("NotEnoughSpace", "Not enough space to render panels") v.Wrap = true } return nil @@ -153,7 +155,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { if err != gocui.ErrUnknownView { return err } - v.Title = lang.SLocalize("DiffTitle", "Diff") + v.Title = gui.Tr.SLocalize("DiffTitle", "Diff") v.Wrap = true v.FgColor = gocui.ColorWhite } @@ -162,7 +164,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { if err != gocui.ErrUnknownView { return err } - v.Title = lang.SLocalize("StatusTitle", "Status") + v.Title = gui.Tr.SLocalize("StatusTitle", "Status") v.FgColor = gocui.ColorWhite } @@ -172,7 +174,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { return err } filesView.Highlight = true - filesView.Title = lang.SLocalize("FilesTitle", "Files") + filesView.Title = gui.Tr.SLocalize("FilesTitle", "Files") v.FgColor = gocui.ColorWhite } @@ -180,7 +182,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { if err != gocui.ErrUnknownView { return err } - v.Title = lang.SLocalize("BranchesTitle", "Branches") + v.Title = gui.Tr.SLocalize("BranchesTitle", "Branches") v.FgColor = gocui.ColorWhite } @@ -188,7 +190,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { if err != gocui.ErrUnknownView { return err } - v.Title = lang.SLocalize("CommitsTitle", "Commits") + v.Title = gui.Tr.SLocalize("CommitsTitle", "Commits") v.FgColor = gocui.ColorWhite } @@ -196,7 +198,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { if err != gocui.ErrUnknownView { return err } - v.Title = lang.SLocalize("StashTitle", "Stash") + v.Title = gui.Tr.SLocalize("StashTitle", "Stash") v.FgColor = gocui.ColorWhite } @@ -215,7 +217,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { return err } g.SetViewOnBottom("commitMessage") - commitMessageView.Title = lang.SLocalize("CommitMessage", "Commit message") + commitMessageView.Title = gui.Tr.SLocalize("CommitMessage", "Commit message") commitMessageView.FgColor = gocui.ColorWhite commitMessageView.Editable = true } diff --git a/pkg/i18n/dutch.go b/pkg/i18n/dutch.go index 12cbc8f43..199b238b3 100644 --- a/pkg/i18n/dutch.go +++ b/pkg/i18n/dutch.go @@ -1,4 +1,4 @@ -package lang +package i18n import ( "github.com/nicksnyder/go-i18n/v2/i18n" diff --git a/pkg/i18n/i18n.go b/pkg/i18n/i18n.go index a62f50c1e..e58c05aec 100644 --- a/pkg/i18n/i18n.go +++ b/pkg/i18n/i18n.go @@ -1,45 +1,68 @@ -package lang +package i18n import ( + "github.com/Sirupsen/logrus" "github.com/cloudfoundry/jibber_jabber" "github.com/nicksnyder/go-i18n/v2/i18n" "golang.org/x/text/language" ) -// the function to setup the localizer -func getlocalizer() *i18n.Localizer { +// Localizer will translate a message into the user's language +type Localizer struct { + i18nLocalizer *i18n.Localizer + language string + Log *logrus.Logger +} + +// NewLocalizer creates a new Localizer +func NewLocalizer(log *logrus.Logger) (*Localizer, error) { // detect the user's language userLang, _ := jibber_jabber.DetectLanguage() + log.Info("language: " + userLang) // create a i18n bundle that can be used to add translations and other things - var i18nObject = &i18n.Bundle{DefaultLanguage: language.English} + i18nBundle := &i18n.Bundle{DefaultLanguage: language.English} - // add translation file(s) - i18nObject = addDutch(i18nObject) + addBundles(i18nBundle) // return the new localizer that can be used to translate text - return i18n.NewLocalizer(i18nObject, userLang) -} + i18nLocalizer := i18n.NewLocalizer(i18nBundle, userLang) -// setup the localizer for later use -var localizer = getlocalizer() + localizer := &Localizer{ + i18nLocalizer: i18nLocalizer, + language: userLang, + Log: log, + } + + return localizer, nil +} // Localize handels the translations // expects i18n.LocalizeConfig as input: https://godoc.org/github.com/nicksnyder/go-i18n/v2/i18n#Localizer.MustLocalize // output: translated string -func Localize(config *i18n.LocalizeConfig) string { - return localizer.MustLocalize(config) +func (l *Localizer) Localize(config *i18n.LocalizeConfig) string { + return l.i18nLocalizer.MustLocalize(config) } // SLocalize (short localize) is for 1 line localizations // ID: The id that is used in the .toml translation files // Other: the default message it needs to return if there is no translation found or the system is english -func SLocalize(ID string, Other string) string { - return Localize(&i18n.LocalizeConfig{ +func (l *Localizer) SLocalize(ID string, Other string) string { + return l.Localize(&i18n.LocalizeConfig{ DefaultMessage: &i18n.Message{ ID: ID, Other: Other, }, }) } + +// GetLanguage returns the currently selected language, e.g 'en' +func (l *Localizer) GetLanguage() string { + return l.language +} + +// add translation file(s) +func addBundles(i18nBundle *i18n.Bundle) { + addDutch(i18nBundle) +}