1 package ja
2
3 import (
4 "math"
5 "strconv"
6 "time"
7
8 "github.com/go-playground/locales"
9 "github.com/go-playground/locales/currency"
10 )
11
12 type ja struct {
13 locale string
14 pluralsCardinal []locales.PluralRule
15 pluralsOrdinal []locales.PluralRule
16 pluralsRange []locales.PluralRule
17 decimal string
18 group string
19 minus string
20 percent string
21 perMille string
22 timeSeparator string
23 inifinity string
24 currencies []string
25 currencyNegativePrefix string
26 currencyNegativeSuffix string
27 monthsAbbreviated []string
28 monthsNarrow []string
29 monthsWide []string
30 daysAbbreviated []string
31 daysNarrow []string
32 daysShort []string
33 daysWide []string
34 periodsAbbreviated []string
35 periodsNarrow []string
36 periodsShort []string
37 periodsWide []string
38 erasAbbreviated []string
39 erasNarrow []string
40 erasWide []string
41 timezones map[string]string
42 }
43
44
45 func New() locales.Translator {
46 return &ja{
47 locale: "ja",
48 pluralsCardinal: []locales.PluralRule{6},
49 pluralsOrdinal: []locales.PluralRule{6},
50 pluralsRange: []locales.PluralRule{6},
51 decimal: ".",
52 group: ",",
53 minus: "-",
54 percent: "%",
55 perMille: "‰",
56 timeSeparator: ":",
57 inifinity: "∞",
58 currencies: []string{"ADP", "AED", "AFA", "AFN", "ALK", "ALL", "AMD", "ANG", "AOA", "AOK", "AON", "AOR", "ARA", "ARL", "ARM", "ARP", "ARS", "ATS", "A$", "AWG", "AZM", "AZN", "BAD", "BAM", "BAN", "BBD", "BDT", "BEC", "BEF", "BEL", "BGL", "BGM", "BGN", "BGO", "BHD", "BIF", "BMD", "BND", "BOB", "BOL", "BOP", "BOV", "BRB", "BRC", "BRE", "R$", "BRN", "BRR", "BRZ", "BSD", "BTN", "BUK", "BWP", "BYB", "BYN", "BYR", "BZD", "CA$", "CDF", "CHE", "CHF", "CHW", "CLE", "CLF", "CLP", "CNH", "CNX", "元", "COP", "COU", "CRC", "CSD", "CSK", "CUC", "CUP", "CVE", "CYP", "CZK", "DDM", "DEM", "DJF", "DKK", "DOP", "DZD", "ECS", "ECV", "EEK", "EGP", "ERN", "ESA", "ESB", "ESP", "ETB", "€", "FIM", "FJD", "FKP", "FRF", "£", "GEK", "GEL", "GHC", "GHS", "GIP", "GMD", "GNF", "GNS", "GQE", "GRD", "GTQ", "GWE", "GWP", "GYD", "HK$", "HNL", "HRD", "HRK", "HTG", "HUF", "IDR", "IEP", "ILP", "ILR", "₪", "₹", "IQD", "IRR", "ISJ", "ISK", "ITL", "JMD", "JOD", "¥", "KES", "KGS", "KHR", "KMF", "KPW", "KRH", "KRO", "₩", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LTL", "LTT", "LUC", "LUF", "LUL", "LVL", "LVR", "LYD", "MAD", "MAF", "MCF", "MDC", "MDL", "MGA", "MGF", "MKD", "MKN", "MLF", "MMK", "MNT", "MOP", "MRO", "MRU", "MTL", "MTP", "MUR", "MVP", "MVR", "MWK", "MX$", "MXP", "MXV", "MYR", "MZE", "MZM", "MZN", "NAD", "NGN", "NIC", "NIO", "NLG", "NOK", "NPR", "NZ$", "OMR", "PAB", "PEI", "PEN", "PES", "PGK", "PHP", "PKR", "PLN", "PLZ", "PTE", "PYG", "QAR", "RHD", "ROL", "RON", "RSD", "RUB", "RUR", "RWF", "SAR", "SBD", "SCR", "SDD", "SDG", "SDP", "SEK", "SGD", "SHP", "SIT", "SKK", "SLL", "SOS", "SRD", "SRG", "SSP", "STD", "STN", "SUR", "SVC", "SYP", "SZL", "THB", "TJR", "TJS", "TMM", "TMT", "TND", "TOP", "TPE", "TRL", "TRY", "TTD", "NT$", "TZS", "UAH", "UAK", "UGS", "UGX", "$", "USN", "USS", "UYI", "UYP", "UYU", "UYW", "UZS", "VEB", "VEF", "VES", "₫", "VNN", "VUV", "WST", "FCFA", "XAG", "XAU", "XBA", "XBB", "XBC", "XBD", "EC$", "XDR", "XEU", "XFO", "XFU", "CFA", "XPD", "CFPF", "XPT", "XRE", "XSU", "XTS", "XUA", "XXX", "YDD", "YER", "YUD", "YUM", "YUN", "YUR", "ZAL", "ZAR", "ZMK", "ZMW", "ZRN", "ZRZ", "ZWD", "ZWL", "ZWR"},
59 currencyNegativePrefix: "(",
60 currencyNegativeSuffix: ")",
61 monthsAbbreviated: []string{"", "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"},
62 monthsNarrow: []string{"", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
63 monthsWide: []string{"", "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"},
64 daysAbbreviated: []string{"日", "月", "火", "水", "木", "金", "土"},
65 daysNarrow: []string{"日", "月", "火", "水", "木", "金", "土"},
66 daysShort: []string{"日", "月", "火", "水", "木", "金", "土"},
67 daysWide: []string{"日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"},
68 periodsAbbreviated: []string{"午前", "午後"},
69 periodsNarrow: []string{"午前", "午後"},
70 periodsWide: []string{"午前", "午後"},
71 erasAbbreviated: []string{"紀元前", "西暦"},
72 erasNarrow: []string{"BC", "AD"},
73 erasWide: []string{"紀元前", "西暦"},
74 timezones: map[string]string{"ACDT": "オーストラリア中部夏時間", "ACST": "オーストラリア中部標準時", "ACWDT": "オーストラリア中西部夏時間", "ACWST": "オーストラリア中西部標準時", "ADT": "大西洋夏時間", "AEDT": "オーストラリア東部夏時間", "AEST": "オーストラリア東部標準時", "AKDT": "アラスカ夏時間", "AKST": "アラスカ標準時", "ARST": "アルゼンチン夏時間", "ART": "アルゼンチン標準時", "AST": "大西洋標準時", "AWDT": "オーストラリア西部夏時間", "AWST": "オーストラリア西部標準時", "BOT": "ボリビア時間", "BT": "ブータン時間", "CAT": "中央アフリカ時間", "CDT": "アメリカ中部夏時間", "CHADT": "チャタム夏時間", "CHAST": "チャタム標準時", "CLST": "チリ夏時間", "CLT": "チリ標準時", "COST": "コロンビア夏時間", "COT": "コロンビア標準時", "CST": "アメリカ中部標準時", "ChST": "チャモロ時間", "EAT": "東アフリカ時間", "ECT": "エクアドル時間", "EDT": "アメリカ東部夏時間", "EST": "アメリカ東部標準時", "GFT": "仏領ギアナ時間", "GMT": "グリニッジ標準時", "GST": "湾岸標準時", "GYT": "ガイアナ時間", "HADT": "ハワイ・アリューシャン夏時間", "HAST": "ハワイ・アリューシャン標準時", "HAT": "ニューファンドランド夏時間", "HECU": "キューバ夏時間", "HEEG": "グリーンランド東部夏時間", "HENOMX": "メキシコ北西部夏時間", "HEOG": "グリーンランド西部夏時間", "HEPM": "サンピエール島・ミクロン島夏時間", "HEPMX": "メキシコ太平洋夏時間", "HKST": "香港夏時間", "HKT": "香港標準時", "HNCU": "キューバ標準時", "HNEG": "グリーンランド東部標準時", "HNNOMX": "メキシコ北西部標準時", "HNOG": "グリーンランド西部標準時", "HNPM": "サンピエール島・ミクロン島標準時", "HNPMX": "メキシコ太平洋標準時", "HNT": "ニューファンドランド標準時", "IST": "インド標準時", "JDT": "日本夏時間", "JST": "日本標準時", "LHDT": "ロードハウ夏時間", "LHST": "ロードハウ標準時", "MDT": "アメリカ山地夏時間", "MESZ": "中央ヨーロッパ夏時間", "MEZ": "中央ヨーロッパ標準時", "MST": "アメリカ山地標準時", "MYT": "マレーシア時間", "NZDT": "ニュージーランド夏時間", "NZST": "ニュージーランド標準時", "OESZ": "東ヨーロッパ夏時間", "OEZ": "東ヨーロッパ標準時", "PDT": "アメリカ太平洋夏時間", "PST": "アメリカ太平洋標準時", "SAST": "南アフリカ標準時", "SGT": "シンガポール標準時", "SRT": "スリナム時間", "TMST": "トルクメニスタン夏時間", "TMT": "トルクメニスタン標準時", "UYST": "ウルグアイ夏時間", "UYT": "ウルグアイ標準時", "VET": "ベネズエラ時間", "WARST": "西部アルゼンチン夏時間", "WART": "西部アルゼンチン標準時", "WAST": "西アフリカ夏時間", "WAT": "西アフリカ標準時", "WESZ": "西ヨーロッパ夏時間", "WEZ": "西ヨーロッパ標準時", "WIB": "インドネシア西部時間", "WIT": "インドネシア東部時間", "WITA": "インドネシア中部時間", "∅∅∅": "ペルー夏時間"},
75 }
76 }
77
78
79 func (ja *ja) Locale() string {
80 return ja.locale
81 }
82
83
84 func (ja *ja) PluralsCardinal() []locales.PluralRule {
85 return ja.pluralsCardinal
86 }
87
88
89 func (ja *ja) PluralsOrdinal() []locales.PluralRule {
90 return ja.pluralsOrdinal
91 }
92
93
94 func (ja *ja) PluralsRange() []locales.PluralRule {
95 return ja.pluralsRange
96 }
97
98
99 func (ja *ja) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
100 return locales.PluralRuleOther
101 }
102
103
104 func (ja *ja) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
105 return locales.PluralRuleOther
106 }
107
108
109 func (ja *ja) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
110 return locales.PluralRuleOther
111 }
112
113
114 func (ja *ja) MonthAbbreviated(month time.Month) string {
115 return ja.monthsAbbreviated[month]
116 }
117
118
119 func (ja *ja) MonthsAbbreviated() []string {
120 return ja.monthsAbbreviated[1:]
121 }
122
123
124 func (ja *ja) MonthNarrow(month time.Month) string {
125 return ja.monthsNarrow[month]
126 }
127
128
129 func (ja *ja) MonthsNarrow() []string {
130 return ja.monthsNarrow[1:]
131 }
132
133
134 func (ja *ja) MonthWide(month time.Month) string {
135 return ja.monthsWide[month]
136 }
137
138
139 func (ja *ja) MonthsWide() []string {
140 return ja.monthsWide[1:]
141 }
142
143
144 func (ja *ja) WeekdayAbbreviated(weekday time.Weekday) string {
145 return ja.daysAbbreviated[weekday]
146 }
147
148
149 func (ja *ja) WeekdaysAbbreviated() []string {
150 return ja.daysAbbreviated
151 }
152
153
154 func (ja *ja) WeekdayNarrow(weekday time.Weekday) string {
155 return ja.daysNarrow[weekday]
156 }
157
158
159 func (ja *ja) WeekdaysNarrow() []string {
160 return ja.daysNarrow
161 }
162
163
164 func (ja *ja) WeekdayShort(weekday time.Weekday) string {
165 return ja.daysShort[weekday]
166 }
167
168
169 func (ja *ja) WeekdaysShort() []string {
170 return ja.daysShort
171 }
172
173
174 func (ja *ja) WeekdayWide(weekday time.Weekday) string {
175 return ja.daysWide[weekday]
176 }
177
178
179 func (ja *ja) WeekdaysWide() []string {
180 return ja.daysWide
181 }
182
183
184 func (ja *ja) Decimal() string {
185 return ja.decimal
186 }
187
188
189 func (ja *ja) Group() string {
190 return ja.group
191 }
192
193
194 func (ja *ja) Minus() string {
195 return ja.minus
196 }
197
198
199 func (ja *ja) FmtNumber(num float64, v uint64) string {
200
201 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
202 l := len(s) + 2 + 1*len(s[:len(s)-int(v)-1])/3
203 count := 0
204 inWhole := v == 0
205 b := make([]byte, 0, l)
206
207 for i := len(s) - 1; i >= 0; i-- {
208
209 if s[i] == '.' {
210 b = append(b, ja.decimal[0])
211 inWhole = true
212 continue
213 }
214
215 if inWhole {
216 if count == 3 {
217 b = append(b, ja.group[0])
218 count = 1
219 } else {
220 count++
221 }
222 }
223
224 b = append(b, s[i])
225 }
226
227 if num < 0 {
228 b = append(b, ja.minus[0])
229 }
230
231
232 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
233 b[i], b[j] = b[j], b[i]
234 }
235
236 return string(b)
237 }
238
239
240
241 func (ja *ja) FmtPercent(num float64, v uint64) string {
242 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
243 l := len(s) + 3
244 b := make([]byte, 0, l)
245
246 for i := len(s) - 1; i >= 0; i-- {
247
248 if s[i] == '.' {
249 b = append(b, ja.decimal[0])
250 continue
251 }
252
253 b = append(b, s[i])
254 }
255
256 if num < 0 {
257 b = append(b, ja.minus[0])
258 }
259
260
261 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
262 b[i], b[j] = b[j], b[i]
263 }
264
265 b = append(b, ja.percent...)
266
267 return string(b)
268 }
269
270
271 func (ja *ja) FmtCurrency(num float64, v uint64, currency currency.Type) string {
272
273 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
274 symbol := ja.currencies[currency]
275 l := len(s) + len(symbol) + 2 + 1*len(s[:len(s)-int(v)-1])/3
276 count := 0
277 inWhole := v == 0
278 b := make([]byte, 0, l)
279
280 for i := len(s) - 1; i >= 0; i-- {
281
282 if s[i] == '.' {
283 b = append(b, ja.decimal[0])
284 inWhole = true
285 continue
286 }
287
288 if inWhole {
289 if count == 3 {
290 b = append(b, ja.group[0])
291 count = 1
292 } else {
293 count++
294 }
295 }
296
297 b = append(b, s[i])
298 }
299
300 for j := len(symbol) - 1; j >= 0; j-- {
301 b = append(b, symbol[j])
302 }
303
304 if num < 0 {
305 b = append(b, ja.minus[0])
306 }
307
308
309 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
310 b[i], b[j] = b[j], b[i]
311 }
312
313 if int(v) < 2 {
314
315 if v == 0 {
316 b = append(b, ja.decimal...)
317 }
318
319 for i := 0; i < 2-int(v); i++ {
320 b = append(b, '0')
321 }
322 }
323
324 return string(b)
325 }
326
327
328
329 func (ja *ja) FmtAccounting(num float64, v uint64, currency currency.Type) string {
330
331 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
332 symbol := ja.currencies[currency]
333 l := len(s) + len(symbol) + 4 + 1*len(s[:len(s)-int(v)-1])/3
334 count := 0
335 inWhole := v == 0
336 b := make([]byte, 0, l)
337
338 for i := len(s) - 1; i >= 0; i-- {
339
340 if s[i] == '.' {
341 b = append(b, ja.decimal[0])
342 inWhole = true
343 continue
344 }
345
346 if inWhole {
347 if count == 3 {
348 b = append(b, ja.group[0])
349 count = 1
350 } else {
351 count++
352 }
353 }
354
355 b = append(b, s[i])
356 }
357
358 if num < 0 {
359
360 for j := len(symbol) - 1; j >= 0; j-- {
361 b = append(b, symbol[j])
362 }
363
364 b = append(b, ja.currencyNegativePrefix[0])
365
366 } else {
367
368 for j := len(symbol) - 1; j >= 0; j-- {
369 b = append(b, symbol[j])
370 }
371
372 }
373
374
375 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
376 b[i], b[j] = b[j], b[i]
377 }
378
379 if int(v) < 2 {
380
381 if v == 0 {
382 b = append(b, ja.decimal...)
383 }
384
385 for i := 0; i < 2-int(v); i++ {
386 b = append(b, '0')
387 }
388 }
389
390 if num < 0 {
391 b = append(b, ja.currencyNegativeSuffix...)
392 }
393
394 return string(b)
395 }
396
397
398 func (ja *ja) FmtDateShort(t time.Time) string {
399
400 b := make([]byte, 0, 32)
401
402 if t.Year() > 0 {
403 b = strconv.AppendInt(b, int64(t.Year()), 10)
404 } else {
405 b = strconv.AppendInt(b, int64(-t.Year()), 10)
406 }
407
408 b = append(b, []byte{0x2f}...)
409
410 if t.Month() < 10 {
411 b = append(b, '0')
412 }
413
414 b = strconv.AppendInt(b, int64(t.Month()), 10)
415
416 b = append(b, []byte{0x2f}...)
417
418 if t.Day() < 10 {
419 b = append(b, '0')
420 }
421
422 b = strconv.AppendInt(b, int64(t.Day()), 10)
423
424 return string(b)
425 }
426
427
428 func (ja *ja) FmtDateMedium(t time.Time) string {
429
430 b := make([]byte, 0, 32)
431
432 if t.Year() > 0 {
433 b = strconv.AppendInt(b, int64(t.Year()), 10)
434 } else {
435 b = strconv.AppendInt(b, int64(-t.Year()), 10)
436 }
437
438 b = append(b, []byte{0x2f}...)
439
440 if t.Month() < 10 {
441 b = append(b, '0')
442 }
443
444 b = strconv.AppendInt(b, int64(t.Month()), 10)
445
446 b = append(b, []byte{0x2f}...)
447
448 if t.Day() < 10 {
449 b = append(b, '0')
450 }
451
452 b = strconv.AppendInt(b, int64(t.Day()), 10)
453
454 return string(b)
455 }
456
457
458 func (ja *ja) FmtDateLong(t time.Time) string {
459
460 b := make([]byte, 0, 32)
461
462 if t.Year() > 0 {
463 b = strconv.AppendInt(b, int64(t.Year()), 10)
464 } else {
465 b = strconv.AppendInt(b, int64(-t.Year()), 10)
466 }
467
468 b = append(b, []byte{0xe5, 0xb9, 0xb4}...)
469 b = strconv.AppendInt(b, int64(t.Month()), 10)
470 b = append(b, []byte{0xe6, 0x9c, 0x88}...)
471 b = strconv.AppendInt(b, int64(t.Day()), 10)
472 b = append(b, []byte{0xe6, 0x97, 0xa5}...)
473
474 return string(b)
475 }
476
477
478 func (ja *ja) FmtDateFull(t time.Time) string {
479
480 b := make([]byte, 0, 32)
481
482 if t.Year() > 0 {
483 b = strconv.AppendInt(b, int64(t.Year()), 10)
484 } else {
485 b = strconv.AppendInt(b, int64(-t.Year()), 10)
486 }
487
488 b = append(b, []byte{0xe5, 0xb9, 0xb4}...)
489 b = strconv.AppendInt(b, int64(t.Month()), 10)
490 b = append(b, []byte{0xe6, 0x9c, 0x88}...)
491 b = strconv.AppendInt(b, int64(t.Day()), 10)
492 b = append(b, []byte{0xe6, 0x97, 0xa5}...)
493 b = append(b, ja.daysWide[t.Weekday()]...)
494
495 return string(b)
496 }
497
498
499 func (ja *ja) FmtTimeShort(t time.Time) string {
500
501 b := make([]byte, 0, 32)
502
503 b = strconv.AppendInt(b, int64(t.Hour()), 10)
504 b = append(b, ja.timeSeparator...)
505
506 if t.Minute() < 10 {
507 b = append(b, '0')
508 }
509
510 b = strconv.AppendInt(b, int64(t.Minute()), 10)
511
512 return string(b)
513 }
514
515
516 func (ja *ja) FmtTimeMedium(t time.Time) string {
517
518 b := make([]byte, 0, 32)
519
520 b = strconv.AppendInt(b, int64(t.Hour()), 10)
521 b = append(b, ja.timeSeparator...)
522
523 if t.Minute() < 10 {
524 b = append(b, '0')
525 }
526
527 b = strconv.AppendInt(b, int64(t.Minute()), 10)
528 b = append(b, ja.timeSeparator...)
529
530 if t.Second() < 10 {
531 b = append(b, '0')
532 }
533
534 b = strconv.AppendInt(b, int64(t.Second()), 10)
535
536 return string(b)
537 }
538
539
540 func (ja *ja) FmtTimeLong(t time.Time) string {
541
542 b := make([]byte, 0, 32)
543
544 b = strconv.AppendInt(b, int64(t.Hour()), 10)
545 b = append(b, ja.timeSeparator...)
546
547 if t.Minute() < 10 {
548 b = append(b, '0')
549 }
550
551 b = strconv.AppendInt(b, int64(t.Minute()), 10)
552 b = append(b, ja.timeSeparator...)
553
554 if t.Second() < 10 {
555 b = append(b, '0')
556 }
557
558 b = strconv.AppendInt(b, int64(t.Second()), 10)
559 b = append(b, []byte{0x20}...)
560
561 tz, _ := t.Zone()
562 b = append(b, tz...)
563
564 return string(b)
565 }
566
567
568 func (ja *ja) FmtTimeFull(t time.Time) string {
569
570 b := make([]byte, 0, 32)
571
572 b = strconv.AppendInt(b, int64(t.Hour()), 10)
573 b = append(b, []byte{0xe6, 0x99, 0x82}...)
574
575 if t.Minute() < 10 {
576 b = append(b, '0')
577 }
578
579 b = strconv.AppendInt(b, int64(t.Minute()), 10)
580 b = append(b, []byte{0xe5, 0x88, 0x86}...)
581
582 if t.Second() < 10 {
583 b = append(b, '0')
584 }
585
586 b = strconv.AppendInt(b, int64(t.Second()), 10)
587 b = append(b, []byte{0xe7, 0xa7, 0x92, 0x20}...)
588
589 tz, _ := t.Zone()
590
591 if btz, ok := ja.timezones[tz]; ok {
592 b = append(b, btz...)
593 } else {
594 b = append(b, tz...)
595 }
596
597 return string(b)
598 }
599
View as plain text