-
Notifications
You must be signed in to change notification settings - Fork 26
/
fs_json.go
66 lines (58 loc) · 1.56 KB
/
fs_json.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
// Copyright 2020 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gettext
import (
"encoding/json"
"fmt"
"sort"
)
type jsonFS struct {
name string
x map[string]struct {
LC_MESSAGES map[string][]struct {
MsgContext string `json:"msgctxt"` // msgctxt context
MsgId string `json:"msgid"` // msgid untranslated-string
MsgIdPlural string `json:"msgid_plural"` // msgid_plural untranslated-string-plural
MsgStr []string `json:"msgstr"` // msgstr translated-string
}
LC_RESOURCE map[string]map[string]string
}
}
func isJsonData() bool {
return false
}
func newJson(jsonData []byte, name string) (*jsonFS, error) {
p := &jsonFS{name: name}
if err := json.Unmarshal(jsonData, &p.x); err != nil {
return nil, err
}
return p, nil
}
func (p *jsonFS) LocaleList() []string {
var ss []string
for lang := range p.x {
ss = append(ss, lang)
}
sort.Strings(ss)
return ss
}
func (p *jsonFS) LoadMessagesFile(domain, lang, ext string) ([]byte, error) {
if v, ok := p.x[lang]; ok {
if v, ok := v.LC_MESSAGES[domain+ext]; ok {
return json.Marshal(v)
}
}
return nil, fmt.Errorf("not found")
}
func (p *jsonFS) LoadResourceFile(domain, lang, name string) ([]byte, error) {
if v, ok := p.x[lang]; ok {
if v, ok := v.LC_RESOURCE[domain]; ok {
return []byte(v[name]), nil
}
}
return nil, fmt.Errorf("not found")
}
func (p *jsonFS) String() string {
return "gettext.nilfs(" + p.name + ")"
}