-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmo_file.go
More file actions
79 lines (68 loc) · 1.53 KB
/
Copy pathmo_file.go
File metadata and controls
79 lines (68 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package pogo
import (
"io"
"strings"
)
const (
ctxtSep = "\x04"
pluralSep = "\x00"
)
// MOFile represents an po file
type MOFile struct {
Header
Entries map[string][]string
}
// ReadMOFile from reader
func ReadMOFile(r io.Reader) (*MOFile, error) {
mr := moReader{r: r}
if err := mr.Read(); err != nil {
return nil, err
}
file := &MOFile{
Entries: make(map[string][]string, mr.N),
}
for i := 0; i < int(mr.N); i++ {
id := mr.Originals[i]
str := mr.Translations[i]
if id == "" {
file.Header.parseEntryMsgStr(str)
} else {
file.Entries[id] = strings.Split(str, pluralSep)
}
}
return file, nil
}
// Print file to writer
func (file *MOFile) Write(w io.Writer) error {
mw := moWriter{w: w, file: file}
return mw.Write()
}
// Get translation by original
func (file *MOFile) Get(msg string) string {
return file.Entries[msg][0]
}
// GetN plural translation by original
func (file *MOFile) GetN(msg, plural string, n int) string {
i := file.PluralForms.Eval(n)
id := msg + pluralSep + plural
forms := file.Entries[id]
if i >= len(forms) {
return ""
}
return forms[i]
}
// GetCtxt translation by original and context
func (file *MOFile) GetCtxt(msg, ctxt string) string {
id := ctxt + ctxtSep + msg
return file.Entries[id][0]
}
// GetCtxtN plural translation by original and context
func (file *MOFile) GetCtxtN(msg, plural, ctxt string, n int) string {
i := file.PluralForms.Eval(n)
id := ctxt + ctxtSep + msg + pluralSep + plural
forms := file.Entries[id]
if i >= len(forms) {
return ""
}
return forms[i]
}