-
Notifications
You must be signed in to change notification settings - Fork 26
/
fs.go
84 lines (72 loc) · 1.68 KB
/
fs.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
// 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"
"bytes"
"fmt"
)
type FileSystem interface {
LocaleList() []string
LoadMessagesFile(domain, lang, ext string) ([]byte, error)
LoadResourceFile(domain, lang, name string) ([]byte, error)
String() string
}
func NewFS(name string, x interface{}) FileSystem {
if x == nil {
if name != "" {
return OS(name)
}
return NilFS(name)
}
switch x := x.(type) {
case []byte:
if len(x) == 0 {
return OS(name)
}
if r, err := zip.NewReader(bytes.NewReader(x), int64(len(x))); err == nil {
return ZipFS(r, name)
}
if fs, err := newJson(x, name); err == nil {
return fs
}
case string:
if len(x) == 0 {
return OS(name)
}
if r, err := zip.NewReader(bytes.NewReader([]byte(x)), int64(len(x))); err == nil {
return ZipFS(r, name)
}
if fs, err := newJson([]byte(x), name); err == nil {
return fs
}
case FileSystem:
return x
}
return NilFS(name)
}
func OS(root string) FileSystem {
return newOsFS(root)
}
func ZipFS(r *zip.Reader, name string) FileSystem {
return newZipFS(r, name)
}
func NilFS(name string) FileSystem {
return &nilFS{name}
}
type nilFS struct {
name string
}
func (p *nilFS) LocaleList() []string {
return nil
}
func (p *nilFS) LoadMessagesFile(domain, lang, ext string) ([]byte, error) {
return nil, fmt.Errorf("not found")
}
func (p *nilFS) LoadResourceFile(domain, lang, name string) ([]byte, error) {
return nil, fmt.Errorf("not found")
}
func (p *nilFS) String() string {
return "gettext.nilfs(" + p.name + ")"
}