diff options
author | Anthony HAMON <anthony.hamon@iadvize.com> | 2018-08-20 21:04:04 +0200 |
---|---|---|
committer | Anthony HAMON <anthony.hamon@iadvize.com> | 2018-08-20 21:12:42 +0200 |
commit | e4070ccb4f2de10f873561a0794bbae1a0513a29 (patch) | |
tree | 8a831c234f5bafd3a788e7cd15ab269259b0270f /pkg | |
parent | 3c0fb9b32461ae2fe4e71eeeda95583bd64a812a (diff) |
rewrite language detection, rewrite tests
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/app/app.go | 5 | ||||
-rw-r--r-- | pkg/i18n/i18n.go | 53 | ||||
-rw-r--r-- | pkg/i18n/i18n_test.go | 73 |
3 files changed, 64 insertions, 67 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..e46cb3fcb 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 @@ -96,3 +75,29 @@ func addBundles(log *logrus.Logger, i18nBundle *i18n.Bundle) { } } + +// 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 index 3fec62456..481a40863 100644 --- a/pkg/i18n/i18n_test.go +++ b/pkg/i18n/i18n_test.go @@ -1,7 +1,7 @@ package i18n import ( - "os" + "fmt" "testing" "github.com/nicksnyder/go-i18n/v2/i18n" @@ -11,38 +11,46 @@ import ( ) func TestNewLocalizer(t *testing.T) { + assert.NotNil(t, NewLocalizer(logrus.New())) +} + +func TestDetectLanguage(t *testing.T) { type scenario struct { - setup func() - test func(*Localizer, error) - teardown func() + langDetector func() (string, error) + expected string } - LCALL := os.Getenv("LC_ALL") - LANG := os.Getenv("LANG") - scenarios := []scenario{ { - func() { - os.Setenv("LC_ALL", "") - os.Setenv("LANG", "") - }, - func(l *Localizer, err error) { - assert.EqualValues(t, "C", l.GetLanguage()) - }, - func() { - os.Setenv("LC_ALL", LCALL) - os.Setenv("LANG", LANG) + func() (string, error) { + return "", fmt.Errorf("An error occurred") }, + "C", }, { - func() { - os.Setenv("LC_ALL", "whatever") - os.Setenv("LANG", "whatever") + func() (string, error) { + return "en", nil }, - func(l *Localizer, err error) { - assert.NoError(t, err) + "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) + } - assert.EqualValues(t, "whatever", l.GetLanguage()) + 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", @@ -51,18 +59,10 @@ func TestNewLocalizer(t *testing.T) { 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"})) }, - func() { - os.Setenv("LC_ALL", LCALL) - os.Setenv("LANG", LANG) - }, }, { - func() { - os.Setenv("LC_ALL", "nl") - }, - func(l *Localizer, err error) { - assert.NoError(t, err) - + "nl", + func(l *Localizer) { assert.EqualValues(t, "nl", l.GetLanguage()) assert.Equal(t, "Diff", l.Localize(&i18n.LocalizeConfig{ DefaultMessage: &i18n.Message{ @@ -72,15 +72,10 @@ func TestNewLocalizer(t *testing.T) { 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"})) }, - func() { - os.Setenv("LC_ALL", LCALL) - }, }, } for _, s := range scenarios { - s.setup() - s.test(NewLocalizer(logrus.New())) - s.teardown() + s.test(setupLocalizer(logrus.New(), s.userLang)) } } |