diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2018-08-25 11:02:46 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2018-08-25 11:02:46 +1000 |
commit | f24c95aedec785985dfcd98ff3997152c193bd3e (patch) | |
tree | 9f8ac1a61cf686052f623b767e13d6d1a14e3683 /pkg/i18n | |
parent | 5628eae502ee3b4d3d72a8e4045713b637f7a764 (diff) | |
parent | 93ab892bdd1226f9a519a938c8b28590e71e54f3 (diff) |
Merge branch 'master' into feature/auto-updates
Diffstat (limited to 'pkg/i18n')
-rw-r--r-- | pkg/i18n/dutch.go | 8 | ||||
-rw-r--r-- | pkg/i18n/english.go | 8 | ||||
-rw-r--r-- | pkg/i18n/i18n.go | 72 | ||||
-rw-r--r-- | pkg/i18n/i18n_test.go | 81 |
4 files changed, 133 insertions, 36 deletions
diff --git a/pkg/i18n/dutch.go b/pkg/i18n/dutch.go index 60133e662..68e7c82bd 100644 --- a/pkg/i18n/dutch.go +++ b/pkg/i18n/dutch.go @@ -132,7 +132,10 @@ func addDutch(i18nObject *i18n.Bundle) error { Other: "Verwijder branch", }, &i18n.Message{ ID: "DeleteBranchMessage", - Other: "Weet je zeker dat je {{.selectedBranchName}} branch wil verwijderen?", + Other: "Weet je zeker dat je branch {{.selectedBranchName}} wil verwijderen?", + }, &i18n.Message{ + ID: "ForceDeleteBranchMessage", + Other: "Weet je zeker dat je branch {{.selectedBranchName}} geforceerd wil verwijderen?", }, &i18n.Message{ ID: "CantMergeBranchIntoItself", Other: "Je kan niet een branch in zichzelf mergen", @@ -152,6 +155,9 @@ func addDutch(i18nObject *i18n.Bundle) error { ID: "deleteBranch", Other: "verwijder branch", }, &i18n.Message{ + ID: "forceDeleteBranch", + Other: "verwijder branch (forceer)", + }, &i18n.Message{ ID: "NoBranchesThisRepo", Other: "Geen branches voor deze repo", }, &i18n.Message{ diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index c0384136b..38fbac4cb 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -140,7 +140,10 @@ func addEnglish(i18nObject *i18n.Bundle) error { Other: "Delete Branch", }, &i18n.Message{ ID: "DeleteBranchMessage", - Other: "Are you sure you want delete the branch {{.selectedBranchName}} ?", + Other: "Are you sure you want to delete the branch {{.selectedBranchName}}?", + }, &i18n.Message{ + ID: "ForceDeleteBranchMessage", + Other: "Are you sure you want to force delete the branch {{.selectedBranchName}}?", }, &i18n.Message{ ID: "CantMergeBranchIntoItself", Other: "You cannot merge a branch into itself", @@ -160,6 +163,9 @@ func addEnglish(i18nObject *i18n.Bundle) error { ID: "deleteBranch", Other: "delete branch", }, &i18n.Message{ + ID: "forceDeleteBranch", + Other: "delete branch (force)", + }, &i18n.Message{ ID: "NoBranchesThisRepo", Other: "No branches for this repo", }, &i18n.Message{ diff --git a/pkg/i18n/i18n.go b/pkg/i18n/i18n.go index e209d55c5..898a13906 100644 --- a/pkg/i18n/i18n.go +++ b/pkg/i18n/i18n.go @@ -1,7 +1,7 @@ package i18n import ( - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" "github.com/cloudfoundry/jibber_jabber" "github.com/nicksnyder/go-i18n/v2/i18n" "golang.org/x/text/language" @@ -18,33 +18,12 @@ type Localizer struct { } // NewLocalizer creates a new Localizer -func NewLocalizer(log *logrus.Logger) (*Localizer, error) { +func NewLocalizer(log *logrus.Logger) *Localizer { + userLang := detectLanguage(jibber_jabber.DetectLanguage) - // detect the user's language - userLang, err := jibber_jabber.DetectLanguage() - if err != nil { - if err.Error() != "Could not detect Language" { - return nil, err - } - userLang = "C" - } log.Info("language: " + userLang) - // create a i18n bundle that can be used to add translations and other things - i18nBundle := &i18n.Bundle{DefaultLanguage: language.English} - - addBundles(log, i18nBundle) - - // return the new localizer that can be used to translate text - i18nLocalizer := i18n.NewLocalizer(i18nBundle, userLang) - - localizer := &Localizer{ - i18nLocalizer: i18nLocalizer, - language: userLang, - Log: log, - } - - return localizer, nil + return setupLocalizer(log, userLang) } // Localize handels the translations @@ -82,17 +61,42 @@ func (l *Localizer) GetLanguage() string { // add translation file(s) func addBundles(log *logrus.Logger, i18nBundle *i18n.Bundle) { - err := addPolish(i18nBundle) - if err != nil { - log.Fatal(err) + fs := []func(*i18n.Bundle) error{ + addPolish, + addDutch, + addEnglish, } - err = addDutch(i18nBundle) - if err != nil { - log.Fatal(err) + + for _, f := range fs { + if err := f(i18nBundle); err != nil { + log.Fatal(err) + + } } - err = addEnglish(i18nBundle) - if err != nil { - log.Fatal(err) +} + +// detectLanguage extracts user language from environment +func detectLanguage(langDetector func() (string, error)) string { + if userLang, err := langDetector(); err == nil { + return userLang } + return "C" +} + +// setupLocalizer creates a new localizer using given userLang +func setupLocalizer(log *logrus.Logger, userLang string) *Localizer { + // create a i18n bundle that can be used to add translations and other things + i18nBundle := &i18n.Bundle{DefaultLanguage: language.English} + + addBundles(log, i18nBundle) + + // return the new localizer that can be used to translate text + i18nLocalizer := i18n.NewLocalizer(i18nBundle, userLang) + + return &Localizer{ + i18nLocalizer: i18nLocalizer, + language: userLang, + Log: log, + } } diff --git a/pkg/i18n/i18n_test.go b/pkg/i18n/i18n_test.go new file mode 100644 index 000000000..5eeddb907 --- /dev/null +++ b/pkg/i18n/i18n_test.go @@ -0,0 +1,81 @@ +package i18n + +import ( + "fmt" + "testing" + + "github.com/nicksnyder/go-i18n/v2/i18n" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" +) + +func TestNewLocalizer(t *testing.T) { + assert.NotNil(t, NewLocalizer(logrus.New())) +} + +func TestDetectLanguage(t *testing.T) { + type scenario struct { + langDetector func() (string, error) + expected string + } + + scenarios := []scenario{ + { + func() (string, error) { + return "", fmt.Errorf("An error occurred") + }, + "C", + }, + { + func() (string, error) { + return "en", nil + }, + "en", + }, + } + + for _, s := range scenarios { + assert.EqualValues(t, s.expected, detectLanguage(s.langDetector)) + } +} + +func TestLocalizer(t *testing.T) { + type scenario struct { + userLang string + test func(*Localizer) + } + + scenarios := []scenario{ + { + "C", + func(l *Localizer) { + assert.EqualValues(t, "C", l.GetLanguage()) + assert.Equal(t, "Diff", l.Localize(&i18n.LocalizeConfig{ + DefaultMessage: &i18n.Message{ + ID: "DiffTitle", + }, + })) + assert.Equal(t, "Diff", l.SLocalize("DiffTitle")) + assert.Equal(t, "Are you sure you want to delete the branch test?", l.TemplateLocalize("DeleteBranchMessage", Teml{"selectedBranchName": "test"})) + }, + }, + { + "nl", + func(l *Localizer) { + assert.EqualValues(t, "nl", l.GetLanguage()) + assert.Equal(t, "Diff", l.Localize(&i18n.LocalizeConfig{ + DefaultMessage: &i18n.Message{ + ID: "DiffTitle", + }, + })) + assert.Equal(t, "Diff", l.SLocalize("DiffTitle")) + assert.Equal(t, "Weet je zeker dat je branch test wil verwijderen?", l.TemplateLocalize("DeleteBranchMessage", Teml{"selectedBranchName": "test"})) + }, + }, + } + + for _, s := range scenarios { + s.test(setupLocalizer(logrus.New(), s.userLang)) + } +} |