-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.go
90 lines (73 loc) · 1.95 KB
/
i18n.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
package i18n
import (
"bufio"
"errors"
"fmt"
"os"
"regexp"
"strconv"
)
type TranslationFunction func(string, ...interface{}) string
var T TranslationFunction
func Tfunc(translationFile string) (TranslationFunction, error) {
file, fileScanner, err := loadFile(translationFile)
if err != nil {
return nil, err
}
defer file.Close()
translations, err := parseFile(fileScanner)
if err != nil {
return nil, err
}
if scannerErr := fileScanner.Err(); scannerErr != nil {
return nil, scannerErr
}
return func(key string, args ...interface{}) string {
if translation, ok := translations[key]; ok {
return fmt.Sprintf(translation, args...)
}
return key
}, nil
}
func SetGlobalT(f TranslationFunction) {
T = f
}
func GlobalTfunc(translationFile string) error {
t, err := Tfunc(translationFile)
if err != nil {
return err
}
SetGlobalT(t)
return nil
}
func parseFile(scanner *bufio.Scanner) (map[string]string, error) {
translations := make(map[string]string)
definitionRegexp := regexp.MustCompile(`^([\d\w\-_]+)\s*=\s*(".*")\s*(?:\#.*)?$`)
emptyLineRegexp := regexp.MustCompile(`^(|\s*(\#.*)?)$`)
for lineNumber := 1; scanner.Scan(); lineNumber++ {
line := scanner.Text()
if emptyLineRegexp.MatchString(line) {
continue
}
matches := definitionRegexp.FindStringSubmatch(line)
if len(matches) != 3 {
return nil, errors.New(fmt.Sprintf("Malformed line :%d (%q)", lineNumber, line))
}
unquoted, err := strconv.Unquote(matches[2])
if err != nil {
return nil, errors.New(fmt.Sprintf("Malformed string :%d (%q)", lineNumber, line))
}
if _, ok := translations[matches[1]]; ok {
return nil, errors.New(fmt.Sprintf("Multiple definitions of key %q", matches[1]))
}
translations[matches[1]] = unquoted
}
return translations, nil
}
func loadFile(filePath string) (*os.File, *bufio.Scanner, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, nil, err
}
return file, bufio.NewScanner(file), nil
}