-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
locales.go
54 lines (45 loc) · 1.17 KB
/
locales.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
// Package localescompressed compresses and wraps all translators in github.com/gohugoio/locales.
// The translators are not created until asked for in Get.
package localescompressed
import (
"strings"
"sync"
"github.com/gohugoio/locales"
"github.com/gohugoio/locales/currency"
)
var (
// One normally only need a small subset of all the languages,
// so delay creation until needed.
mu sync.RWMutex
translatorFuncs = make(map[string]func() locales.Translator)
translators = make(map[string]locales.Translator)
)
// GetTranslator gets the Translator for the given locale, nil if not found.
func GetTranslator(locale string) locales.Translator {
locale = strings.ToLower(strings.ReplaceAll(locale, "-", "_"))
mu.RLock()
t, found := translators[locale]
if found {
mu.RUnlock()
return t
}
fn, found := translatorFuncs[locale]
mu.RUnlock()
if !found {
return nil
}
mu.Lock()
t = fn()
translators[locale] = t
mu.Unlock()
return t
}
// GetCurrency gets the currency for the given ISO 4217 code,
// or -1 if not found.
func GetCurrency(code string) currency.Type {
c, found := currencies[strings.ToUpper(code)]
if !found {
return -1
}
return c
}