|
| 1 | +package gotext |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "encoding/gob" |
| 7 | + "net/textproto" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + "sync" |
| 11 | + |
| 12 | + "golang.org/x/text/language" |
| 13 | + |
| 14 | + "github.com/leonelquinteros/gotext/plurals" |
| 15 | +) |
| 16 | + |
| 17 | +// Domain has all the common functions for dealing with a gettext domain |
| 18 | +// it's initialized with a GettextFile (which represents either a Po or Mo file) |
| 19 | +type Domain struct { |
| 20 | + Headers textproto.MIMEHeader |
| 21 | + |
| 22 | + // Language header |
| 23 | + Language string |
| 24 | + tag language.Tag |
| 25 | + |
| 26 | + // Plural-Forms header |
| 27 | + PluralForms string |
| 28 | + |
| 29 | + // Parsed Plural-Forms header values |
| 30 | + nplurals int |
| 31 | + plural string |
| 32 | + pluralforms plurals.Expression |
| 33 | + |
| 34 | + // Storage |
| 35 | + translations map[string]*Translation |
| 36 | + contexts map[string]map[string]*Translation |
| 37 | + pluralTranslations map[string]*Translation |
| 38 | + |
| 39 | + // Sync Mutex |
| 40 | + trMutex sync.RWMutex |
| 41 | + pluralMutex sync.RWMutex |
| 42 | + |
| 43 | + // Parsing buffers |
| 44 | + trBuffer *Translation |
| 45 | + ctxBuffer string |
| 46 | +} |
| 47 | + |
| 48 | +func NewDomain() *Domain { |
| 49 | + domain := new(Domain) |
| 50 | + |
| 51 | + domain.translations = make(map[string]*Translation) |
| 52 | + domain.contexts = make(map[string]map[string]*Translation) |
| 53 | + domain.pluralTranslations = make(map[string]*Translation) |
| 54 | + |
| 55 | + return domain |
| 56 | +} |
| 57 | + |
| 58 | +func (do *Domain) pluralForm(n int) int { |
| 59 | + // do we really need locking here? not sure how this plurals.Expression works, so sticking with it for now |
| 60 | + do.pluralMutex.RLock() |
| 61 | + defer do.pluralMutex.RUnlock() |
| 62 | + |
| 63 | + // Failure fallback |
| 64 | + if do.pluralforms == nil { |
| 65 | + /* Use the Germanic plural rule. */ |
| 66 | + if n == 1 { |
| 67 | + return 0 |
| 68 | + } |
| 69 | + return 1 |
| 70 | + } |
| 71 | + return do.pluralforms.Eval(uint32(n)) |
| 72 | +} |
| 73 | + |
| 74 | +// parseHeaders retrieves data from previously parsed headers. it's called by both Mo and Po when parsing |
| 75 | +func (do *Domain) parseHeaders() { |
| 76 | + // Make sure we end with 2 carriage returns. |
| 77 | + empty := "" |
| 78 | + if _, ok := do.translations[empty]; ok { |
| 79 | + empty = do.translations[empty].Get() |
| 80 | + } |
| 81 | + raw := empty + "\n\n" |
| 82 | + |
| 83 | + // Read |
| 84 | + reader := bufio.NewReader(strings.NewReader(raw)) |
| 85 | + tp := textproto.NewReader(reader) |
| 86 | + |
| 87 | + var err error |
| 88 | + |
| 89 | + do.Headers, err = tp.ReadMIMEHeader() |
| 90 | + if err != nil { |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + // Get/save needed headers |
| 95 | + do.Language = do.Headers.Get("Language") |
| 96 | + do.tag = language.Make(do.Language) |
| 97 | + do.PluralForms = do.Headers.Get("Plural-Forms") |
| 98 | + |
| 99 | + // Parse Plural-Forms formula |
| 100 | + if do.PluralForms == "" { |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + // Split plural form header value |
| 105 | + pfs := strings.Split(do.PluralForms, ";") |
| 106 | + |
| 107 | + // Parse values |
| 108 | + for _, i := range pfs { |
| 109 | + vs := strings.SplitN(i, "=", 2) |
| 110 | + if len(vs) != 2 { |
| 111 | + continue |
| 112 | + } |
| 113 | + |
| 114 | + switch strings.TrimSpace(vs[0]) { |
| 115 | + case "nplurals": |
| 116 | + do.nplurals, _ = strconv.Atoi(vs[1]) |
| 117 | + |
| 118 | + case "plural": |
| 119 | + do.plural = vs[1] |
| 120 | + |
| 121 | + if expr, err := plurals.Compile(do.plural); err == nil { |
| 122 | + do.pluralforms = expr |
| 123 | + } |
| 124 | + |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func (do *Domain) Get(str string, vars ...interface{}) string { |
| 130 | + // Sync read |
| 131 | + do.trMutex.RLock() |
| 132 | + defer do.trMutex.RUnlock() |
| 133 | + |
| 134 | + if do.translations != nil { |
| 135 | + if _, ok := do.translations[str]; ok { |
| 136 | + return Printf(do.translations[str].Get(), vars...) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // Return the same we received by default |
| 141 | + return Printf(str, vars...) |
| 142 | +} |
| 143 | + |
| 144 | +// GetN retrieves the (N)th plural form of Translation for the given string. |
| 145 | +// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax. |
| 146 | +func (do *Domain) GetN(str, plural string, n int, vars ...interface{}) string { |
| 147 | + // Sync read |
| 148 | + do.trMutex.RLock() |
| 149 | + defer do.trMutex.RUnlock() |
| 150 | + |
| 151 | + if do.translations != nil { |
| 152 | + if _, ok := do.translations[str]; ok { |
| 153 | + return Printf(do.translations[str].GetN(do.pluralForm(n)), vars...) |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + // Parse plural forms to distinguish between plural and singular |
| 158 | + if do.pluralForm(n) == 0 { |
| 159 | + return Printf(str, vars...) |
| 160 | + } |
| 161 | + return Printf(plural, vars...) |
| 162 | +} |
| 163 | + |
| 164 | +// GetC retrieves the corresponding Translation for a given string in the given context. |
| 165 | +// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax. |
| 166 | +func (do *Domain) GetC(str, ctx string, vars ...interface{}) string { |
| 167 | + do.trMutex.RLock() |
| 168 | + defer do.trMutex.RUnlock() |
| 169 | + |
| 170 | + if do.contexts != nil { |
| 171 | + if _, ok := do.contexts[ctx]; ok { |
| 172 | + if do.contexts[ctx] != nil { |
| 173 | + if _, ok := do.contexts[ctx][str]; ok { |
| 174 | + return Printf(do.contexts[ctx][str].Get(), vars...) |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + // Return the string we received by default |
| 181 | + return Printf(str, vars...) |
| 182 | +} |
| 183 | + |
| 184 | +// GetNC retrieves the (N)th plural form of Translation for the given string in the given context. |
| 185 | +// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax. |
| 186 | +func (do *Domain) GetNC(str, plural string, n int, ctx string, vars ...interface{}) string { |
| 187 | + do.trMutex.RLock() |
| 188 | + defer do.trMutex.RUnlock() |
| 189 | + |
| 190 | + if do.contexts != nil { |
| 191 | + if _, ok := do.contexts[ctx]; ok { |
| 192 | + if do.contexts[ctx] != nil { |
| 193 | + if _, ok := do.contexts[ctx][str]; ok { |
| 194 | + return Printf(do.contexts[ctx][str].GetN(do.pluralForm(n)), vars...) |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + if n == 1 { |
| 201 | + return Printf(str, vars...) |
| 202 | + } |
| 203 | + return Printf(plural, vars...) |
| 204 | +} |
| 205 | + |
| 206 | +// MarshalBinary implements encoding.BinaryMarshaler interface |
| 207 | +func (do *Domain) MarshalBinary() ([]byte, error) { |
| 208 | + obj := new(TranslatorEncoding) |
| 209 | + obj.Headers = do.Headers |
| 210 | + obj.Language = do.Language |
| 211 | + obj.PluralForms = do.PluralForms |
| 212 | + obj.Nplurals = do.nplurals |
| 213 | + obj.Plural = do.plural |
| 214 | + obj.Translations = do.translations |
| 215 | + obj.Contexts = do.contexts |
| 216 | + |
| 217 | + var buff bytes.Buffer |
| 218 | + encoder := gob.NewEncoder(&buff) |
| 219 | + err := encoder.Encode(obj) |
| 220 | + |
| 221 | + return buff.Bytes(), err |
| 222 | +} |
| 223 | + |
| 224 | +// UnmarshalBinary implements encoding.BinaryUnmarshaler interface |
| 225 | +func (do *Domain) UnmarshalBinary(data []byte) error { |
| 226 | + buff := bytes.NewBuffer(data) |
| 227 | + obj := new(TranslatorEncoding) |
| 228 | + |
| 229 | + decoder := gob.NewDecoder(buff) |
| 230 | + err := decoder.Decode(obj) |
| 231 | + if err != nil { |
| 232 | + return err |
| 233 | + } |
| 234 | + |
| 235 | + do.Headers = obj.Headers |
| 236 | + do.Language = obj.Language |
| 237 | + do.PluralForms = obj.PluralForms |
| 238 | + do.nplurals = obj.Nplurals |
| 239 | + do.plural = obj.Plural |
| 240 | + do.translations = obj.Translations |
| 241 | + do.contexts = obj.Contexts |
| 242 | + |
| 243 | + if expr, err := plurals.Compile(do.plural); err == nil { |
| 244 | + do.pluralforms = expr |
| 245 | + } |
| 246 | + |
| 247 | + return nil |
| 248 | +} |
0 commit comments