From 2082fdf84a823b7533792b99e5e3a5c3334586e9 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 17 Jul 2023 09:29:56 +1000 Subject: [PATCH] i18n for worktrees --- .../controllers/helpers/worktree_helper.go | 55 +++++++------------ pkg/gui/presentation/branches.go | 2 +- pkg/i18n/english.go | 16 +++++- 3 files changed, 34 insertions(+), 39 deletions(-) diff --git a/pkg/gui/controllers/helpers/worktree_helper.go b/pkg/gui/controllers/helpers/worktree_helper.go index bcf31a87e..602ad3ac3 100644 --- a/pkg/gui/controllers/helpers/worktree_helper.go +++ b/pkg/gui/controllers/helpers/worktree_helper.go @@ -2,7 +2,6 @@ package helpers import ( "errors" - "fmt" "io/fs" "os" "strings" @@ -73,7 +72,7 @@ func (self *WorktreeHelper) NewWorktree() error { f := func(detached bool) error { return self.c.Prompt(types.PromptOpts{ - Title: self.c.Tr.NewWorktreeBranch, + Title: self.c.Tr.NewWorktreeBase, InitialContent: currentBranchName, FindSuggestionsFunc: self.suggestionsHelper.GetRefsSuggestionsFunc(), HandleConfirm: func(base string) error { @@ -83,17 +82,19 @@ func (self *WorktreeHelper) NewWorktree() error { }) } + placeholders := map[string]string{"ref": "ref"} + return self.c.Menu(types.CreateMenuOptions{ Title: self.c.Tr.WorktreeTitle, Items: []*types.MenuItem{ { - LabelColumns: []string{"Create new worktree from ref"}, + LabelColumns: []string{utils.ResolvePlaceholderString(self.c.Tr.CreateWorktreeFrom, placeholders)}, OnPress: func() error { return f(false) }, }, { - LabelColumns: []string{"Create new worktree from ref (detached)"}, + LabelColumns: []string{utils.ResolvePlaceholderString(self.c.Tr.CreateWorktreeFromDetached, placeholders)}, OnPress: func() error { return f(true) }, @@ -128,9 +129,10 @@ func (self *WorktreeHelper) NewWorktreeCheckout(base string, canCheckoutBase boo } if canCheckoutBase { + title := utils.ResolvePlaceholderString(self.c.Tr.NewBranchNameLeaveBlank, map[string]string{"default": base}) // prompt for the new branch name where a blank means we just check out the branch return self.c.Prompt(types.PromptOpts{ - Title: fmt.Sprintf("New branch name (leave blank to checkout %s)", base), + Title: title, HandleConfirm: func(branchName string) error { opts.Branch = branchName @@ -140,10 +142,10 @@ func (self *WorktreeHelper) NewWorktreeCheckout(base string, canCheckoutBase boo } else { // prompt for the new branch name where a blank means we just check out the branch return self.c.Prompt(types.PromptOpts{ - Title: "New branch name", + Title: self.c.Tr.NewBranchName, HandleConfirm: func(branchName string) error { if branchName == "" { - return self.c.ErrorMsg("Branch name cannot be blank") + return self.c.ErrorMsg(self.c.Tr.BranchNameCannotBeBlank) } opts.Branch = branchName @@ -217,47 +219,28 @@ func (self *WorktreeHelper) Detach(worktree *models.Worktree) error { } func (self *WorktreeHelper) ViewWorktreeOptions(context types.IListContext, ref string) error { - if context == self.c.Contexts().Branches { - return self.ViewBranchWorktreeOptions(ref) - } + currentBranch := self.refsHelper.GetCheckedOutRef() + canCheckoutBase := context == self.c.Contexts().Branches && ref != currentBranch.RefName() - return self.ViewRefWorktreeOptions(ref) + return self.ViewBranchWorktreeOptions(ref, canCheckoutBase) } -func (self *WorktreeHelper) ViewBranchWorktreeOptions(branchName string) error { +func (self *WorktreeHelper) ViewBranchWorktreeOptions(branchName string, canCheckoutBase bool) error { + placeholders := map[string]string{"ref": branchName} + return self.c.Menu(types.CreateMenuOptions{ Title: self.c.Tr.WorktreeTitle, Items: []*types.MenuItem{ { - LabelColumns: []string{"Create new worktree from branch"}, + LabelColumns: []string{utils.ResolvePlaceholderString(self.c.Tr.CreateWorktreeFrom, placeholders)}, OnPress: func() error { - return self.NewWorktreeCheckout(branchName, true, false) + return self.NewWorktreeCheckout(branchName, canCheckoutBase, false) }, }, { - LabelColumns: []string{"Create new worktree from branch (detached)"}, + LabelColumns: []string{utils.ResolvePlaceholderString(self.c.Tr.CreateWorktreeFromDetached, placeholders)}, OnPress: func() error { - return self.NewWorktreeCheckout(branchName, true, true) - }, - }, - }, - }) -} - -func (self *WorktreeHelper) ViewRefWorktreeOptions(ref string) error { - return self.c.Menu(types.CreateMenuOptions{ - Title: self.c.Tr.WorktreeTitle, - Items: []*types.MenuItem{ - { - LabelColumns: []string{"Create new worktree from ref"}, - OnPress: func() error { - return self.NewWorktreeCheckout(ref, false, false) - }, - }, - { - LabelColumns: []string{"Create new worktree from ref (detached)"}, - OnPress: func() error { - return self.NewWorktreeCheckout(ref, false, true) + return self.NewWorktreeCheckout(branchName, canCheckoutBase, true) }, }, }, diff --git a/pkg/gui/presentation/branches.go b/pkg/gui/presentation/branches.go index 243c9d0a3..42cbdf22b 100644 --- a/pkg/gui/presentation/branches.go +++ b/pkg/gui/presentation/branches.go @@ -51,7 +51,7 @@ func getBranchDisplayStrings( coloredName := nameTextStyle.Sprint(displayName) branchStatus := utils.WithPadding(ColoredBranchStatus(b, tr), 2, utils.AlignLeft) if b.CheckedOutByOtherWorktree { - worktreeIcon := lo.Ternary(icons.IsIconEnabled(), icons.LINKED_WORKTREE_ICON, "(worktree)") + worktreeIcon := lo.Ternary(icons.IsIconEnabled(), icons.LINKED_WORKTREE_ICON, fmt.Sprintf("(%s)", tr.LcWorktree)) coloredName = fmt.Sprintf("%s %s", coloredName, style.FgDefault.Sprint(worktreeIcon)) } coloredName = fmt.Sprintf("%s %s", coloredName, branchStatus) diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 699b5f52c..6463bbaf3 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -562,8 +562,14 @@ type TranslationSet struct { MainWorktree string CreateWorktree string NewWorktreePath string - NewWorktreeBranch string + NewWorktreeBase string + BranchNameCannotBeBlank string + NewBranchName string + NewBranchNameLeaveBlank string ViewWorktreeOptions string + CreateWorktreeFrom string + CreateWorktreeFromDetached string + LcWorktree string Name string Branch string Path string @@ -1288,8 +1294,14 @@ func EnglishTranslationSet() TranslationSet { MainWorktree: "(main)", CreateWorktree: "Create worktree", NewWorktreePath: "New worktree path", - NewWorktreeBranch: "New worktree branch (leave blank to use the current branch)", + NewWorktreeBase: "New worktree base ref", + BranchNameCannotBeBlank: "Branch name cannot be blank", + NewBranchName: "New branch name", + NewBranchNameLeaveBlank: "New branch name (leave blank to checkout {{.default}})", ViewWorktreeOptions: "View worktree options", + CreateWorktreeFrom: "Create worktree from {{.ref}}", + CreateWorktreeFromDetached: "Create worktree from {{.ref}} (detached)", + LcWorktree: "worktree", Name: "Name", Branch: "Branch", Path: "Path",