-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathmanager.go
More file actions
110 lines (90 loc) · 2.23 KB
/
Copy pathmanager.go
File metadata and controls
110 lines (90 loc) · 2.23 KB
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
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cloud .
package langs
import (
"fmt"
"strings"
)
var defaultManager = NewManager()
type Manager struct {
langMap map[string]*Lang // lang code => *Lang, lang code must be in lowercase
defaultLangCode string
}
func NewManager() *Manager {
return &Manager{
langMap: map[string]*Lang{},
defaultLangCode: "zh-cn",
}
}
func DefaultManager() *Manager {
return defaultManager
}
func (this *Manager) AddLang(code string) *Lang {
var lang = NewLang(code)
this.langMap[code] = lang
return lang
}
func (this *Manager) HasLang(code string) bool {
_, ok := this.langMap[code]
return ok
}
func (this *Manager) GetLang(code string) (lang *Lang, ok bool) {
lang, ok = this.langMap[code]
return
}
func (this *Manager) MatchLang(code string) (matchedCode string) {
// lookup exact match
code = strings.ToLower(code)
_, ok := this.langMap[code]
if ok {
return code
}
// lookup language family, such as en-us, en
if strings.Contains(code, "-") {
code, _, _ = strings.Cut(code, "-")
}
for rawCode := range this.langMap {
if strings.HasPrefix(rawCode, code+"-") { // en-us vs en
return rawCode
}
}
return this.DefaultLang()
}
func (this *Manager) SetDefaultLang(code string) {
this.defaultLangCode = code
}
func (this *Manager) DefaultLang() string {
if len(this.defaultLangCode) > 0 {
return this.defaultLangCode
}
return "zh-cn"
}
// GetMessage
// message: name: %s, age: %d, salary: %.2f
func (this *Manager) GetMessage(langCode string, messageCode MessageCode, args ...any) string {
var lang = this.langMap[langCode]
if lang == nil && len(this.defaultLangCode) > 0 {
lang = this.langMap[this.defaultLangCode]
}
if lang == nil {
return ""
}
var message = lang.Get(messageCode)
if len(message) == 0 {
// try to get message from default lang
if lang.code != this.defaultLangCode {
var defaultLang = this.langMap[this.defaultLangCode]
if defaultLang != nil {
message = defaultLang.Get(messageCode)
if len(args) == 0 {
return message
}
return fmt.Sprintf(message, args...)
}
}
return ""
}
if len(args) == 0 {
return message
}
return fmt.Sprintf(message, args...)
}