-
Notifications
You must be signed in to change notification settings - Fork 26
/
fs_zip.go
142 lines (127 loc) · 3.01 KB
/
fs_zip.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2013 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 (
"archive/zip"
"fmt"
"io/ioutil"
"sort"
"strings"
)
type zipFS struct {
root string
name string
r *zip.Reader
}
func newZipFS(r *zip.Reader, name string) *zipFS {
fs := &zipFS{r: r, name: name}
fs.root = fs.zipRoot()
return fs
}
func (p *zipFS) zipName() string {
name := p.name
if x := strings.LastIndexAny(name, `\/`); x != -1 {
name = name[x+1:]
}
name = strings.TrimSuffix(name, ".zip")
return name
}
func (p *zipFS) zipRoot() string {
var somepath string
for _, f := range p.r.File {
if x := strings.Index(f.Name, "LC_MESSAGES"); x != -1 {
somepath = f.Name
}
if x := strings.Index(f.Name, "LC_RESOURCE"); x != -1 {
somepath = f.Name
}
}
if somepath == "" {
return p.zipName()
}
ss := strings.Split(somepath, "/")
for i, s := range ss {
// $(root)/$(lang)/LC_MESSAGES
// $(root)/$(lang)/LC_RESOURCE
if (s == "LC_MESSAGES" || s == "LC_RESOURCE") && i >= 2 {
return strings.Join(ss[:i-1], "/")
}
}
return p.zipName()
}
func (p *zipFS) LocaleList() []string {
var locals []string
for s := range p.lsZip(p.r) {
locals = append(locals, s)
}
sort.Strings(locals)
return locals
}
func (p *zipFS) LoadMessagesFile(domain, lang, ext string) ([]byte, error) {
trName := p.makeMessagesFileName(domain, lang, ext)
for _, f := range p.r.File {
if f.Name != trName {
continue
}
rc, err := f.Open()
if err != nil {
return nil, err
}
rcData, err := ioutil.ReadAll(rc)
rc.Close()
return rcData, err
}
return nil, fmt.Errorf("not found")
}
func (p *zipFS) LoadResourceFile(domain, lang, name string) ([]byte, error) {
rcName := p.makeResourceFileName(domain, lang, name)
for _, f := range p.r.File {
if f.Name != rcName {
continue
}
rc, err := f.Open()
if err != nil {
return nil, err
}
rcData, err := ioutil.ReadAll(rc)
rc.Close()
return rcData, err
}
return nil, fmt.Errorf("not found")
}
func (p *zipFS) String() string {
return "gettext.zipfs(" + p.name + ")"
}
func (p *zipFS) makeMessagesFileName(domain, lang, ext string) string {
return fmt.Sprintf("%s/%s/LC_MESSAGES/%s%s", p.root, lang, domain, ext)
}
func (p *zipFS) makeResourceFileName(domain, lang, name string) string {
return fmt.Sprintf("%s/%s/LC_RESOURCE/%s/%s", p.root, lang, domain, name)
}
func (p *zipFS) lsZip(r *zip.Reader) map[string]bool {
ssMap := make(map[string]bool)
for _, f := range r.File {
if x := strings.Index(f.Name, "LC_MESSAGES"); x != -1 {
s := strings.TrimRight(f.Name[:x], `\/`)
if x = strings.LastIndexAny(s, `\/`); x != -1 {
s = s[x+1:]
}
if s != "" {
ssMap[s] = true
}
continue
}
if x := strings.Index(f.Name, "LC_RESOURCE"); x != -1 {
s := strings.TrimRight(f.Name[:x], `\/`)
if x = strings.LastIndexAny(s, `\/`); x != -1 {
s = s[x+1:]
}
if s != "" {
ssMap[s] = true
}
continue
}
}
return ssMap
}