|
| 1 | +package compile |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/Tom5521/gotext-tools/v2/pkg/po" |
| 10 | +) |
| 11 | + |
| 12 | +func escapePOString(s string) string { |
| 13 | + var buf strings.Builder |
| 14 | + for _, r := range s { |
| 15 | + switch r { |
| 16 | + case '"': |
| 17 | + buf.WriteString(`\"`) |
| 18 | + case '\\': |
| 19 | + buf.WriteString(`\\`) |
| 20 | + case '\n': |
| 21 | + buf.WriteString(`\n`) |
| 22 | + case '\t': |
| 23 | + buf.WriteString(`\t`) |
| 24 | + case '\r': |
| 25 | + buf.WriteString(`\r`) |
| 26 | + default: |
| 27 | + if strconv.IsPrint(r) { |
| 28 | + buf.WriteRune(r) |
| 29 | + } else { |
| 30 | + fmt.Fprintf(&buf, "\\x%02x", r) |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + return buf.String() |
| 35 | +} |
| 36 | + |
| 37 | +func (c PoCompiler) compileEntries() { |
| 38 | + for _, e := range c.File.Entries { |
| 39 | + c.entry(e) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func (c PoCompiler) entry(entry po.Entry) { |
| 44 | + eb := EntryBuilder{entry, c.writer, c.Config} |
| 45 | + eb.Build() |
| 46 | +} |
| 47 | + |
| 48 | +type EntryBuilder struct { |
| 49 | + po.Entry |
| 50 | + Builder io.Writer |
| 51 | + Config PoConfig |
| 52 | +} |
| 53 | + |
| 54 | +func (eb EntryBuilder) Build() { |
| 55 | + eb.comment() |
| 56 | + eb.msgid() |
| 57 | + eb.msgstr() |
| 58 | +} |
| 59 | + |
| 60 | +func (eb EntryBuilder) header() {} |
| 61 | +func (eb EntryBuilder) msgid() { |
| 62 | + if eb.HasContext() { |
| 63 | + eb.keyword("msgctxt") |
| 64 | + eb.string(eb.Context) |
| 65 | + } |
| 66 | + eb.keyword("msgid") |
| 67 | + eb.string(eb.ID) |
| 68 | + |
| 69 | + if eb.IsPlural() { |
| 70 | + eb.keyword("msgid_plural") |
| 71 | + eb.string(eb.Plural) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func (eb EntryBuilder) msgstr() { |
| 76 | + const format = "msgstr[%d]" |
| 77 | + if eb.IsPlural() { |
| 78 | + for _, pe := range eb.Plurals { |
| 79 | + eb.keyword(fmt.Sprintf(format, pe.ID)) |
| 80 | + eb.string(pe.Str) |
| 81 | + } |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + eb.keyword("msgstr") |
| 86 | + eb.string(eb.Str) |
| 87 | +} |
| 88 | + |
| 89 | +func (eb EntryBuilder) fuzzy() {} |
| 90 | +func (eb EntryBuilder) obsolete() {} |
| 91 | +func (eb EntryBuilder) translated() {} |
| 92 | +func (eb EntryBuilder) untranslated() {} |
| 93 | + |
| 94 | +func (eb EntryBuilder) comment() { |
| 95 | + eb.translatorComment() |
| 96 | + eb.extractedComment() |
| 97 | + eb.referenceComment() |
| 98 | + eb.flagComment() |
| 99 | + eb.previousComment() |
| 100 | +} |
| 101 | + |
| 102 | +func (eb EntryBuilder) translatorComment() {} |
| 103 | +func (eb EntryBuilder) extractedComment() {} |
| 104 | + |
| 105 | +func (eb EntryBuilder) referenceComment() { |
| 106 | + eb.reference() |
| 107 | +} |
| 108 | + |
| 109 | +func (eb EntryBuilder) reference() {} |
| 110 | + |
| 111 | +func (eb EntryBuilder) flagComment() { |
| 112 | + eb.flag() |
| 113 | +} |
| 114 | + |
| 115 | +func (eb EntryBuilder) flag() { |
| 116 | + eb.fuzzyFlag() |
| 117 | +} |
| 118 | + |
| 119 | +func (eb EntryBuilder) fuzzyFlag() {} |
| 120 | + |
| 121 | +func (eb EntryBuilder) previousComment() { |
| 122 | + eb.previous() |
| 123 | +} |
| 124 | + |
| 125 | +func (eb EntryBuilder) previous() {} |
| 126 | + |
| 127 | +func (eb EntryBuilder) keyword(kw string) { |
| 128 | + fmt.Fprint(eb.Builder, kw+" ") |
| 129 | +} |
| 130 | + |
| 131 | +func (eb EntryBuilder) string(str string) { |
| 132 | + if eb.Config.WordWrap { |
| 133 | + lines := strings.Split(str, "\n") |
| 134 | + for i, line := range lines { |
| 135 | + if i != len(lines)-1 { |
| 136 | + line += "\n" |
| 137 | + } |
| 138 | + fmt.Fprint(eb.Builder, `"`) |
| 139 | + eb.text(escapePOString(line)) |
| 140 | + fmt.Fprintln(eb.Builder, `"`) |
| 141 | + } |
| 142 | + |
| 143 | + return |
| 144 | + } |
| 145 | + fmt.Fprint(eb.Builder, `"`) |
| 146 | + eb.text(escapePOString(str)) |
| 147 | + fmt.Fprintln(eb.Builder, `"`) |
| 148 | +} |
| 149 | + |
| 150 | +func (eb EntryBuilder) text(txt string) { |
| 151 | + fmt.Fprint(eb.Builder, txt) |
| 152 | + /* |
| 153 | + eb.escapeSequence() |
| 154 | + eb.formatDirective() |
| 155 | + eb.invalidFormatDirective() |
| 156 | + */ |
| 157 | +} |
| 158 | + |
| 159 | +func (eb EntryBuilder) escapeSequence() {} |
| 160 | +func (eb EntryBuilder) formatDirective() {} |
| 161 | +func (eb EntryBuilder) invalidFormatDirective() {} |
0 commit comments