From 478f4d29b7463614ffad780708de9798ba866b7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20B=C3=B6hmke?= Date: Mon, 24 Feb 2020 17:52:39 +0100 Subject: [PATCH] added option to set default domain --- cli/xgotext/main.go | 11 ++++++++--- cli/xgotext/parser/domain.go | 27 ++++++++++++++++++++++----- cli/xgotext/parser/golang.go | 8 +++----- cli/xgotext/parser/parser.go | 9 ++++----- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/cli/xgotext/main.go b/cli/xgotext/main.go index 0ff16b6..17ef313 100644 --- a/cli/xgotext/main.go +++ b/cli/xgotext/main.go @@ -8,8 +8,9 @@ import ( ) var ( - dirName = flag.String("in", "", "input dir: /path/to/go/pkg") - outputDir = flag.String("out", "", "output dir: /path/to/i18n/files") + 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") ) func main() { @@ -25,7 +26,11 @@ func main() { log.Fatal("No output directory given") } - data, err := parser.ParseDirRec(*dirName) + data := &parser.DomainMap{ + Default: *defaultDomain, + } + + err := parser.ParseDirRec(*dirName, data) if err != nil { log.Fatal(err) } diff --git a/cli/xgotext/parser/domain.go b/cli/xgotext/parser/domain.go index 86a9bef..08c1765 100644 --- a/cli/xgotext/parser/domain.go +++ b/cli/xgotext/parser/domain.go @@ -149,14 +149,31 @@ msgstr "" } // DomainMap contains multiple domains as map with name as key -type DomainMap map[string]*Domain +type DomainMap struct { + Domains map[string]*Domain + Default string +} // AddTranslation to domain map func (m *DomainMap) AddTranslation(domain string, translation *Translation) { - if _, ok := (*m)[domain]; !ok { - (*m)[domain] = new(Domain) + if m.Domains == nil { + m.Domains = make(map[string]*Domain, 1) } - (*m)[domain].AddTranslation(translation) + + // use "default" as default domain if not set + if m.Default == "" { + m.Default = "default" + } + + // no domain given -> use default + if domain == "" { + domain = m.Default + } + + if _, ok := m.Domains[domain]; !ok { + m.Domains[domain] = new(Domain) + } + m.Domains[domain].AddTranslation(translation) } // Save domains to directory @@ -168,7 +185,7 @@ func (m *DomainMap) Save(directory string) error { } // save each domain in a separate po file - for name, domain := range *m { + for name, domain := range m.Domains { err := domain.Save(filepath.Join(directory, name+".po")) 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 ec535dc..62fbdd9 100644 --- a/cli/xgotext/parser/golang.go +++ b/cli/xgotext/parser/golang.go @@ -53,7 +53,7 @@ func init() { } // parse go package -func goParser(dirPath, basePath string, data DomainMap) error { +func goParser(dirPath, basePath string, data *DomainMap) error { fileSet := token.NewFileSet() conf := packages.Config{ @@ -100,7 +100,7 @@ func goParser(dirPath, basePath string, data DomainMap) error { type GoFile struct { filePath string basePath string - data DomainMap + data *DomainMap fileSet *token.FileSet pkgConf *packages.Config @@ -234,9 +234,7 @@ func (g *GoFile) parseGetter(def GetterDef, args []*ast.BasicLit, pos string) { // get domain var domain string - if def.Domain == -1 { - domain = "default" // TODO - } else { + if def.Domain != -1 { domain, _ = strconv.Unquote(args[def.Domain].Value) } diff --git a/cli/xgotext/parser/parser.go b/cli/xgotext/parser/parser.go index de01f20..9f12e1b 100644 --- a/cli/xgotext/parser/parser.go +++ b/cli/xgotext/parser/parser.go @@ -6,7 +6,7 @@ import ( ) // ParseDirFunc parses one directory -type ParseDirFunc func(filePath, basePath string, data DomainMap) error +type ParseDirFunc func(filePath, basePath string, data *DomainMap) error var knownParser []ParseDirFunc @@ -20,7 +20,7 @@ func AddParser(parser ParseDirFunc) { } // ParseDir calls all known parser for each directory -func ParseDir(dirPath, basePath string, data DomainMap) error { +func ParseDir(dirPath, basePath string, data *DomainMap) error { dirPath, _ = filepath.Abs(dirPath) basePath, _ = filepath.Abs(basePath) @@ -34,8 +34,7 @@ func ParseDir(dirPath, basePath string, data DomainMap) error { } // ParseDirRec calls all known parser for each directory -func ParseDirRec(dirPath string) (DomainMap, error) { - data := make(DomainMap) +func ParseDirRec(dirPath string, data *DomainMap) error { dirPath, _ = filepath.Abs(dirPath) err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error { @@ -51,5 +50,5 @@ func ParseDirRec(dirPath string) (DomainMap, error) { } return nil }) - return data, err + return err }