diff --git a/cli/xgotext/README.md b/cli/xgotext/README.md index 3ec29dd..e488c39 100644 --- a/cli/xgotext/README.md +++ b/cli/xgotext/README.md @@ -1,6 +1,6 @@ # 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 @@ -11,7 +11,15 @@ go install github.com/leonelquinteros/gotext/cli/xgotext ## 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 @@ -27,7 +35,7 @@ Isn't able to parse calls to translation functions using parameters inside varia gotext.Get("Translate this") 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) ``` diff --git a/cli/xgotext/fixtures/main.go b/cli/xgotext/fixtures/main.go index 19b0b0a..29944d1 100644 --- a/cli/xgotext/fixtures/main.go +++ b/cli/xgotext/fixtures/main.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "github.com/leonelquinteros/gotext" @@ -78,6 +79,9 @@ func main() { // redefine alias with fake struct alias := Fake2{} alias.Get("3") + + err := errors.New("test") + fmt.Print(err.Error()) } // dummy function diff --git a/cli/xgotext/main.go b/cli/xgotext/main.go index 17ef313..fa12cd4 100644 --- a/cli/xgotext/main.go +++ b/cli/xgotext/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "log" + "strings" "github.com/leonelquinteros/gotext/cli/xgotext/parser" ) @@ -11,6 +12,7 @@ var ( dirName = flag.String("in", "", "input dir: /path/to/go/pkg") outputDir = flag.String("out", "", "output dir: /path/to/i18n/files") defaultDomain = flag.String("default", "default", "Name of default domain") + excludeDirs = flag.String("exclude", ".git", "Comma separated list of directories to exclude") ) func main() { @@ -30,7 +32,7 @@ func main() { Default: *defaultDomain, } - err := parser.ParseDirRec(*dirName, data) + err := parser.ParseDirRec(*dirName, strings.Split(*excludeDirs, ","), data) if err != nil { log.Fatal(err) } diff --git a/cli/xgotext/parser/domain.go b/cli/xgotext/parser/domain.go index 08c1765..55ff4a5 100644 --- a/cli/xgotext/parser/domain.go +++ b/cli/xgotext/parser/domain.go @@ -186,7 +186,7 @@ func (m *DomainMap) Save(directory string) error { // save each domain in a separate po file 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 { return fmt.Errorf("failed to save domain %s: %v", name, err) } diff --git a/cli/xgotext/parser/golang.go b/cli/xgotext/parser/golang.go index 62fbdd9..2693503 100644 --- a/cli/xgotext/parser/golang.go +++ b/cli/xgotext/parser/golang.go @@ -165,7 +165,7 @@ func (g *GoFile) checkType(rawType types.Type) bool { return g.checkType(t.Elem()) 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 } default: @@ -193,7 +193,8 @@ func (g *GoFile) inspectCallExpr(n *ast.CallExpr) { } else { // validate type of object - if !g.checkType(g.getType(e).Type()) { + t := g.getType(e) + if t == nil || !g.checkType(t.Type()) { return } } @@ -201,7 +202,8 @@ func (g *GoFile) inspectCallExpr(n *ast.CallExpr) { // call to attribute case *ast.SelectorExpr: // 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 } diff --git a/cli/xgotext/parser/parser.go b/cli/xgotext/parser/parser.go index 9f12e1b..4fdd2d3 100644 --- a/cli/xgotext/parser/parser.go +++ b/cli/xgotext/parser/parser.go @@ -1,8 +1,10 @@ package parser import ( + "log" "os" "path/filepath" + "strings" ) // ParseDirFunc parses one directory @@ -34,7 +36,7 @@ func ParseDir(dirPath, basePath string, data *DomainMap) error { } // 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) 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() { + // 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) if err != nil { return err