-
Notifications
You must be signed in to change notification settings - Fork 1
/
locale.go
113 lines (93 loc) · 3.95 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
101
102
103
104
105
106
107
108
109
110
111
112
113
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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.
//go:generate go run generator.go
package lunes
import (
"fmt"
)
// A Locale provides a collection of time layouts values in a specific language.
// It is used to provide a map between the time layout elements in foreign language to English.
type Locale interface {
// Language represents a BCP 47 tag, specifying this locale language.
Language() string
// LongDayNames returns the long day names translations for the week days.
// It must be sorted, starting from Sunday to Saturday, and contains all 7 elements,
// even if one or more days are empty. If this locale does not support this format,
// it should return an empty slice.
LongDayNames() []string
// ShortDayNames returns the short day names translations for the week days.
// It must be sorted, starting from Sunday to Saturday, and contains all 7 elements,
// even if one or more days are empty. If this locale does not support this format,
// it should return an empty slice.
ShortDayNames() []string
// LongMonthNames returns the long day names translations for the months names.
// It must be sorted, starting from January to December, and contains all 12 elements,
// even if one or more months are empty. If this locale does not support this format,
// it should return an empty slice.
LongMonthNames() []string
// ShortMonthNames returns the short day names translations for the months names.
// It must be sorted, starting from January to December, and contains all 12 elements,
// even if one or more months are empty. If this locale does not support this format,
// it should return an empty slice.
ShortMonthNames() []string
// DayPeriods returns the periods of day translations for the AM and PM abbreviations.
// It must be sorted, starting from AM to PM, and contains both elements, even if one
// of them is empty. If this locale does not support this format, it should return an
// empty slice.
DayPeriods() []string
}
type genericLocale struct {
lang string
table [5][]string
}
func (g *genericLocale) LongDayNames() []string {
return g.table[longDayNamesField]
}
func (g *genericLocale) ShortDayNames() []string {
return g.table[shortDayNamesField]
}
func (g *genericLocale) LongMonthNames() []string {
return g.table[longMonthNamesField]
}
func (g *genericLocale) ShortMonthNames() []string {
return g.table[shortMonthNamesField]
}
func (g *genericLocale) DayPeriods() []string {
return g.table[dayPeriodsField]
}
func (g *genericLocale) Language() string {
return g.lang
}
// ErrUnsupportedLocale indicates that a provided language.Tag is not supported by the
// default CLDR generic locales.
type ErrUnsupportedLocale struct {
lang string
}
func (e *ErrUnsupportedLocale) Error() string {
return fmt.Sprintf("locale %s not supported", e.lang)
}
// NewDefaultLocale creates a new generic locale for the given BCP 47 language tag, using
// the default CLDR gregorian calendars data of the specified language.
// If the language is unknown and no default data is found, it returns ErrUnsupportedLocale.
func NewDefaultLocale(lang string) (Locale, error) {
table, ok := tables[lang]
if !ok {
return nil, &ErrUnsupportedLocale{lang}
}
locale := genericLocale{lang: lang, table: table}
return &locale, nil
}