-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkeys.go
191 lines (153 loc) · 4.81 KB
/
keys.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package localization
import (
"errors"
"os"
"strings"
"sync"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
)
const (
//
// Section: Core
//
CoreOfKin = "core.ofKin"
CoreKin = "core.kin"
//
// Section: Pushes
//
PushTitleDepositReceived = "push.title.depositReceived"
PushSubtitleDepositReceived = "push.subtitle.depositReceived"
PushTitleKinReturned = "push.title.kinReturned"
PushSubtitleKinReturned = "push.subtitle.kinReturned"
PushTitleTwitterAccountConnected = "push.title.twitterAccountConnected"
PushSubtitleTwitterAccountConnected = "push.subtitle.twitterAccountConnected"
//
// Section: Chats
//
// Chat Titles
ChatTitleCashTransactions = "title.chat.cashTransactions"
ChatTitleCodeTeam = "title.chat.codeTeam"
ChatTitleKinPurchases = "title.chat.kinPurchases"
ChatTitlePayments = "title.chat.payments"
ChatTitleTips = "title.chat.tips"
// Message Bodies
ChatMessageReferralBonus = "subtitle.chat.referralBonus"
ChatMessageWelcomeBonus = "subtitle.chat.welcomeBonus"
ChatMessageUsdcDeposited = "subtitle.chat.usdcDeposited"
ChatMessageUsdcBeingConverted = "subtitle.chat.usdcBeingConverted"
ChatMessageKinAvailableForUse = "subtitle.chat.kinAvailableForUse"
//
// Verbs
//
VerbGave = "subtitle.youGave"
VerbReceived = "subtitle.youReceived"
VerbWithdrew = "subtitle.youWithdrew"
VerbDeposited = "subtitle.youDeposited"
VerbSent = "subtitle.youSent"
VerbSpent = "subtitle.youSpent"
VerbPaid = "subtitle.youPaid"
VerbPurchased = "subtitle.youPurchased"
VerbReturned = "subtitle.wasReturnedToYou"
VerbReceivedTip = "subtitle.someoneTippedYou"
VerbSentTip = "subtitle.youTipped"
)
var (
bundleMu sync.RWMutex
bundle *i18n.Bundle
defaultLocale = language.English
)
// LoadKeys loads localization key-value pairs from the provided directory.
//
// todo: we'll want to improve this, but just getting something quick up to setup his localization package.
func LoadKeys(directory string) error {
if !strings.HasSuffix(directory, "/") {
directory = directory + "/"
}
bundleMu.Lock()
defer bundleMu.Unlock()
newBundle := i18n.NewBundle(defaultLocale)
dirEntries, err := os.ReadDir(directory)
if err != nil {
return err
}
for _, dirEntry := range dirEntries {
if !dirEntry.IsDir() && strings.HasSuffix(dirEntry.Name(), ".json") {
_, err = newBundle.LoadMessageFile(directory + dirEntry.Name())
if err != nil {
return err
}
}
}
bundle = newBundle
return nil
}
// LoadTestKeys is a utility for injecting test localization keys
func LoadTestKeys(kvsByLocale map[language.Tag]map[string]string) {
bundleMu.Lock()
defer bundleMu.Unlock()
newBundle := i18n.NewBundle(language.English)
for locale, kvs := range kvsByLocale {
messages := make([]*i18n.Message, 0)
for k, v := range kvs {
messages = append(messages, &i18n.Message{
ID: k,
Other: v,
})
}
newBundle.AddMessages(locale, messages...)
}
bundle = newBundle
}
// ResetKeys resets localization to an empty mapping
func ResetKeys() {
bundleMu.Lock()
defer bundleMu.Unlock()
bundle = i18n.NewBundle(language.English)
}
func localizeKey(locale language.Tag, key string, args ...string) (string, *language.Tag, error) {
bundleMu.RLock()
defer bundleMu.RUnlock()
if bundle == nil {
return "", nil, errors.New("localization bundle not configured")
}
localizeConfigInvite := i18n.LocalizeConfig{
MessageID: key,
}
localizer := i18n.NewLocalizer(bundle, locale.String())
localized, tag, err := localizer.LocalizeWithTag(&localizeConfigInvite)
switch err.(type) {
case *i18n.MessageNotFoundErr:
// Fall back to default locale if the key doesn't exist for the requested
// locale
localizer := i18n.NewLocalizer(bundle, defaultLocale.String())
localized, tag, err = localizer.LocalizeWithTag(&localizeConfigInvite)
if err != nil {
return "", nil, err
}
case nil:
default:
return "", nil, err
}
for _, arg := range args {
localized = strings.Replace(localized, "%@", arg, 1)
}
return localized, &tag, nil
}
// Localize localizes a key to the corresponding string in the provided locale.
// An optional set of string parameters can be provided to be replaced in the string.
// Currenctly, these arguments must be localized outside of this function.
//
// todo: Generic argument handling, so all localization can happen in here
func Localize(locale language.Tag, key string, args ...string) (string, error) {
localized, _, err := localizeKey(locale, key, args...)
return localized, err
}
// LocalizeWithFallback is like Localize, but returns defaultValue on error.
func LocalizeWithFallback(locale language.Tag, defaultValue, key string, args ...string) string {
localized, _, err := localizeKey(locale, key, args...)
if err != nil {
return defaultValue
}
return localized
}