Merge pull request #36 from bboehmke/xgotext_fixes

xgotext fixes
This commit is contained in:
Leonel Quinteros
2020-02-29 16:38:15 -03:00
committed by GitHub
6 changed files with 36 additions and 9 deletions

View File

@@ -1,6 +1,6 @@
# xgotext # xgotext
CLI tool to extract translation strings from Go packages into .PO files. CLI tool to extract translation strings from Go packages into .POT files.
## Installation ## Installation
@@ -11,7 +11,15 @@ go install github.com/leonelquinteros/gotext/cli/xgotext
## Usage ## Usage
``` ```
xgotext -in /path/to/go/package -out /path/to/output/dir Usage of xgotext:
-default string
Name of default domain (default "default")
-exclude string
Comma separated list of directories to exclude (default ".git")
-in string
input dir: /path/to/go/pkg
-out string
output dir: /path/to/i18n/files
``` ```
## Implementation ## Implementation
@@ -27,7 +35,7 @@ Isn't able to parse calls to translation functions using parameters inside varia
gotext.Get("Translate this") gotext.Get("Translate this")
tr := "Translate this string" tr := "Translate this string"
// The following line will NOT be added to the .po file // The following line will NOT be added to the .pot file
gotext.Get(tr) gotext.Get(tr)
``` ```

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"github.com/leonelquinteros/gotext" "github.com/leonelquinteros/gotext"
@@ -78,6 +79,9 @@ func main() {
// redefine alias with fake struct // redefine alias with fake struct
alias := Fake2{} alias := Fake2{}
alias.Get("3") alias.Get("3")
err := errors.New("test")
fmt.Print(err.Error())
} }
// dummy function // dummy function

View File

@@ -3,6 +3,7 @@ package main
import ( import (
"flag" "flag"
"log" "log"
"strings"
"github.com/leonelquinteros/gotext/cli/xgotext/parser" "github.com/leonelquinteros/gotext/cli/xgotext/parser"
) )
@@ -11,6 +12,7 @@ var (
dirName = flag.String("in", "", "input dir: /path/to/go/pkg") dirName = flag.String("in", "", "input dir: /path/to/go/pkg")
outputDir = flag.String("out", "", "output dir: /path/to/i18n/files") outputDir = flag.String("out", "", "output dir: /path/to/i18n/files")
defaultDomain = flag.String("default", "default", "Name of default domain") defaultDomain = flag.String("default", "default", "Name of default domain")
excludeDirs = flag.String("exclude", ".git", "Comma separated list of directories to exclude")
) )
func main() { func main() {
@@ -30,7 +32,7 @@ func main() {
Default: *defaultDomain, Default: *defaultDomain,
} }
err := parser.ParseDirRec(*dirName, data) err := parser.ParseDirRec(*dirName, strings.Split(*excludeDirs, ","), data)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@@ -186,7 +186,7 @@ func (m *DomainMap) Save(directory string) error {
// save each domain in a separate po file // save each domain in a separate po file
for name, domain := range m.Domains { for name, domain := range m.Domains {
err := domain.Save(filepath.Join(directory, name+".po")) err := domain.Save(filepath.Join(directory, name+".pot"))
if err != nil { if err != nil {
return fmt.Errorf("failed to save domain %s: %v", name, err) return fmt.Errorf("failed to save domain %s: %v", name, err)
} }

View File

@@ -165,7 +165,7 @@ func (g *GoFile) checkType(rawType types.Type) bool {
return g.checkType(t.Elem()) return g.checkType(t.Elem())
case *types.Named: case *types.Named:
if t.Obj().Pkg().Path() != "github.com/leonelquinteros/gotext" { if t.Obj().Pkg() == nil || t.Obj().Pkg().Path() != "github.com/leonelquinteros/gotext" {
return false return false
} }
default: default:
@@ -193,7 +193,8 @@ func (g *GoFile) inspectCallExpr(n *ast.CallExpr) {
} else { } else {
// validate type of object // validate type of object
if !g.checkType(g.getType(e).Type()) { t := g.getType(e)
if t == nil || !g.checkType(t.Type()) {
return return
} }
} }
@@ -201,7 +202,8 @@ func (g *GoFile) inspectCallExpr(n *ast.CallExpr) {
// call to attribute // call to attribute
case *ast.SelectorExpr: case *ast.SelectorExpr:
// validate type of object // validate type of object
if !g.checkType(g.getType(e.Sel).Type()) { t := g.getType(e.Sel)
if t == nil || !g.checkType(t.Type()) {
return return
} }

View File

@@ -1,8 +1,10 @@
package parser package parser
import ( import (
"log"
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
// ParseDirFunc parses one directory // ParseDirFunc parses one directory
@@ -34,7 +36,7 @@ func ParseDir(dirPath, basePath string, data *DomainMap) error {
} }
// ParseDirRec calls all known parser for each directory // ParseDirRec calls all known parser for each directory
func ParseDirRec(dirPath string, data *DomainMap) error { func ParseDirRec(dirPath string, exclude []string, data *DomainMap) error {
dirPath, _ = filepath.Abs(dirPath) dirPath, _ = filepath.Abs(dirPath)
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error { err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
@@ -43,6 +45,15 @@ func ParseDirRec(dirPath string, data *DomainMap) error {
} }
if info.IsDir() { if info.IsDir() {
// skip directory if in exclude list
subDir, _ := filepath.Rel(dirPath, path)
for _, d := range exclude {
if strings.HasPrefix(subDir, d) {
return nil
}
}
log.Print(path)
err := ParseDir(path, dirPath, data) err := ParseDir(path, dirPath, data)
if err != nil { if err != nil {
return err return err