added option to set default domain

This commit is contained in:
Benjamin Böhmke
2020-02-24 17:52:39 +01:00
parent 9091772533
commit 478f4d29b7
4 changed files with 37 additions and 18 deletions

View File

@@ -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)

View File

@@ -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)
}

View File

@@ -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
}