Debian Codegraph @

cli/xgotext/parser/domain.go

raw
1 package parser
2
3 import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "sort"
8 "strings"
9 )
10
11 // Translation for a text to translate
12 type Translation struct {
13 MsgId string
14 MsgIdPlural string
15 Context string
16 SourceLocations []string
17 }
18
19 // AddLocations to translation
20 func (t *Translation) AddLocations(locations []string) {
21 if t.SourceLocations == nil {
22 t.SourceLocations = locations
23 } else {
24 t.SourceLocations = append(t.SourceLocations, locations...)
25 }
26 }
27
28 // Dump translation as string
29 func (t *Translation) Dump() string {
30 data := make([]string, 0, len(t.SourceLocations)+5)
31
32 for _, location := range t.SourceLocations {
33 data = append(data, "#: "+location)
34 }
35
36 if t.Context != "" {
37 data = append(data, "msgctxt "+t.Context)
38 }
39
40 data = append(data, "msgid "+t.MsgId)
41
42 if t.MsgIdPlural == "" {
43 data = append(data, "msgstr \"\"")
44 } else {
45 data = append(data,
46 "msgid_plural "+t.MsgIdPlural,
47 "msgstr[0] \"\"",
48 "msgstr[1] \"\"")
49 }
50
51 return strings.Join(data, "\n")
52 }
53
54 // TranslationMap contains a map of translations with the ID as key
55 type TranslationMap map[string]*Translation
56
57 // Dump the translation map as string
58 func (m TranslationMap) Dump() string {
59 // sort by translation id for consistence output
60 keys := make([]string, 0, len(m))
61 for k := range m {
62 keys = append(keys, k)
63 }
64 sort.Strings(keys)
65
66 data := make([]string, 0, len(m))
67 for _, key := range keys {
68 data = append(data, (m)[key].Dump())
69 }
70 return strings.Join(data, "\n\n")
71 }
72
73 // Domain holds all translations of one domain
74 type Domain struct {
75 Translations TranslationMap
76 ContextTranslations map[string]TranslationMap
77 }
78
79 // AddTranslation to the domain
80 func (d *Domain) AddTranslation(translation *Translation) {
81 if d.Translations == nil {
82 d.Translations = make(TranslationMap)
83 d.ContextTranslations = make(map[string]TranslationMap)
84 }
85
86 if translation.Context == "" {
87 if t, ok := d.Translations[translation.MsgId]; ok {
88 t.AddLocations(translation.SourceLocations)
89 } else {
90 d.Translations[translation.MsgId] = translation
91 }
92 } else {
93 if _, ok := d.ContextTranslations[translation.Context]; !ok {
94 d.ContextTranslations[translation.Context] = make(TranslationMap)
95 }
96
97 if t, ok := d.ContextTranslations[translation.Context][translation.MsgId]; ok {
98 t.AddLocations(translation.SourceLocations)
99 } else {
100 d.ContextTranslations[translation.Context][translation.MsgId] = translation
101 }
102 }
103 }
104
105 // Dump the domain as string
106 func (d *Domain) Dump() string {
107 data := make([]string, 0, len(d.ContextTranslations)+1)
108 data = append(data, d.Translations.Dump())
109
110 // sort context translations by context for consistence output
111 keys := make([]string, 0, len(d.ContextTranslations))
112 for k := range d.ContextTranslations {
113 keys = append(keys, k)
114 }
115 sort.Strings(keys)
116
117 for _, key := range keys {
118 data = append(data, d.ContextTranslations[key].Dump())
119 }
120 return strings.Join(data, "\n\n")
121 }
122
123 // Save domain to file
124 func (d *Domain) Save(path string) error {
125 file, err := os.Create(path)
126 if err != nil {
127 return fmt.Errorf("failed to domain: %v", err)
128 }
129 defer file.Close()
130
131 // write header
132 _, err = file.WriteString(`msgid ""
133 msgstr ""
134 "Plural-Forms: nplurals=2; plural=(n != 1);\n"
135 "MIME-Version: 1.0\n"
136 "Content-Type: text/plain; charset=UTF-8\n"
137 "Content-Transfer-Encoding: 8bit\n"
138 "Language: \n"
139 "X-Generator: xgotext\n"
140
141 `)
142 if err != nil {
143 return err
144 }
145
146 // write domain content
147 _, err = file.WriteString(d.Dump())
148 return err
149 }
150
151 // DomainMap contains multiple domains as map with name as key
152 type DomainMap struct {
153 Domains map[string]*Domain
154 Default string
155 }
156
157 // AddTranslation to domain map
158 func (m *DomainMap) AddTranslation(domain string, translation *Translation) {
159 if m.Domains == nil {
160 m.Domains = make(map[string]*Domain, 1)
161 }
162
163 // use "default" as default domain if not set
164 if m.Default == "" {
165 m.Default = "default"
166 }
167
168 // no domain given -> use default
169 if domain == "" {
170 domain = m.Default
171 }
172
173 if _, ok := m.Domains[domain]; !ok {
174 m.Domains[domain] = new(Domain)
175 }
176 m.Domains[domain].AddTranslation(translation)
177 }
178
179 // Save domains to directory
180 func (m *DomainMap) Save(directory string) error {
181 // ensure output directory exist
182 err := os.MkdirAll(directory, os.ModePerm)
183 if err != nil {
184 return fmt.Errorf("failed to create output dir: %v", err)
185 }
186
187 // save each domain in a separate po file
188 for name, domain := range m.Domains {
189 err := domain.Save(filepath.Join(directory, name+".pot"))
190 if err != nil {
191 return fmt.Errorf("failed to save domain %s: %v", name, err)
192 }
193 }
194 return nil
195 }