-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpot.go
75 lines (67 loc) · 1.78 KB
/
pot.go
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
package translate
import (
"fmt"
"strings"
)
var prefix = "# Translations template for PROJECT.\n" +
"# Copyright (C) 2018 ORGANIZATION\n" +
"# This file is distributed under the same license as the PROJECT project.\n" +
"# FIRST AUTHOR <EMAIL@ADDRESS>, 2018.\n" +
"#\n" +
"#, fuzzy\n" +
"msgid \"\"\n" +
"msgstr \"\"\n" +
"\"Project-Id-Version: PROJECT VERSION\"\n" +
"\"Report-Msgid-Bugs-To: EMAIL@ADDRESS\"\n" +
"\"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\"\n" +
"\"Last-Translator: FULL NAME <EMAIL@ADDRESS>\"\n" +
"\"Language-Team: LANGUAGE <[email protected]>\"\n" +
"\"MIME-Version: 1.0\"\n" +
"\"Content-Type: text/plain; charset=utf-8\"\n" +
"\"Content-Transfer-Encoding: 8bit\"\n" +
"\"Generated-By: go-translate 0.1\"\n\n"
type POT struct {
translations []POTTranslation
}
type POTTranslation struct {
original string
translation string
}
func (pot *POT) AddTrans(defaultText, transText string) {
pot.translations = append(pot.translations, POTTranslation{defaultText, transText})
}
func (pot *POT) idLines(id string) []string {
parts := strings.Split(id, "\n")
if len(parts) == 1 {
return parts
}
lines := []string{""}
for i, part := range parts {
if i != len(parts)-1 {
part += "\\n"
}
lines = append(lines, part)
}
return lines
}
func (pot *POT) ToString() (string, error) {
content := prefix
first := true
for _, trans := range pot.translations {
if first {
first = false
} else {
content += "\n"
}
jinjaTrans := NewJinjaTranslation(trans.original)
if jinjaTrans.hasVars {
content += "#, python-format\n"
}
content += fmt.Sprintf("msgid ")
for _, line := range pot.idLines(jinjaTrans.toString()) {
content += fmt.Sprintf("\"%s\"\n", line)
}
content += fmt.Sprintf("msgtxt \"%s\"\n", trans.translation)
}
return content, nil
}