forked from goinggo/beego-mgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locale.go
65 lines (58 loc) · 1.61 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
// Package locale parses locale strings.
package locale
import (
"fmt"
"github.com/goinggo/beego-mgo/go-i18n/i18n/language"
"regexp"
"strings"
)
// Locale is a language and a geographic region (e.g. en-US, en-GB).
type Locale struct {
ID string
Language *language.Language
}
// tagMatcher matches language tags (e.g. zh-CN).
var tagMatcher = regexp.MustCompile(`^([a-z]{2})[_\-]([A-Z]{2})$`)
// tagSplitter matches characters not found in language tags.
var tagSplitter = regexp.MustCompile(`[^a-zA-Z_\-]+`)
// New searches s for a valid language tag (RFC 5646)
// of the form xx-YY or xx_YY where
// xx is a 2 character language code and
// YY is a 2 character country code.
//
// It returns an error if s doesn't contain exactly one language tag or
// if the language represented by the tag is not supported by this package.
func New(s string) (*Locale, error) {
parts := tagSplitter.Split(s, -1)
var id, lc string
count := 0
for _, part := range parts {
if tag := tagMatcher.FindStringSubmatch(part); tag != nil {
count += 1
id, lc = tag[0], tag[1]
}
}
if count != 1 {
return nil, fmt.Errorf("%d locales found in string %s", count, s)
}
id = strings.Replace(id, "_", "-", -1)
lang := language.LanguageWithID(id)
if lang == nil {
lang = language.LanguageWithID(lc)
}
if lang == nil {
return nil, fmt.Errorf("unknown language %s", id)
}
return &Locale{id, lang}, nil
}
// MustNew is similar to New except that it panics if an error happens.
func MustNew(s string) *Locale {
locale, err := New(s)
if err != nil {
panic(err)
}
return locale
}
func (l *Locale) String() string {
return l.ID
}