moved PO file writing to domain object
This commit is contained in:
@@ -3,8 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/leonelquinteros/gotext/cli/xgotext/parser"
|
"github.com/leonelquinteros/gotext/cli/xgotext/parser"
|
||||||
)
|
)
|
||||||
@@ -32,29 +30,8 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.MkdirAll(*outputDir, os.ModePerm)
|
err = data.Save(*outputDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to create output dir: %s", err)
|
log.Fatal(err)
|
||||||
}
|
|
||||||
|
|
||||||
for name, domain := range data {
|
|
||||||
outFile := filepath.Join(*outputDir, name+".po")
|
|
||||||
file, err := os.Create(outFile)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("failed to save po file for %s: %s", name, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
file.WriteString(`msgid ""
|
|
||||||
msgstr ""
|
|
||||||
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
|
|
||||||
"MIME-Version: 1.0\n"
|
|
||||||
"Content-Type: text/plain; charset=UTF-8\n"
|
|
||||||
"Content-Transfer-Encoding: 8bit\n"
|
|
||||||
"Language: \n"
|
|
||||||
"X-Generator: xgotext\n"
|
|
||||||
|
|
||||||
`)
|
|
||||||
file.WriteString(domain.Dump())
|
|
||||||
file.Close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package parser
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -117,6 +120,34 @@ func (d *Domain) Dump() string {
|
|||||||
return strings.Join(data, "\n\n")
|
return strings.Join(data, "\n\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save domain to file
|
||||||
|
func (d *Domain) Save(path string) error {
|
||||||
|
file, err := os.Create(path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to domain: %w", err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
// write header
|
||||||
|
_, err = file.WriteString(`msgid ""
|
||||||
|
msgstr ""
|
||||||
|
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
|
||||||
|
"MIME-Version: 1.0\n"
|
||||||
|
"Content-Type: text/plain; charset=UTF-8\n"
|
||||||
|
"Content-Transfer-Encoding: 8bit\n"
|
||||||
|
"Language: \n"
|
||||||
|
"X-Generator: xgotext\n"
|
||||||
|
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// write domain content
|
||||||
|
_, err = file.WriteString(d.Dump())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// DomainMap contains multiple domains as map with name as key
|
// DomainMap contains multiple domains as map with name as key
|
||||||
type DomainMap map[string]*Domain
|
type DomainMap map[string]*Domain
|
||||||
|
|
||||||
@@ -127,3 +158,21 @@ func (m *DomainMap) AddTranslation(domain string, translation *Translation) {
|
|||||||
}
|
}
|
||||||
(*m)[domain].AddTranslation(translation)
|
(*m)[domain].AddTranslation(translation)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save domains to directory
|
||||||
|
func (m *DomainMap) Save(directory string) error {
|
||||||
|
// ensure output directory exist
|
||||||
|
err := os.MkdirAll(directory, os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create output dir: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// save each domain in a separate po file
|
||||||
|
for name, domain := range *m {
|
||||||
|
err := domain.Save(filepath.Join(directory, name+".po"))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to save domain %s: %w", name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user