-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.go
115 lines (96 loc) · 2.89 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
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
package i18n
import (
"fmt"
"github.com/robfig/config"
"os"
"path/filepath"
"regexp"
"strings"
)
const messageFilePattern = `^\w+\.[a-zA-Z]{2}$`
var locales = &translator{}
type translator struct {
messages map[string]*config.Config
}
// loadMessages recursively read and cache all available messages from all message files on the given path.
func (c *translator) loadMessages(path string) {
c.messages = make(map[string]*config.Config)
if error := filepath.Walk(path, loadMessageFile); error != nil && !os.IsNotExist(error) {
fmt.Println("Error reading messages files:", error)
}
}
func (c *translator) translate(locale, message string, args []interface{}) string {
language, region := parseLocale(locale)
messageConfig, knownLanguage := c.messages[language]
if !knownLanguage {
fmt.Printf("Unsupported language for locale '%s' and message '%s' \n", locale, message)
return message
}
value, error := messageConfig.String(region, message)
if error != nil {
fmt.Printf("Unknown message '%s' for locale '%s' \n", message, locale)
return message
}
if len(args) > 0 {
value = fmt.Sprintf(value, args...)
}
return value
}
// listLanguages returns all currently loaded message languages.
func (c *translator) listLanguages() []string {
languages := make([]string, len(c.messages))
i := 0
for language, _ := range c.messages {
languages[i] = language
i++
}
return languages
}
// Load a single message file
func loadMessageFile(path string, info os.FileInfo, osError error) error {
if osError != nil {
return osError
}
if info.IsDir() {
return nil
}
if matched, _ := regexp.MatchString(messageFilePattern, info.Name()); matched {
if config, error := config.ReadDefault(path); error != nil {
return error
} else {
locale := parseLocaleFromFileName(info.Name())
// If already parsed a message file for this locale, merge both
if _, exists := locales.messages[locale]; exists {
locales.messages[locale].Merge(config)
} else {
locales.messages[locale] = config
}
}
} else {
fmt.Printf("Ignoring file %s because it did not have a valid extension \n", info.Name())
}
return nil
}
func parseLocaleFromFileName(file string) string {
extension := filepath.Ext(file)[1:]
return strings.ToLower(extension)
}
func parseLocale(locale string) (language, region string) {
if strings.Contains(locale, "-") {
languageAndRegion := strings.Split(locale, "-")
return languageAndRegion[0], languageAndRegion[1]
}
return locale, ""
}
// LoadMessages loads messages from all message files on the given path.
func LoadMessages(path string) {
locales.loadMessages(path)
}
// Translate translates content to target language.
func Translate(locale, message string, args ...interface{}) string {
return locales.translate(locale, message, args)
}
// ListLanguages returns all currently loaded message languages.
func ListLanguages() []string {
return locales.listLanguages()
}