diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2018-08-21 17:34:13 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-21 17:34:13 +1000 |
commit | da4c12bf9e50f2e4a03a0a2ae67cd4ba82220963 (patch) | |
tree | a23f94cbb9b66db19fa72a2fb2e25430d4c32cbb /pkg | |
parent | d6ee41358751e97b746785c80d7ba60d35b2ce07 (diff) | |
parent | 4972a4c21828220c60b6ca2d62be43930a639884 (diff) |
Merge pull request #193 from antham/add-tests
Add tests to i18n package
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/app/app.go | 5 | ||||
-rw-r--r-- | pkg/i18n/i18n.go | 70 | ||||
-rw-r--r-- | pkg/i18n/i18n_test.go | 81 |
3 files changed, 119 insertions, 37 deletions
diff --git a/pkg/app/app.go b/pkg/app/app.go index aaa925e53..20a97276e 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -51,10 +51,7 @@ func NewApp(config config.AppConfigurer) (*App, error) { return app, err } - app.Tr, err = i18n.NewLocalizer(app.Log) - if err != nil { - return app, err - } + app.Tr = i18n.NewLocalizer(app.Log) app.GitCommand, err = commands.NewGitCommand(app.Log, app.OSCommand) if err != nil { diff --git a/pkg/i18n/i18n.go b/pkg/i18n/i18n.go index e209d55c5..bb97cc9a0 100644 --- a/pkg/i18n/i18n.go +++ b/pkg/i18n/i18n.go @@ -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..481a40863 --- /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 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 test branch wil verwijderen?", l.TemplateLocalize("DeleteBranchMessage", Teml{"selectedBranchName": "test"})) + }, + }, + } + + for _, s := range scenarios { + s.test(setupLocalizer(logrus.New(), s.userLang)) + } +} |