summaryrefslogtreecommitdiffhomepage
path: root/src/i18n.go
blob: dac7c5eb710647fdf1fb1ba6b3f8d325cccfe71e (plain) (blame)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//go:generate gotext -srclang=en -dir=rosetta extract -lang=bg,el,en,nl .
//go:generate ../exttmpl

package src

import (
	"fmt"
	"strings"
	"time"

	"golang.org/x/text/language"
	"golang.org/x/text/message"
)

type Printer struct {
	Locale locale
	inner  *message.Printer
}

type locale struct {
	Bcp, Name         string
	Eurozone, Enabled bool
	dateFmt, moneyFmt string
}

var (
	Locales = [...]locale{
		{
			Bcp:      "ca",
			Name:     "català",
			dateFmt:  "2/1/2006",
			Eurozone: true,
			Enabled:  false,
		},
		{
			Bcp:      "de",
			Name:     "Deutsch",
			dateFmt:  "2.1.2006",
			Eurozone: true,
			Enabled:  false,
		},
		{
			Bcp:      "el",
			Name:     "ελληνικά",
			dateFmt:  "2/1/2006",
			Eurozone: true,
			Enabled:  true,
		},
		{
			Bcp:      "en",
			Name:     "English",
			dateFmt:  "02/01/2006",
			Eurozone: true,
			Enabled:  true,
		},
		{
			Bcp:      "es",
			Name:     "español",
			dateFmt:  "2/1/2006",
			Eurozone: true,
			Enabled:  false,
		},
		{
			Bcp:      "et",
			Name:     "eesti",
			dateFmt:  "2.1.2006",
			Eurozone: true,
			Enabled:  false,
		},
		{
			Bcp:      "fi",
			Name:     "suomi",
			dateFmt:  "2.1.2006",
			Eurozone: true,
			Enabled:  false,
		},
		{
			Bcp:      "fr",
			Name:     "français",
			dateFmt:  "02/01/2006",
			Eurozone: true,
			Enabled:  false,
		},
		{
			Bcp:      "ga",
			Name:     "Gaeilge",
			dateFmt:  "02/01/2006",
			Eurozone: true,
			Enabled:  false,
		},
		{
			Bcp:      "hr",
			Name:     "hrvatski",
			dateFmt:  "02. 01. 2006.",
			Eurozone: true,
			Enabled:  false,
		},
		{
			Bcp:      "it",
			Name:     "italiano",
			dateFmt:  "02/01/2006",
			Eurozone: true,
			Enabled:  false,
		},
		{
			Bcp:      "lb",
			Name:     "lëtzebuergesch",
			dateFmt:  "2.1.2006",
			Eurozone: true,
			Enabled:  false,
		},
		{
			Bcp:      "lt",
			Name:     "lietuvių",
			dateFmt:  "2006-01-02",
			Eurozone: true,
			Enabled:  false,
		},
		{
			Bcp:      "lv",
			Name:     "latviešu",
			dateFmt:  "2.01.2006.",
			Eurozone: true,
			Enabled:  false,
		},
		{
			Bcp:      "mt",
			Name:     "Malti",
			dateFmt:  "2/1/2006",
			Eurozone: true,
			Enabled:  false,
		},
		{
			Bcp:      "nl",
			Name:     "Nederlands",
			dateFmt:  "2-1-2006",
			Eurozone: true,
			Enabled:  true,
		},
		{
			Bcp:      "pt",
			Name:     "português",
			dateFmt:  "02/01/2006",
			Eurozone: true,
			Enabled:  false,
		},
		{
			Bcp:      "sk",
			Name:     "slovenčina",
			dateFmt:  "2. 1. 2006",
			Eurozone: true,
			Enabled:  false,
		},
		{
			Bcp:      "sl",
			Name:     "slovenščina",
			dateFmt:  "2. 1. 2006",
			Eurozone: true,
			Enabled:  false,
		},

		/* Non-Eurozone locales */
		{
			Bcp:      "bg",
			Name:     "български",
			dateFmt:  "2.01.2006 г.",
			Eurozone: false,
			Enabled:  true,
		},
		{
			Bcp:      "en-US",
			Name:     "English (US)",
			dateFmt:  "1/2/2006",
			Eurozone: false,
			Enabled:  false,
		},
		{
			Bcp:      "ro",
			Name:     "română",
			dateFmt:  "02.01.2006",
			Eurozone: false,
			Enabled:  false,
		},
		{
			Bcp:      "uk",
			Name:     "yкраїнська",
			dateFmt:  "02.01.2006",
			Eurozone: false,
			Enabled:  false,
		},
	}
	/* Map of language codes to printers.  We do this instead of just
	   using language.MustParse() directly so that we can easily see if a
	   language is supported or not. */
	printers       map[string]Printer = make(map[string]Printer, len(Locales))
	defaultPrinter Printer
)

func init() {
	for _, loc := range Locales {
		if loc.Enabled {
			lang := language.MustParse(loc.Bcp)
			printers[strings.ToLower(loc.Bcp)] = Printer{
				Locale: loc,
				inner:  message.NewPrinter(lang),
			}
		}
	}
	defaultPrinter = printers["en"]
}

func (p Printer) T(fmt string, args ...any) string {
	return p.inner.Sprintf(fmt, args...)
}

func (p Printer) N(n int) string {
	return p.inner.Sprint(n)
}

func (p Printer) Date(d time.Time) string {
	return d.Format(p.Locale.dateFmt)
}

func (p Printer) M(val float64, round bool) string {
	var valstr string

	/* Hack to avoid gotext writing these two ‘translations’ into the
	   translations file */
	f := p.inner.Sprintf
	if round {
		valstr = f("%d", int(val))
	} else {
		valstr = f("%.2f", val)
	}

	/* All Eurozone languages place the eurosign after the value except
	   for Dutch, English, Gaelic, and Maltese.  Austrian German also
	   uses Dutch-style formatting, but we do not support that dialect. */
	switch p.Locale.Bcp {
	case "en", "en-US", "ga", "mt":
		return fmt.Sprintf("€%s", valstr)
	case "nl":
		return fmt.Sprintf("€ %s", valstr)
	default:
		return fmt.Sprintf("%s €", valstr)
	}
}

/* Transform ‘en-US’ to ‘en’ */
func (l locale) Language() string {
	return l.Bcp[:2]
}