summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Haller <stefan@haller-berlin.de>2024-07-13 12:14:39 +0200
committerGitHub <noreply@github.com>2024-07-13 12:14:39 +0200
commit71ad3fac63a3ef3326021837b49e9497d332818b (patch)
tree16fbcb506d1073d3deb6858ebbead45751fadd77
parente1d973d62a640e15c867a115f7362ab6f19d83e6 (diff)
parentae4a579153bbe799a785c27f956bca5be46e429c (diff)
Fix language auto detection (#3744)v0.43.1
- **PR Description** Fix a regression (introduced with #3649) that broke language auto-detection. When starting lazygit with the `gui.language` config set to "auto" (which is the default), lazygit would fail to start if the LANG environment is set to one of our supported languages. For example: ``` $ export LANG=nl_NL $ lazygit 2024/07/13 11:43:03 open translations/nl-NL.json: file does not exist ``` Fixes #3743
-rw-r--r--pkg/i18n/i18n.go2
-rw-r--r--pkg/i18n/i18n_test.go84
2 files changed, 85 insertions, 1 deletions
diff --git a/pkg/i18n/i18n.go b/pkg/i18n/i18n.go
index f9febbe01..3848d42cf 100644
--- a/pkg/i18n/i18n.go
+++ b/pkg/i18n/i18n.go
@@ -24,7 +24,7 @@ func NewTranslationSetFromConfig(log *logrus.Entry, configLanguage string) (*Tra
language := detectLanguage(jibber_jabber.DetectIETF)
for _, languageCode := range languageCodes {
if strings.HasPrefix(language, languageCode) {
- return newTranslationSet(log, language)
+ return newTranslationSet(log, languageCode)
}
}
diff --git a/pkg/i18n/i18n_test.go b/pkg/i18n/i18n_test.go
index 7023ea40a..8c5787fcb 100644
--- a/pkg/i18n/i18n_test.go
+++ b/pkg/i18n/i18n_test.go
@@ -2,8 +2,11 @@ package i18n
import (
"fmt"
+ "io"
+ "runtime"
"testing"
+ "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
@@ -33,3 +36,84 @@ func TestDetectLanguage(t *testing.T) {
assert.EqualValues(t, s.expected, detectLanguage(s.langDetector))
}
}
+
+// Can't use utils.NewDummyLog() because of a cyclic dependency
+func newDummyLog() *logrus.Entry {
+ log := logrus.New()
+ log.Out = io.Discard
+ return log.WithField("test", "test")
+}
+
+func TestNewTranslationSetFromConfig(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ // These tests are based on setting the LANG environment variable, which
+ // isn't respected on Windows.
+ t.Skip("Skipping test on Windows")
+ }
+
+ scenarios := []struct {
+ name string
+ configLanguage string
+ envLanguage string
+ expected string
+ expectedErr bool
+ }{
+ {
+ name: "configLanguage is nl",
+ configLanguage: "nl",
+ envLanguage: "en_US",
+ expected: "nl",
+ expectedErr: false,
+ },
+ {
+ name: "configLanguage is an unsupported language",
+ configLanguage: "xy",
+ envLanguage: "en_US",
+ expectedErr: true,
+ },
+ {
+ name: "auto-detection without LANG set",
+ configLanguage: "auto",
+ envLanguage: "",
+ expected: "en",
+ expectedErr: false,
+ },
+ {
+ name: "auto-detection with LANG set to nl_NL",
+ configLanguage: "auto",
+ envLanguage: "nl_NL",
+ expected: "nl",
+ expectedErr: false,
+ },
+ {
+ name: "auto-detection with LANG set to zh-CN",
+ configLanguage: "auto",
+ envLanguage: "zh-CN",
+ expected: "zh-CN",
+ expectedErr: false,
+ },
+ {
+ name: "auto-detection with LANG set to an unsupported language",
+ configLanguage: "auto",
+ envLanguage: "xy_XY",
+ expected: "en",
+ expectedErr: false,
+ },
+ }
+
+ for _, s := range scenarios {
+ t.Run(s.name, func(t *testing.T) {
+ log := newDummyLog()
+ t.Setenv("LANG", s.envLanguage)
+ actualTranslationSet, err := NewTranslationSetFromConfig(log, s.configLanguage)
+ if s.expectedErr {
+ assert.Error(t, err)
+ } else {
+ assert.NoError(t, err)
+
+ expectedTranslationSet, _ := newTranslationSet(log, s.expected)
+ assert.Equal(t, expectedTranslationSet, actualTranslationSet)
+ }
+ })
+ }
+}