mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-12 12:55:47 +02:00
make local i18n package confirm to project structure
This commit is contained in:
parent
6e518142b4
commit
5cbacb0c67
6 changed files with 76 additions and 42 deletions
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/jesseduffield/lazygit/pkg/commands"
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
||||||
"github.com/jesseduffield/lazygit/pkg/config"
|
"github.com/jesseduffield/lazygit/pkg/config"
|
||||||
"github.com/jesseduffield/lazygit/pkg/gui"
|
"github.com/jesseduffield/lazygit/pkg/gui"
|
||||||
|
"github.com/mjarkk/lazygit/pkg/i18n"
|
||||||
)
|
)
|
||||||
|
|
||||||
// App struct
|
// App struct
|
||||||
|
@ -20,6 +21,7 @@ type App struct {
|
||||||
OSCommand *commands.OSCommand
|
OSCommand *commands.OSCommand
|
||||||
GitCommand *commands.GitCommand
|
GitCommand *commands.GitCommand
|
||||||
Gui *gui.Gui
|
Gui *gui.Gui
|
||||||
|
Tr *i18n.Localizer
|
||||||
}
|
}
|
||||||
|
|
||||||
func newLogger(config config.AppConfigurer) *logrus.Logger {
|
func newLogger(config config.AppConfigurer) *logrus.Logger {
|
||||||
|
@ -48,11 +50,17 @@ func NewApp(config config.AppConfigurer) (*App, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.Tr, err = i18n.NewLocalizer()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
app.GitCommand, err = commands.NewGitCommand(app.Log, app.OSCommand)
|
app.GitCommand, err = commands.NewGitCommand(app.Log, app.OSCommand)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package gui
|
package gui
|
||||||
|
|
||||||
import "github.com/jesseduffield/gocui"
|
import (
|
||||||
|
"github.com/jesseduffield/gocui"
|
||||||
|
)
|
||||||
|
|
||||||
func (gui *Gui) handleCommitConfirm(g *gocui.Gui, v *gocui.View) error {
|
func (gui *Gui) handleCommitConfirm(g *gocui.Gui, v *gocui.View) error {
|
||||||
message := gui.trimmedContent(v)
|
message := gui.trimmedContent(v)
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package gui
|
package gui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/jesseduffield/lazygit/pkg/i18n"
|
|
||||||
|
|
||||||
// "io"
|
// "io"
|
||||||
// "io/ioutil"
|
// "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 {
|
func (gui *Gui) renderfilesOptions(g *gocui.Gui, file *commands.File) error {
|
||||||
optionsMap := map[string]string{
|
optionsMap := map[string]string{
|
||||||
"← → ↑ ↓": lang.SLocalize("navigate", "navigate"),
|
"← → ↑ ↓": gui.Tr.SLocalize("navigate", "navigate"),
|
||||||
"S": lang.SLocalize("stashFiles", "stash files"),
|
"S": gui.Tr.SLocalize("stashFiles", "stash files"),
|
||||||
"c": lang.SLocalize("CommitChanges", "commit changes"),
|
"c": gui.Tr.SLocalize("CommitChanges", "commit changes"),
|
||||||
"o": lang.SLocalize("open", "open"),
|
"o": gui.Tr.SLocalize("open", "open"),
|
||||||
"i": lang.SLocalize("ignore", "ignore"),
|
"i": gui.Tr.SLocalize("ignore", "ignore"),
|
||||||
"d": lang.SLocalize("delete", "delete"),
|
"d": gui.Tr.SLocalize("delete", "delete"),
|
||||||
"space": lang.SLocalize("toggleStaged", "toggle staged"),
|
"space": gui.Tr.SLocalize("toggleStaged", "toggle staged"),
|
||||||
"R": lang.SLocalize("refresh", "refresh"),
|
"R": gui.Tr.SLocalize("refresh", "refresh"),
|
||||||
"t": lang.SLocalize("addPatch", "add patch"),
|
"t": gui.Tr.SLocalize("addPatch", "add patch"),
|
||||||
"e": lang.SLocalize("edit", "edit"),
|
"e": gui.Tr.SLocalize("edit", "edit"),
|
||||||
"PgUp/PgDn": lang.SLocalize("scroll", "scroll"),
|
"PgUp/PgDn": gui.Tr.SLocalize("scroll", "scroll"),
|
||||||
}
|
}
|
||||||
if gui.State.HasMergeConflicts {
|
if gui.State.HasMergeConflicts {
|
||||||
optionsMap["a"] = lang.SLocalize("abortMerge", "abort merge")
|
optionsMap["a"] = gui.Tr.SLocalize("abortMerge", "abort merge")
|
||||||
optionsMap["m"] = lang.SLocalize("resolveMergeConflicts", "resolve merge conflicts")
|
optionsMap["m"] = gui.Tr.SLocalize("resolveMergeConflicts", "resolve merge conflicts")
|
||||||
}
|
}
|
||||||
if file == nil {
|
if file == nil {
|
||||||
return gui.renderOptionsMap(g, optionsMap)
|
return gui.renderOptionsMap(g, optionsMap)
|
||||||
}
|
}
|
||||||
if file.Tracked {
|
if file.Tracked {
|
||||||
optionsMap["d"] = lang.SLocalize("checkout", "checkout")
|
optionsMap["d"] = gui.Tr.SLocalize("checkout", "checkout")
|
||||||
}
|
}
|
||||||
return gui.renderOptionsMap(g, optionsMap)
|
return gui.renderOptionsMap(g, optionsMap)
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ import (
|
||||||
"github.com/golang-collections/collections/stack"
|
"github.com/golang-collections/collections/stack"
|
||||||
"github.com/jesseduffield/gocui"
|
"github.com/jesseduffield/gocui"
|
||||||
"github.com/jesseduffield/lazygit/pkg/commands"
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
||||||
"github.com/jesseduffield/lazygit/pkg/i18n"
|
"github.com/mjarkk/lazygit/pkg/i18n"
|
||||||
)
|
)
|
||||||
|
|
||||||
// OverlappingEdges determines if panel edges overlap
|
// OverlappingEdges determines if panel edges overlap
|
||||||
|
@ -40,6 +40,7 @@ type Gui struct {
|
||||||
Version string
|
Version string
|
||||||
SubProcess *exec.Cmd
|
SubProcess *exec.Cmd
|
||||||
State guiState
|
State guiState
|
||||||
|
Tr *i18n.Localizer
|
||||||
}
|
}
|
||||||
|
|
||||||
type guiState struct {
|
type guiState struct {
|
||||||
|
@ -58,7 +59,7 @@ type guiState struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGui builds a new gui handler
|
// 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{
|
initialState := guiState{
|
||||||
Files: make([]commands.File, 0),
|
Files: make([]commands.File, 0),
|
||||||
PreviousView: "files",
|
PreviousView: "files",
|
||||||
|
@ -78,6 +79,7 @@ func NewGui(log *logrus.Logger, gitCommand *commands.GitCommand, oSCommand *comm
|
||||||
OSCommand: oSCommand,
|
OSCommand: oSCommand,
|
||||||
Version: version,
|
Version: version,
|
||||||
State: initialState,
|
State: initialState,
|
||||||
|
Tr: tr,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,7 +136,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||||
if err != gocui.ErrUnknownView {
|
if err != gocui.ErrUnknownView {
|
||||||
return err
|
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
|
v.Wrap = true
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -153,7 +155,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||||
if err != gocui.ErrUnknownView {
|
if err != gocui.ErrUnknownView {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
v.Title = lang.SLocalize("DiffTitle", "Diff")
|
v.Title = gui.Tr.SLocalize("DiffTitle", "Diff")
|
||||||
v.Wrap = true
|
v.Wrap = true
|
||||||
v.FgColor = gocui.ColorWhite
|
v.FgColor = gocui.ColorWhite
|
||||||
}
|
}
|
||||||
|
@ -162,7 +164,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||||
if err != gocui.ErrUnknownView {
|
if err != gocui.ErrUnknownView {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
v.Title = lang.SLocalize("StatusTitle", "Status")
|
v.Title = gui.Tr.SLocalize("StatusTitle", "Status")
|
||||||
v.FgColor = gocui.ColorWhite
|
v.FgColor = gocui.ColorWhite
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,7 +174,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
filesView.Highlight = true
|
filesView.Highlight = true
|
||||||
filesView.Title = lang.SLocalize("FilesTitle", "Files")
|
filesView.Title = gui.Tr.SLocalize("FilesTitle", "Files")
|
||||||
v.FgColor = gocui.ColorWhite
|
v.FgColor = gocui.ColorWhite
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,7 +182,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||||
if err != gocui.ErrUnknownView {
|
if err != gocui.ErrUnknownView {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
v.Title = lang.SLocalize("BranchesTitle", "Branches")
|
v.Title = gui.Tr.SLocalize("BranchesTitle", "Branches")
|
||||||
v.FgColor = gocui.ColorWhite
|
v.FgColor = gocui.ColorWhite
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,7 +190,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||||
if err != gocui.ErrUnknownView {
|
if err != gocui.ErrUnknownView {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
v.Title = lang.SLocalize("CommitsTitle", "Commits")
|
v.Title = gui.Tr.SLocalize("CommitsTitle", "Commits")
|
||||||
v.FgColor = gocui.ColorWhite
|
v.FgColor = gocui.ColorWhite
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,7 +198,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||||
if err != gocui.ErrUnknownView {
|
if err != gocui.ErrUnknownView {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
v.Title = lang.SLocalize("StashTitle", "Stash")
|
v.Title = gui.Tr.SLocalize("StashTitle", "Stash")
|
||||||
v.FgColor = gocui.ColorWhite
|
v.FgColor = gocui.ColorWhite
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,7 +217,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
g.SetViewOnBottom("commitMessage")
|
g.SetViewOnBottom("commitMessage")
|
||||||
commitMessageView.Title = lang.SLocalize("CommitMessage", "Commit message")
|
commitMessageView.Title = gui.Tr.SLocalize("CommitMessage", "Commit message")
|
||||||
commitMessageView.FgColor = gocui.ColorWhite
|
commitMessageView.FgColor = gocui.ColorWhite
|
||||||
commitMessageView.Editable = true
|
commitMessageView.Editable = true
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package lang
|
package i18n
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/nicksnyder/go-i18n/v2/i18n"
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
||||||
|
|
|
@ -1,45 +1,68 @@
|
||||||
package lang
|
package i18n
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/cloudfoundry/jibber_jabber"
|
"github.com/cloudfoundry/jibber_jabber"
|
||||||
"github.com/nicksnyder/go-i18n/v2/i18n"
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
||||||
"golang.org/x/text/language"
|
"golang.org/x/text/language"
|
||||||
)
|
)
|
||||||
|
|
||||||
// the function to setup the localizer
|
// Localizer will translate a message into the user's language
|
||||||
func getlocalizer() *i18n.Localizer {
|
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
|
// detect the user's language
|
||||||
userLang, _ := jibber_jabber.DetectLanguage()
|
userLang, _ := jibber_jabber.DetectLanguage()
|
||||||
|
log.Info("language: " + userLang)
|
||||||
|
|
||||||
// create a i18n bundle that can be used to add translations and other things
|
// 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)
|
addBundles(i18nBundle)
|
||||||
i18nObject = addDutch(i18nObject)
|
|
||||||
|
|
||||||
// return the new localizer that can be used to translate text
|
// 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
|
localizer := &Localizer{
|
||||||
var localizer = getlocalizer()
|
i18nLocalizer: i18nLocalizer,
|
||||||
|
language: userLang,
|
||||||
|
Log: log,
|
||||||
|
}
|
||||||
|
|
||||||
|
return localizer, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Localize handels the translations
|
// Localize handels the translations
|
||||||
// expects i18n.LocalizeConfig as input: https://godoc.org/github.com/nicksnyder/go-i18n/v2/i18n#Localizer.MustLocalize
|
// expects i18n.LocalizeConfig as input: https://godoc.org/github.com/nicksnyder/go-i18n/v2/i18n#Localizer.MustLocalize
|
||||||
// output: translated string
|
// output: translated string
|
||||||
func Localize(config *i18n.LocalizeConfig) string {
|
func (l *Localizer) Localize(config *i18n.LocalizeConfig) string {
|
||||||
return localizer.MustLocalize(config)
|
return l.i18nLocalizer.MustLocalize(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SLocalize (short localize) is for 1 line localizations
|
// SLocalize (short localize) is for 1 line localizations
|
||||||
// ID: The id that is used in the .toml translation files
|
// 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
|
// 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 {
|
func (l *Localizer) SLocalize(ID string, Other string) string {
|
||||||
return Localize(&i18n.LocalizeConfig{
|
return l.Localize(&i18n.LocalizeConfig{
|
||||||
DefaultMessage: &i18n.Message{
|
DefaultMessage: &i18n.Message{
|
||||||
ID: ID,
|
ID: ID,
|
||||||
Other: Other,
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue