aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ki18n/gettext/gettext.go20
-rw-r--r--ki18n/gettext/gettext_test.go23
2 files changed, 29 insertions, 14 deletions
diff --git a/ki18n/gettext/gettext.go b/ki18n/gettext/gettext.go
index de5a9f2..dbf82d0 100644
--- a/ki18n/gettext/gettext.go
+++ b/ki18n/gettext/gettext.go
@@ -11,6 +11,7 @@ import (
"os"
"path/filepath"
"regexp"
+ "strconv"
"strings"
"github.com/martinlehoux/kagamigo/kcore"
@@ -84,11 +85,11 @@ func (p ArgsParser) ArgsCount() int {
func extractKeys(content string) map[string]int {
extractedKeys := make(map[string]int, 0)
- reg := regexp.MustCompile(`\{ ?((?:login\.)?Tr\("\w+"[\w"(),. :]*\) ?)\}`)
+ reg := regexp.MustCompile(`\{ ?((?:login\.)?Tr\("[^"]*"[^}]*\) ?)\}`)
matches := reg.FindAllStringSubmatch(content, -1)
for _, match := range matches {
fakePackage := fmt.Sprintf(`package main
-
+
func main() {
%s
}
@@ -109,8 +110,13 @@ func extractKeys(content string) map[string]int {
isTrCall = ident.Name == "Tr"
}
if isTrCall {
- key := strings.Trim(call.Args[0].(*ast.BasicLit).Value, `"`)
- extractedKeys[key] = len(call.Args) - 1
+ keyLiteral := call.Args[1].(*ast.BasicLit).Value
+ key, err := strconv.Unquote(keyLiteral)
+ if err != nil {
+ slog.Warn("error unquoting key", "key", keyLiteral)
+ key = strings.Trim(keyLiteral, `"`)
+ }
+ extractedKeys[key] = len(call.Args) - 2
}
}
return extractedKeys
@@ -139,9 +145,9 @@ func getOrCreateLocale(lang string, logger *slog.Logger) map[string]string {
locales, err := os.ReadFile(filepath.Join("locales", lang, "index.yml")) // #nosec G304
if errors.Is(err, os.ErrNotExist) {
logger.Info("no locales file found, creating")
- err = os.MkdirAll(filepath.Join("locales", lang), 0700)
+ err = os.MkdirAll(filepath.Join("locales", lang), 0o700)
kcore.Expect(err, "error creating directory")
- err = os.WriteFile(filepath.Join("locales", lang, "index.yml"), []byte{}, 0600)
+ err = os.WriteFile(filepath.Join("locales", lang, "index.yml"), []byte{}, 0o600)
kcore.Expect(err, "error writing file")
locales, err = os.ReadFile(filepath.Join("locales", lang, "index.yml")) // #nosec G304
kcore.Expect(err, "error reading file")
@@ -201,7 +207,7 @@ func main() {
if *write {
content, err := yaml.Marshal(newLocales)
kcore.Expect(err, "error marshalling yaml")
- kcore.Expect(os.WriteFile(filepath.Join("locales", lang, "index.yml"), content, 0600), "error writing file")
+ kcore.Expect(os.WriteFile(filepath.Join("locales", lang, "index.yml"), content, 0o600), "error writing file")
}
}
}
diff --git a/ki18n/gettext/gettext_test.go b/ki18n/gettext/gettext_test.go
index 26b5870..14c7160 100644
--- a/ki18n/gettext/gettext_test.go
+++ b/ki18n/gettext/gettext_test.go
@@ -7,7 +7,7 @@ import (
)
func TestExtractKeys(t *testing.T) {
- content := `{ login.Tr("Hello") }`
+ content := `{ login.Tr("fr", "Hello") }`
keys := extractKeys(content)
@@ -16,7 +16,7 @@ func TestExtractKeys(t *testing.T) {
}
func TestExtractKeysWithoutSpaces(t *testing.T) {
- content := `{login.Tr("Hello")}`
+ content := `{login.Tr("fr", "Hello")}`
keys := extractKeys(content)
@@ -25,7 +25,7 @@ func TestExtractKeysWithoutSpaces(t *testing.T) {
}
func TestExtractKeysWithOneArgs(t *testing.T) {
- content := `{ login.Tr("approveButton", a.Username) }`
+ content := `{ login.Tr("fr", "approveButton", a.Username) }`
keys := extractKeys(content)
@@ -34,7 +34,7 @@ func TestExtractKeysWithOneArgs(t *testing.T) {
}
func TestExtractKeysWithSeveralArgs(t *testing.T) {
- content := `{ login.Tr("approveButton", a.Username, a.Age, a.Email) }`
+ content := `{ login.Tr("fr", "approveButton", a.Username, a.Age, a.Email) }`
keys := extractKeys(content)
@@ -43,7 +43,7 @@ func TestExtractKeysWithSeveralArgs(t *testing.T) {
}
func TestExtractKeysWithComplexArgs(t *testing.T) {
- content := `{ login.Tr("raceStart_chosen", a.StartAt.Format("Monday, January 2, 2006 at 15:04")) }`
+ content := `{ login.Tr("fr", "raceStart_chosen", a.StartAt.Format("Monday, January 2, 2006 at 15:04")) }`
keys := extractKeys(content)
@@ -52,7 +52,7 @@ func TestExtractKeysWithComplexArgs(t *testing.T) {
}
func TestExtractMultipleKeys(t *testing.T) {
- content := `{ login.Tr("test_1", a.StartAt.Format("Monday, January 2, 2006 at 15:04")) }{ login.Tr("test_2", a.Test) }`
+ content := `{ login.Tr("fr", "test_1", a.StartAt.Format("Monday, January 2, 2006 at 15:04")) }{ login.Tr("fr", "test_2", a.Test) }`
keys := extractKeys(content)
@@ -62,10 +62,19 @@ func TestExtractMultipleKeys(t *testing.T) {
}
func TestExtractKeyFromSimpleTrFunc(t *testing.T) {
- content := `{ Tr("test") }`
+ content := `{ Tr("fr", "test") }`
keys := extractKeys(content)
assert.Len(t, keys, 1)
assert.Equal(t, 0, keys["test"])
}
+
+func TestExtractKeyWithSpan(t *testing.T) {
+ content := `{ Tr("fr", "Hello <span class=\"text-bold\">%s</span>", username) }`
+
+ keys := extractKeys(content)
+
+ assert.Len(t, keys, 1)
+ assert.Equal(t, 1, keys["Hello <span class=\"text-bold\">%s</span>"])
+}