From c366ce0fe48d1d5e1e3f51997d28b346f54d5e56 Mon Sep 17 00:00:00 2001 From: Slach Date: Sat, 27 Jan 2018 17:42:28 +0500 Subject: [PATCH] add filepath.Glob matching for go-xgettext Signed-off-by: Slach --- .gitignore | 2 ++ go-xgettext/main.go | 28 ++++++++++++++++++++++------ go-xgettext/main_test.go | 28 ++++++++++++++-------------- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 45d62d8..bcd0852 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ *.sw? +.idea +.vagrant diff --git a/go-xgettext/main.go b/go-xgettext/main.go index 5f89abe..9f900f3 100644 --- a/go-xgettext/main.go +++ b/go-xgettext/main.go @@ -40,6 +40,7 @@ import ( "sort" "strings" "time" + "path/filepath" ) var ( @@ -76,7 +77,7 @@ type keywordDef struct { type keywords map[string]*keywordDef -type allKeywordsConfig []*keywordDef +// type allKeywordsConfig []*keywordDef type msgID struct { msgidPlural string @@ -153,7 +154,7 @@ func constructValue(val interface{}) (string, error) { return "", err } // strip left " (or `) - right = right[1:len(right)] + right = right[1:] return left + right, nil default: return "", fmt.Errorf("unknown type: %v", val) @@ -267,8 +268,18 @@ func processFiles(args []string) error { fset := token.NewFileSet() for _, fname := range args { - if err := processSingleGoSource(fset, fname); err != nil { - return err + if _, err := os.Stat(fname); err==nil { + if err := processSingleGoSource(fset, fname); err != nil { + return err + } + } else { + if fnames , err := filepath.Glob(fname); err == nil { + for _, fname := range fnames { + if err := processSingleGoSource(fset, fname); err != nil { + return err + } + } + } } } @@ -305,6 +316,11 @@ func parseKeywords() (keywords, error) { Name: *keywordContextual, SkipArgs: *skipArgs, } + k[*keywordPluralContextual] = &keywordDef{ + Type: kTypePluralContextual, + Name: *keywordPluralContextual, + SkipArgs: *skipArgs, + } } return k, nil } @@ -361,7 +377,7 @@ msgstr "Project-Id-Version: %s\n" fmt.Fprintf(out, "%s", header) // yes, this is the way to do it in go - sortedKeys := []string{} + var sortedKeys []string for k := range msgIDs { sortedKeys = append(sortedKeys, k) } @@ -415,7 +431,7 @@ func main() { flag.Parse() args := flag.Args() if len(args) == 0 { - fmt.Println("Usage: go-xgettext [options] file1 ...") + fmt.Println("Usage: go-xgettext [options] file1 file2 fileGlobPattern...") fmt.Println("Options:") flag.PrintDefaults() os.Exit(0) diff --git a/go-xgettext/main_test.go b/go-xgettext/main_test.go index eada037..9e4709c 100644 --- a/go-xgettext/main_test.go +++ b/go-xgettext/main_test.go @@ -99,7 +99,7 @@ func main() { c.Assert(err, IsNil) c.Assert(msgIDs, DeepEquals, map[string][]msgID{ - "foo": []msgID{ + "foo": { { comment: "#. TRANSLATORS: foo comment\n", fname: fname, @@ -124,7 +124,7 @@ func main() { c.Assert(err, IsNil) c.Assert(msgIDs, DeepEquals, map[string][]msgID{ - "foo": []msgID{ + "foo": { { comment: "#. TRANSLATORS: foo comment\n", fname: fname, @@ -160,7 +160,7 @@ msgstr "Project-Id-Version: snappy\n" func (s *xgettextTestSuite) TestWriteOutputSimple(c *C) { msgIDs = map[string][]msgID{ - "foo": []msgID{ + "foo": { { fname: "fname", line: 2, @@ -183,7 +183,7 @@ msgstr "" func (s *xgettextTestSuite) TestWriteOutputMultiple(c *C) { msgIDs = map[string][]msgID{ - "foo": []msgID{ + "foo": { { fname: "fname", line: 2, @@ -212,7 +212,7 @@ msgstr "" func (s *xgettextTestSuite) TestWriteOutputNoComment(c *C) { msgIDs = map[string][]msgID{ - "foo": []msgID{ + "foo": { { fname: "fname", line: 2, @@ -233,7 +233,7 @@ msgstr "" func (s *xgettextTestSuite) TestWriteOutputNoLocation(c *C) { msgIDs = map[string][]msgID{ - "foo": []msgID{ + "foo": { { fname: "fname", line: 2, @@ -255,7 +255,7 @@ msgstr "" func (s *xgettextTestSuite) TestWriteOutputFormatHint(c *C) { msgIDs = map[string][]msgID{ - "foo": []msgID{ + "foo": { { fname: "fname", line: 2, @@ -279,7 +279,7 @@ msgstr "" func (s *xgettextTestSuite) TestWriteOutputPlural(c *C) { msgIDs = map[string][]msgID{ - "foo": []msgID{ + "foo": { { msgidPlural: "plural", fname: "fname", @@ -304,13 +304,13 @@ msgstr[1] "" func (s *xgettextTestSuite) TestWriteOutputSorted(c *C) { msgIDs = map[string][]msgID{ - "aaa": []msgID{ + "aaa": { { fname: "fname", line: 2, }, }, - "zzz": []msgID{ + "zzz": { { fname: "fname", line: 2, @@ -411,7 +411,7 @@ func main() { c.Assert(err, IsNil) c.Assert(msgIDs, DeepEquals, map[string][]msgID{ - "foo\\nbar\\nbaz": []msgID{ + "foo\\nbar\\nbaz": { { comment: "#. TRANSLATORS: foo comment\n", fname: fname, @@ -446,7 +446,7 @@ msgstr "" func (s *xgettextTestSuite) TestWriteOutputMultilines(c *C) { msgIDs = map[string][]msgID{ - "foo\\nbar\\nbaz": []msgID{ + "foo\\nbar\\nbaz": { { fname: "fname", line: 2, @@ -470,13 +470,13 @@ msgstr "" func (s *xgettextTestSuite) TestWriteOutputTidy(c *C) { msgIDs = map[string][]msgID{ - "foo\\nbar\\nbaz": []msgID{ + "foo\\nbar\\nbaz": { { fname: "fname", line: 2, }, }, - "zzz\\n": []msgID{ + "zzz\\n": { { fname: "fname", line: 4,