diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2023-10-24 09:55:19 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2023-10-24 09:55:19 +0200 |
| commit | 18a9adb776ef18d3c2d947e22c0d5cf1f1fe7812 (patch) | |
| tree | 8fb15b799528d4844b79e72297d016118be30745 | |
| parent | a9927763f43d59c418b264790ccee59defe8ca29 (diff) | |
fix(gettext): Fix gettext to use with templv0.3.1
| -rwxr-xr-x | .githooks/pre-commit | 1 | ||||
| -rw-r--r-- | README.md | 2 | ||||
| -rwxr-xr-x | ci-deps.sh | 4 | ||||
| -rw-r--r-- | cmd/gettext/gettext.go | 30 | ||||
| -rw-r--r-- | cmd/gettext/gettext_test.go | 29 |
5 files changed, 39 insertions, 27 deletions
diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 6a353fc..36f6357 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -1,7 +1,6 @@ #!/bin/sh set -ev -templ generate gofmt -d -e -s . go vet ./... go run github.com/martinlehoux/kagamigo/cmd/cvet ./... @@ -1,3 +1,3 @@ ## TODO -- Repair gettext +- Add gettext to CI @@ -1,9 +1,7 @@ #!/bin/sh set -ev -go install github.com/a-h/templ/cmd/templ@latest go install github.com/fzipp/gocyclo/cmd/gocyclo@latest go install honnef.co/go/tools/cmd/staticcheck@latest go install github.com/securego/gosec/v2/cmd/gosec@latest -go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest -go install github.com/a-h/templ/cmd/templ@latest
\ No newline at end of file +go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
\ No newline at end of file diff --git a/cmd/gettext/gettext.go b/cmd/gettext/gettext.go index 2dbd65e..dd1ba0c 100644 --- a/cmd/gettext/gettext.go +++ b/cmd/gettext/gettext.go @@ -3,6 +3,9 @@ package main import ( "flag" "fmt" + "go/ast" + "go/parser" + "go/token" "io/fs" "os" "path/filepath" @@ -80,14 +83,25 @@ func (p ArgsParser) ArgsCount() int { func extractKeys(content string) map[string]int { extractedKeys := make(map[string]int, 0) - reg := regexp.MustCompile(`\{\{\s*call \$?\.T\s*"(\w+)"([\w.\s",:()]*)}}`) + reg := regexp.MustCompile(`\{ ?(login\.Tr\("\w+"[\w"(),. :]*\) ?)\}`) matches := reg.FindAllStringSubmatch(content, -1) for _, match := range matches { - key := match[1] - args := strings.TrimSpace(match[2]) - parser := ArgsParser{} - parser.Parse(args) - extractedKeys[key] = parser.ArgsCount() + fakePackage := fmt.Sprintf(`package main + + func main() { + %s + } + `, match[1]) + fset := token.NewFileSet() + f, err := parser.ParseFile(fset, "", fakePackage, 0) + kcore.Expect(err, "error parsing file") + main := f.Decls[0].(*ast.FuncDecl) + call := main.Body.List[0].(*ast.ExprStmt).X.(*ast.CallExpr) + selector := call.Fun.(*ast.SelectorExpr) + if selector.Sel.Name == "Tr" { + key := strings.Trim(call.Args[0].(*ast.BasicLit).Value, `"`) + extractedKeys[key] = len(call.Args) - 1 + } } return extractedKeys } @@ -95,8 +109,8 @@ func extractKeys(content string) map[string]int { func extractAllKeys() map[string]int { extractedKeys := make(map[string]int, 0) - err := filepath.Walk("templates", func(path string, info fs.FileInfo, err error) error { - if !info.IsDir() && filepath.Ext(path) == ".html" { + err := filepath.Walk(".", func(path string, info fs.FileInfo, err error) error { + if !info.IsDir() && filepath.Ext(path) == ".templ" { content, err := os.ReadFile(path) // #nosec G304 kcore.Expect(err, "error reading file") for key, value := range extractKeys(string(content)) { diff --git a/cmd/gettext/gettext_test.go b/cmd/gettext/gettext_test.go index f1947be..8952c73 100644 --- a/cmd/gettext/gettext_test.go +++ b/cmd/gettext/gettext_test.go @@ -7,7 +7,7 @@ import ( ) func TestExtractKeys(t *testing.T) { - content := `{{ call .T "Hello" }}` + content := `{ login.Tr("Hello") }` keys := extractKeys(content) @@ -16,16 +16,7 @@ func TestExtractKeys(t *testing.T) { } func TestExtractKeysWithoutSpaces(t *testing.T) { - content := `{{call .T "Hello"}}` - - keys := extractKeys(content) - - assert.Len(t, keys, 1) - assert.Equal(t, 0, keys["Hello"]) -} - -func TestExtractKeysWithDollar(t *testing.T) { - content := `{{ call $.T "Hello" }}` + content := `{login.Tr("Hello")}` keys := extractKeys(content) @@ -34,7 +25,7 @@ func TestExtractKeysWithDollar(t *testing.T) { } func TestExtractKeysWithOneArgs(t *testing.T) { - content := `{{ call .T "approveButton" .Username }}` + content := `{ login.Tr("approveButton", a.Username) }` keys := extractKeys(content) @@ -43,7 +34,7 @@ func TestExtractKeysWithOneArgs(t *testing.T) { } func TestExtractKeysWithSeveralArgs(t *testing.T) { - content := `{{ call .T "approveButton" .Username .Age .Email }}` + content := `{ login.Tr("approveButton", a.Username, a.Age, a.Email) }` keys := extractKeys(content) @@ -52,10 +43,20 @@ func TestExtractKeysWithSeveralArgs(t *testing.T) { } func TestExtractKeysWithComplexArgs(t *testing.T) { - content := `{{ call $.T "raceStart_chosen" (.StartAt.Format "Monday, January 2, 2006 at 15:04") }}` + content := `{ login.Tr("raceStart_chosen", a.StartAt.Format("Monday, January 2, 2006 at 15:04")) }` keys := extractKeys(content) assert.Len(t, keys, 1) assert.Equal(t, 1, keys["raceStart_chosen"]) } + +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) }` + + keys := extractKeys(content) + + assert.Len(t, keys, 2) + assert.Equal(t, 1, keys["test_1"]) + assert.Equal(t, 1, keys["test_2"]) +} |
