diff options
-rw-r--r-- | src/gotext.go | 53 | ||||
-rw-r--r-- | tests/gotext.go | 25 |
2 files changed, 48 insertions, 30 deletions
diff --git a/src/gotext.go b/src/gotext.go index 0e61a91..2df437e 100644 --- a/src/gotext.go +++ b/src/gotext.go @@ -56,7 +56,12 @@ func formatComment(com string) string { return out } -func findCommentsForTranslation(fset *token.FileSet, f *ast.File, posCall token.Position) string { +func findCommentsForTranslation( + args argsT, + fset *token.FileSet, + f *ast.File, + posCall token.Position, +) string { com := "" for _, cg := range f.Comments { // search for all comments in the previous line @@ -74,7 +79,7 @@ func findCommentsForTranslation(fset *token.FileSet, f *ast.File, posCall token. // only return if we have a matching prefix formatedComment := formatComment(com) - needle := fmt.Sprintf("#. %s", opts.AddCommentsTag) + needle := fmt.Sprintf("#. %s", args.commentTag) if !strings.HasPrefix(formatedComment, needle) { formatedComment = "" } @@ -105,10 +110,15 @@ func constructValue(val interface{}) string { } } -func inspectNodeForTranslations(fset *token.FileSet, f *ast.File, n ast.Node) bool { +func inspectNodeForTranslations( + args argsT, + fset *token.FileSet, + f *ast.File, + n ast.Node, +) bool { // FIXME: this assume we always have a "gettext.Gettext" style keyword var gettextSelector, gettextFuncName string - l := strings.Split(opts.Keyword, ".") + l := strings.Split(args.keyword, ".") if len(l) > 1 { gettextSelector = l[0] @@ -118,7 +128,7 @@ func inspectNodeForTranslations(fset *token.FileSet, f *ast.File, n ast.Node) bo } var gettextSelectorPlural, gettextFuncNamePlural string - l = strings.Split(opts.KeywordPlural, ".") + l = strings.Split(args.keywordPlural, ".") if len(l) > 1 { gettextSelectorPlural = l[0] @@ -171,7 +181,9 @@ func inspectNodeForTranslations(fset *token.FileSet, f *ast.File, n ast.Node) bo msgidPlural: formatI18nStr(i18nStrPlural), fname: posCall.Filename, line: posCall.Line, - comment: findCommentsForTranslation(fset, f, posCall), + comment: findCommentsForTranslation( + args, fset, f, posCall, + ), }) } @@ -194,13 +206,13 @@ func formatI18nStr(s string) string { return s } -func processFiles(args []string) error { +func processFiles(args argsT) error { // go over the input files msgIDs = make(map[string][]msgID) fset := token.NewFileSet() - for _, fname := range args { - if err := processSingleGoSource(fset, fname); err != nil { + for _, fname := range args.subArgs { + if err := processSingleGoSource(args, fset, fname); err != nil { return err } } @@ -208,7 +220,11 @@ func processFiles(args []string) error { return nil } -func processSingleGoSource(fset *token.FileSet, fname string) error { +func processSingleGoSource( + args argsT, + fset *token.FileSet, + fname string, +) error { fnameContent, err := ioutil.ReadFile(fname) if err != nil { panic(err) @@ -221,7 +237,7 @@ func processSingleGoSource(fset *token.FileSet, fname string) error { } ast.Inspect(f, func(n ast.Node) bool { - return inspectNodeForTranslations(fset, f, n) + return inspectNodeForTranslations(args, fset, f, n) }) return nil @@ -307,13 +323,6 @@ type optsT struct { KeywordPlural string `long:"keyword-plural" default:"gt.NGettext" description:"look for WORD as the keyword for plural strings"` } -// FIXME: Remove me -var opts = optsT{ - AddCommentsTag: "TRANSLATORS:", - Keyword: "i18n.G", - KeywordPlural: "i18n.NG", -} - type argsT struct{ allArgs []string subArgs []string @@ -358,12 +367,6 @@ func getopt(allArgs []string, w io.Writer) (argsT, int) { return argsT{}, 2 } - opts = optsT{ - AddCommentsTag: *commentTag, - Keyword: *keyword, - KeywordPlural: *keywordPlural, - } - subArgs := fs.Args() return argsT{ @@ -389,7 +392,7 @@ func Main() { args, rc := getopt(os.Args, os.Stderr) g.ExitIf(rc) - if err := processFiles(args.subArgs); err != nil { + if err := processFiles(args); err != nil { log.Fatalf("processFiles failed with: %s", err) } diff --git a/tests/gotext.go b/tests/gotext.go index 3c097c8..da57254 100644 --- a/tests/gotext.go +++ b/tests/gotext.go @@ -52,7 +52,10 @@ func test_processFiles() { } `)) defer os.Remove(fname) - g.TErrorIf(processFiles([]string{fname})) + g.TErrorIf(processFiles(argsT{ + subArgs: []string{fname}, + keyword: "i18n.G", + })) g.TAssertEqual(msgIDs, map[string][]msgID{ "foo": []msgID{ @@ -78,7 +81,10 @@ func test_processFiles() { } `)) defer os.Remove(fname) - g.TErrorIf(processFiles([]string{fname})) + g.TErrorIf(processFiles(argsT{ + subArgs: []string{fname}, + keyword: "i18n.G", + })) g.TAssertEqual(msgIDs, map[string][]msgID{ "foo": []msgID{ @@ -106,7 +112,10 @@ func test_processFiles() { i18n.G("foo\n" + "bar\n" + "baz") } `)) - g.TErrorIf(processFiles([]string{fname})) + g.TErrorIf(processFiles(argsT{ + subArgs: []string{fname}, + keyword: "i18n.G", + })) g.TAssertEqual(msgIDs, map[string][]msgID{ "foo\\nbar\\nbaz": []msgID{ @@ -128,7 +137,10 @@ func test_processFiles() { } `), "`")) defer os.Remove(fname) - g.TErrorIf(processFiles([]string{fname})) + g.TErrorIf(processFiles(argsT{ + subArgs: []string{fname}, + keyword: "i18n.G", + })) out := bytes.NewBuffer([]byte("")) writePotFile(out) @@ -429,7 +441,10 @@ func test_writePotFile() { } `)) defer os.Remove(fname) - g.TErrorIf(processFiles([]string{fname})) + g.TErrorIf(processFiles(argsT{ + subArgs: []string{fname}, + keyword: "i18n.G", + })) out := bytes.NewBuffer([]byte("")) writePotFile(out) |