diff options
Diffstat (limited to 'src/gotext.go')
-rw-r--r-- | src/gotext.go | 49 |
1 files changed, 33 insertions, 16 deletions
diff --git a/src/gotext.go b/src/gotext.go index 152b33c..0ac104e 100644 --- a/src/gotext.go +++ b/src/gotext.go @@ -8,7 +8,6 @@ import ( "go/token" "io" "io/ioutil" - "log" "os" "sort" "strings" @@ -84,9 +83,14 @@ type argsT struct{ keywordPlural string } +type envT struct{ + args argsT + in io.Reader + out io.Writer + err io.Writer +} -var msgIDs map[string][]msgID func formatComment(com string) string { out := "" @@ -160,6 +164,7 @@ func constructValue(val interface{}) string { func inspectNodeForTranslations( args argsT, + msgIDs map[string][]msgID, fset *token.FileSet, f *ast.File, n ast.Node, @@ -227,6 +232,7 @@ func inspectNodeForTranslations( msgidStr := formatI18nStr(i18nStr) posCall := fset.Position(n.Pos()) + msgIDs[msgidStr] = append(msgIDs[msgidStr], msgID{ formatHint: formatHint, msgidPlural: formatI18nStr(i18nStrPlural), @@ -257,22 +263,24 @@ func formatI18nStr(s string) string { return s } -func processFiles(args argsT) error { +func processFiles(args argsT) (map[string][]msgID, error) { // go over the input files - msgIDs = make(map[string][]msgID) + msgIDs := map[string][]msgID{} fset := token.NewFileSet() for _, fname := range args.subArgs { - if err := processSingleGoSource(args, fset, fname); err != nil { - return err + err := processSingleGoSource(args, msgIDs, fset, fname) + if err != nil { + return nil, err } } - return nil + return msgIDs, nil } func processSingleGoSource( args argsT, + msgIDs map[string][]msgID, fset *token.FileSet, fname string, ) error { @@ -288,7 +296,7 @@ func processSingleGoSource( } ast.Inspect(f, func(n ast.Node) bool { - return inspectNodeForTranslations(args, fset, f, n) + return inspectNodeForTranslations(args, msgIDs, fset, f, n) }) return nil @@ -308,8 +316,7 @@ func formatOutput(in string) string { } } -func writePotFile(out io.Writer) { - +func writePotFile(out io.Writer, msgIDs map[string][]msgID) { header := g.Heredoc(` #, fuzzy msgid "" @@ -358,7 +365,6 @@ func writePotFile(out io.Writer) { } fmt.Fprintf(out, "\n") } - } func usage(argv0 string, w io.Writer) { @@ -408,6 +414,16 @@ func getopt(allArgs []string, w io.Writer) (argsT, int) { }, 0 } +func run(env envT) int { + msgIDs, err := processFiles(env.args) + if err != nil { + fmt.Fprintln(env.err, err) + return 1 + } + + writePotFile(os.Stdout, msgIDs) + return 0 +} func Main() { // parse args @@ -421,12 +437,13 @@ func Main() { g.Init() args, rc := getopt(os.Args, os.Stderr) g.ExitIf(rc) + os.Exit(run(envT{ + args: args, + in: os.Stdin, + out: os.Stdout, + err: os.Stderr, + })) - if err := processFiles(args); err != nil { - log.Fatalf("processFiles failed with: %s", err) - } - - writePotFile(os.Stdout) } // SetLocale sets the program's current locale. |