-
Notifications
You must be signed in to change notification settings - Fork 25
/
locale.go
100 lines (90 loc) · 3.1 KB
/
locale.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
// Copyright 2015-present, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package i18n
import (
"github.com/corestoreio/pkg/util/slices"
"golang.org/x/text/language"
"golang.org/x/text/language/display"
)
// LocaleSeparator defines the underscore because in Magento land we also have
// the underscore as a separator.
// http://www.unicode.org/reports/tr35/#Language_and_Locale_IDs
const LocaleSeparator = "_"
// Available contains all available locales. One should not modify this slice.
var LocaleAvailable slices.String
// Supported contains all supported locales by this package. One should not modify this slice.
var LocaleSupported slices.String
var (
// Only import the supported dictionaries here to reduce the amount of
// data linked into your binary by only using the predefined Dictionary variables
tags = []language.Tag{
language.English, // first entry here is the default language
language.German,
language.French,
language.Italian,
language.Spanish,
language.Japanese,
language.Ukrainian,
}
dicts = []*display.Dictionary{
display.English,
display.German,
display.French,
display.Italian,
display.Spanish,
display.Japanese,
display.Ukrainian,
}
matcher = language.NewMatcher(tags)
)
func init() {
// @todo check if this is the proper way to generate the locales
for _, tag := range display.Values.Tags() {
b, bc := tag.Base()
r, rc := tag.Region()
if bc >= language.Exact && rc >= language.Low && !b.IsPrivateUse() && !r.IsPrivateUse() && r.IsCountry() {
LocaleAvailable.Append(b.String() + LocaleSeparator + r.String())
}
}
for _, tag := range tags {
b, bc := tag.Base()
r, rc := tag.Region()
if bc >= language.High && rc >= language.Low {
LocaleSupported.Append(b.String() + LocaleSeparator + r.String())
}
}
}
// getDict returns a predefnied dictionary for a given tag. If matching fails
// the fall-back language, which will be the first one passed to NewMatcher,
// will be returned.
func getDict(t language.Tag) *display.Dictionary {
_, i, _ := matcher.Match(t)
return dicts[i]
}
// GetLocale creates a new language Tag from a locale
func GetLocaleTag(locale string) (language.Tag, error) {
return language.Parse(locale)
}
// GetLocale generates a locale from a base and a region and may use an optional script.
// lang
// lang_script
// lang_script_region
// lang_region (aliases to lang_script_region)
func GetLocale(b language.Base, r language.Region, s ...language.Script) string {
ret := b.String()
if len(s) == 1 {
ret += LocaleSeparator + s[0].String()
}
return ret + LocaleSeparator + r.String()
}