1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-11-26 09:00:57 +02:00

rewrite language detection, rewrite tests

This commit is contained in:
Anthony HAMON 2018-08-20 21:04:04 +02:00
parent 3c0fb9b324
commit e4070ccb4f
3 changed files with 65 additions and 68 deletions

View File

@ -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 {

View File

@ -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,
}
}

View File

@ -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) {
type scenario struct {
setup func()
test func(*Localizer, error)
teardown func()
}
assert.NotNil(t, NewLocalizer(logrus.New()))
}
LCALL := os.Getenv("LC_ALL")
LANG := os.Getenv("LANG")
func TestDetectLanguage(t *testing.T) {
type scenario struct {
langDetector func() (string, error)
expected string
}
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",
},
}
assert.EqualValues(t, "whatever", l.GetLanguage())
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",
@ -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))
}
}