-
Notifications
You must be signed in to change notification settings - Fork 58
/
generate_resources.go
2895 lines (2196 loc) · 67.9 KB
/
generate_resources.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"fmt"
"log"
"os"
"os/exec"
"regexp"
"sort"
"strconv"
"strings"
"github.com/go-playground/locales"
"golang.org/x/text/unicode/cldr"
"text/template"
)
const (
locDir = "../%s"
locFilename = locDir + "/%s.go"
)
var (
tfuncs = template.FuncMap{
"is_multibyte": func(s string) bool {
return len([]byte(s)) > 1
},
"reverse_bytes": func(s string) string {
b := make([]byte, 0, 8)
for j := len(s) - 1; j >= 0; j-- {
b = append(b, s[j])
}
return fmt.Sprintf("%#v", b)
},
"byte_count": func(s ...string) string {
var count int
for i := 0; i < len(s); i++ {
count += len([]byte(s[i]))
}
return strconv.Itoa(count)
},
}
prVarFuncs = map[string]string{
"n": "n := math.Abs(num)\n",
"i": "i := int64(n)\n",
// "v": "v := ...", // inherently available as argument
"w": "w := locales.W(n, v)\n",
"f": "f := locales.F(n, v)\n",
"t": "t := locales.T(n, v)\n",
}
translators = make(map[string]*translator)
baseTranslators = make(map[string]*translator)
globalCurrenciesMap = make(map[string]struct{}) // ["USD"] = "$" currency code, just all currencies for mapping to enum
globCurrencyIdxMap = make(map[string]int) // ["USD"] = 0
globalCurrencies = make([]string, 0, 100) // array of currency codes index maps to enum
tmpl *template.Template
nModRegex = regexp.MustCompile("(n%[0-9]+)")
iModRegex = regexp.MustCompile("(i%[0-9]+)")
wModRegex = regexp.MustCompile("(w%[0-9]+)")
fModRegex = regexp.MustCompile("(f%[0-9]+)")
tModRegex = regexp.MustCompile("(t%[0-9]+)")
groupLenRegex = regexp.MustCompile(",([0-9#]+)\\.")
groupLenPercentRegex = regexp.MustCompile(",([0-9#]+)$")
secondaryGroupLenRegex = regexp.MustCompile(",([0-9#]+),")
requiredNumRegex = regexp.MustCompile("([0-9]+)\\.")
requiredDecimalRegex = regexp.MustCompile("\\.([0-9]+)")
enInheritance = map[string]string{
"en_150": "en_001", "en_AG": "en_001", "en_AI": "en_001", "en_AU": "en_001", "en_BB": "en_001", "en_BE": "en_001", "en_BM": "en_001", "en_BS": "en_001", "en_BW": "en_001", "en_BZ": "en_001", "en_CA": "en_001", "en_CC": "en_001", "en_CK": "en_001", "en_CM": "en_001", "en_CX": "en_001", "en_CY": "en_001", "en_DG": "en_001", "en_DM": "en_001", "en_ER": "en_001", "en_FJ": "en_001", "en_FK": "en_001", "en_FM": "en_001", "en_GB": "en_001", "en_GD": "en_001", "en_GG": "en_001", "en_GH": "en_001", "en_GI": "en_001", "en_GM": "en_001", "en_GY": "en_001", "en_HK": "en_001", "en_IE": "en_001", "en_IL": "en_001", "en_IM": "en_001", "en_IN": "en_001", "en_IO": "en_001", "en_JE": "en_001", "en_JM": "en_001", "en_KE": "en_001", "en_KI": "en_001", "en_KN": "en_001", "en_KY": "en_001", "en_LC": "en_001", "en_LR": "en_001", "en_LS": "en_001", "en_MG": "en_001", "en_MO": "en_001", "en_MS": "en_001", "en_MT": "en_001", "en_MU": "en_001", "en_MW": "en_001", "en_MY": "en_001", "en_NA": "en_001", "en_NF": "en_001", "en_NG": "en_001", "en_NR": "en_001", "en_NU": "en_001", "en_NZ": "en_001", "en_PG": "en_001", "en_PH": "en_001", "en_PK": "en_001", "en_PN": "en_001", "en_PW": "en_001", "en_RW": "en_001", "en_SB": "en_001", "en_SC": "en_001", "en_SD": "en_001", "en_SG": "en_001", "en_SH": "en_001", "en_SL": "en_001", "en_SS": "en_001", "en_SX": "en_001", "en_SZ": "en_001", "en_TC": "en_001", "en_TK": "en_001", "en_TO": "en_001", "en_TT": "en_001", "en_TV": "en_001", "en_TZ": "en_001", "en_UG": "en_001", "en_VC": "en_001", "en_VG": "en_001", "en_VU": "en_001", "en_WS": "en_001", "en_ZA": "en_001", "en_ZM": "en_001", "en_ZW": "en_001", }
en150Inheritance = map[string]string{"en_AT": "en_150", "en_CH": "en_150", "en_DE": "en_150", "en_DK": "en_150", "en_FI": "en_150", "en_NL": "en_150", "en_SE": "en_150", "en_SI": "en_150"}
es419Inheritance = map[string]string{
"es_AR": "es_419", "es_BO": "es_419", "es_BR": "es_419", "es_BZ": "es_419", "es_CL": "es_419", "es_CO": "es_419", "es_CR": "es_419", "es_CU": "es_419", "es_DO": "es_419", "es_EC": "es_419", "es_GT": "es_419", "es_HN": "es_419", "es_MX": "es_419", "es_NI": "es_419", "es_PA": "es_419", "es_PE": "es_419", "es_PR": "es_419", "es_PY": "es_419", "es_SV": "es_419", "es_US": "es_419", "es_UY": "es_419", "es_VE": "es_419",
}
rootInheritance = map[string]string{
"az_Arab": "root", "az_Cyrl": "root", "bm_Nkoo": "root", "bs_Cyrl": "root", "en_Dsrt": "root", "en_Shaw": "root", "ha_Arab": "root", "iu_Latn": "root", "mn_Mong": "root", "ms_Arab": "root", "pa_Arab": "root", "shi_Latn": "root", "sr_Latn": "root", "uz_Arab": "root", "uz_Cyrl": "root", "vai_Latn": "root", "zh_Hant": "root", "yue_Hans": "root",
}
ptPtInheritance = map[string]string{
"pt_AO": "pt_PT", "pt_CH": "pt_PT", "pt_CV": "pt_PT", "pt_GQ": "pt_PT", "pt_GW": "pt_PT", "pt_LU": "pt_PT", "pt_MO": "pt_PT", "pt_MZ": "pt_PT", "pt_ST": "pt_PT", "pt_TL": "pt_PT",
}
zhHantHKInheritance = map[string]string{
"zh_Hant_MO": "zh_Hant_HK",
}
inheritMaps = []map[string]string{ enInheritance, en150Inheritance, es419Inheritance, rootInheritance, ptPtInheritance, zhHantHKInheritance}
)
type translator struct {
Locale string
BaseLocale string
// InheritedLocale string
Plurals string
CardinalFunc string
PluralsOrdinal string
OrdinalFunc string
PluralsRange string
RangeFunc string
Decimal string
Group string
Minus string
Percent string
PerMille string
TimeSeparator string
Infinity string
Currencies string
// FmtNumber vars
FmtNumberExists bool
FmtNumberGroupLen int
FmtNumberSecondaryGroupLen int
FmtNumberMinDecimalLen int
// FmtPercent vars
FmtPercentExists bool
FmtPercentGroupLen int
FmtPercentSecondaryGroupLen int
FmtPercentMinDecimalLen int
FmtPercentPrefix string
FmtPercentSuffix string
FmtPercentInPrefix bool
FmtPercentLeft bool
// FmtCurrency vars
FmtCurrencyExists bool
FmtCurrencyGroupLen int
FmtCurrencySecondaryGroupLen int
FmtCurrencyMinDecimalLen int
FmtCurrencyPrefix string
FmtCurrencySuffix string
FmtCurrencyInPrefix bool
FmtCurrencyLeft bool
FmtCurrencyNegativeExists bool
FmtCurrencyNegativePrefix string
FmtCurrencyNegativeSuffix string
FmtCurrencyNegativeInPrefix bool
FmtCurrencyNegativeLeft bool
// Date & Time
FmtCalendarExists bool
FmtMonthsAbbreviated string
FmtMonthsNarrow string
FmtMonthsWide string
FmtDaysAbbreviated string
FmtDaysNarrow string
FmtDaysShort string
FmtDaysWide string
FmtPeriodsAbbreviated string
FmtPeriodsNarrow string
FmtPeriodsShort string
FmtPeriodsWide string
FmtErasAbbreviated string
FmtErasNarrow string
FmtErasWide string
FmtTimezones string
// calculation only fields below this point...
DecimalNumberFormat string
PercentNumberFormat string
CurrencyNumberFormat string
NegativeCurrencyNumberFormat string
// Dates
FmtDateFull string
FmtDateLong string
FmtDateMedium string
FmtDateShort string
// Times
FmtTimeFull string
FmtTimeLong string
FmtTimeMedium string
FmtTimeShort string
// timezones per locale by type
timezones map[string]*zoneAbbrev // key = type eg. America_Eastern zone Abbrev will be long form eg. Eastern Standard Time, Pacific Standard Time.....
}
type zoneAbbrev struct {
standard string
daylight string
}
var timezones = map[string]*zoneAbbrev{} // key = type eg. America_Eastern zone Abbrev eg. EST & EDT
func main() {
var err error
// load template
tmpl, err = template.New("all").Funcs(tfuncs).ParseGlob("*.tmpl")
if err != nil {
log.Fatal(err)
}
// load CLDR recourses
var decoder cldr.Decoder
cldr, err := decoder.DecodePath("data/core")
if err != nil {
panic("failed decode CLDR data; " + err.Error())
}
preProcess(cldr)
postProcess(cldr)
var currencies string
for i, curr := range globalCurrencies {
if i == 0 {
currencies = curr + " Type = iota\n"
continue
}
currencies += curr + "\n"
}
if err = os.MkdirAll(fmt.Sprintf(locDir, "currency"), 0777); err != nil {
log.Fatal(err)
}
filename := fmt.Sprintf(locFilename, "currency", "currency")
output, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
defer output.Close()
if err := tmpl.ExecuteTemplate(output, "currencies", currencies); err != nil {
log.Fatal(err)
}
output.Close()
// after file written run gofmt on file to ensure best formatting
cmd := exec.Command("goimports", "-w", filename)
if err = cmd.Run(); err != nil {
log.Panic("failed execute \"goimports\" for file ", filename, ": ", err)
}
cmd = exec.Command("gofmt", "-s", "-w", filename)
if err = cmd.Run(); err != nil {
log.Panic("failed execute \"gofmt\" for file ", filename, ": ", err)
}
for _, trans := range translators {
fmt.Println("Writing Data:", trans.Locale)
if err = os.MkdirAll(fmt.Sprintf(locDir, trans.Locale), 0777); err != nil {
log.Fatal(err)
}
filename = fmt.Sprintf(locFilename, trans.Locale, trans.Locale)
output, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
defer output.Close()
if err := tmpl.ExecuteTemplate(output, "translator", trans); err != nil {
log.Fatal(err)
}
output.Close()
// after file written run gofmt on file to ensure best formatting
cmd := exec.Command("goimports", "-w", filename)
if err = cmd.Run(); err != nil {
log.Panic("failed execute \"goimports\" for file ", filename, ": ", err)
}
// this simplifies some syntax that I can;t find an option for in goimports, namely '-s'
cmd = exec.Command("gofmt", "-s", "-w", filename)
if err = cmd.Run(); err != nil {
log.Panic("failed execute \"gofmt\" for file ", filename, ": ", err)
}
filename = fmt.Sprintf(locFilename, trans.Locale, trans.Locale+"_test")
if _, err := os.Stat(filename); err == nil {
fmt.Println("*************** test file exists, skipping:", filename)
continue
}
output, err = os.Create(filename)
if err != nil {
log.Fatal(err)
}
defer output.Close()
if err := tmpl.ExecuteTemplate(output, "tests", trans); err != nil {
log.Fatal(err)
}
output.Close()
// after file written run gofmt on file to ensure best formatting
cmd = exec.Command("goimports", "-w", filename)
if err = cmd.Run(); err != nil {
log.Panic("failed execute \"goimports\" for file ", filename, ": ", err)
}
// this simplifies some syntax that I can;t find an option for in goimports, namely '-s'
cmd = exec.Command("gofmt", "-s", "-w", filename)
if err = cmd.Run(); err != nil {
log.Panic("failed execute \"gofmt\" for file ", filename, ": ", err)
}
}
}
func applyOverrides(trans *translator) {
if trans.BaseLocale == "ru" {
trans.PercentNumberFormat = "#,##0%"
}
}
func postProcess(cldr *cldr.CLDR) {
for _, v := range timezones {
// no DST
if len(v.daylight) == 0 {
v.daylight = v.standard
}
}
var inherited *translator
var base *translator
var inheritedFound, baseFound bool
for _, trans := range translators {
fmt.Println("Post Processing:", trans.Locale)
// cardinal plural rules
trans.CardinalFunc, trans.Plurals = parseCardinalPluralRuleFunc(cldr, trans.Locale, trans.BaseLocale)
//ordinal plural rules
trans.OrdinalFunc, trans.PluralsOrdinal = parseOrdinalPluralRuleFunc(cldr, trans.BaseLocale)
// range plural rules
trans.RangeFunc, trans.PluralsRange = parseRangePluralRuleFunc(cldr, trans.BaseLocale)
// ignore base locales
if trans.BaseLocale == trans.Locale {
inheritedFound = false
baseFound = false
} else {
inheritedFound = false
for _, inheritMap := range(inheritMaps) {
if inherit, found := inheritMap[trans.Locale]; found {
inherited, inheritedFound = translators[inherit]
break;
}
}
base, baseFound = baseTranslators[trans.BaseLocale]
}
// Numbers
if len(trans.Decimal) == 0 {
if inheritedFound {
trans.Decimal = inherited.Decimal
}
if len(trans.Decimal) == 0 && baseFound {
trans.Decimal = base.Decimal
}
if len(trans.Decimal) == 0 {
trans.Decimal = ""
}
}
if len(trans.Group) == 0 {
if inheritedFound {
trans.Group = inherited.Group
}
if len(trans.Group) == 0 && baseFound {
trans.Group = base.Group
}
if len(trans.Group) == 0 {
trans.Group = ""
}
}
if len(trans.Minus) == 0 {
if inheritedFound {
trans.Minus = inherited.Minus
}
if len(trans.Minus) == 0 && baseFound {
trans.Minus = base.Minus
}
if len(trans.Minus) == 0 {
trans.Minus = ""
}
}
if len(trans.Percent) == 0 {
if inheritedFound {
trans.Percent = inherited.Percent
}
if len(trans.Percent) == 0 && baseFound {
trans.Percent = base.Percent
}
if len(trans.Percent) == 0 {
trans.Percent = ""
}
}
if len(trans.PerMille) == 0 {
if inheritedFound {
trans.PerMille = inherited.PerMille
}
if len(trans.PerMille) == 0 && baseFound {
trans.PerMille = base.PerMille
}
if len(trans.PerMille) == 0 {
trans.PerMille = ""
}
}
if len(trans.TimeSeparator) == 0 && inheritedFound {
trans.TimeSeparator = inherited.TimeSeparator
}
if len(trans.TimeSeparator) == 0 && baseFound {
trans.TimeSeparator = base.TimeSeparator
}
if len(trans.Infinity) == 0 && inheritedFound {
trans.Infinity = inherited.Infinity
}
if len(trans.Infinity) == 0 && baseFound {
trans.Infinity = base.Infinity
}
// Currency
// number values
if len(trans.DecimalNumberFormat) == 0 && inheritedFound {
trans.DecimalNumberFormat = inherited.DecimalNumberFormat
}
if len(trans.DecimalNumberFormat) == 0 && baseFound {
trans.DecimalNumberFormat = base.DecimalNumberFormat
}
if len(trans.PercentNumberFormat) == 0 && inheritedFound {
trans.PercentNumberFormat = inherited.PercentNumberFormat
}
if len(trans.PercentNumberFormat) == 0 && baseFound {
trans.PercentNumberFormat = base.PercentNumberFormat
}
if len(trans.CurrencyNumberFormat) == 0 && inheritedFound {
trans.CurrencyNumberFormat = inherited.CurrencyNumberFormat
}
if len(trans.CurrencyNumberFormat) == 0 && baseFound {
trans.CurrencyNumberFormat = base.CurrencyNumberFormat
}
if len(trans.NegativeCurrencyNumberFormat) == 0 && inheritedFound {
trans.NegativeCurrencyNumberFormat = inherited.NegativeCurrencyNumberFormat
}
if len(trans.NegativeCurrencyNumberFormat) == 0 && baseFound {
trans.NegativeCurrencyNumberFormat = base.NegativeCurrencyNumberFormat
}
// date values
if len(trans.FmtDateFull) == 0 && inheritedFound {
trans.FmtDateFull = inherited.FmtDateFull
}
if len(trans.FmtDateFull) == 0 && baseFound {
trans.FmtDateFull = base.FmtDateFull
}
if len(trans.FmtDateLong) == 0 && inheritedFound {
trans.FmtDateLong = inherited.FmtDateLong
}
if len(trans.FmtDateLong) == 0 && baseFound {
trans.FmtDateLong = base.FmtDateLong
}
if len(trans.FmtDateMedium) == 0 && inheritedFound {
trans.FmtDateMedium = inherited.FmtDateMedium
}
if len(trans.FmtDateMedium) == 0 && baseFound {
trans.FmtDateMedium = base.FmtDateMedium
}
if len(trans.FmtDateShort) == 0 && inheritedFound {
trans.FmtDateShort = inherited.FmtDateShort
}
if len(trans.FmtDateShort) == 0 && baseFound {
trans.FmtDateShort = base.FmtDateShort
}
// time values
if len(trans.FmtTimeFull) == 0 && inheritedFound {
trans.FmtTimeFull = inherited.FmtTimeFull
}
if len(trans.FmtTimeFull) == 0 && baseFound {
trans.FmtTimeFull = base.FmtTimeFull
}
if len(trans.FmtTimeLong) == 0 && inheritedFound {
trans.FmtTimeLong = inherited.FmtTimeLong
}
if len(trans.FmtTimeLong) == 0 && baseFound {
trans.FmtTimeLong = base.FmtTimeLong
}
if len(trans.FmtTimeMedium) == 0 && inheritedFound {
trans.FmtTimeMedium = inherited.FmtTimeMedium
}
if len(trans.FmtTimeMedium) == 0 && baseFound {
trans.FmtTimeMedium = base.FmtTimeMedium
}
if len(trans.FmtTimeShort) == 0 && inheritedFound {
trans.FmtTimeShort = inherited.FmtTimeShort
}
if len(trans.FmtTimeShort) == 0 && baseFound {
trans.FmtTimeShort = base.FmtTimeShort
}
// month values
if len(trans.FmtMonthsAbbreviated) == 0 && inheritedFound {
trans.FmtMonthsAbbreviated = inherited.FmtMonthsAbbreviated
}
if len(trans.FmtMonthsAbbreviated) == 0 && baseFound {
trans.FmtMonthsAbbreviated = base.FmtMonthsAbbreviated
}
if len(trans.FmtMonthsNarrow) == 0 && inheritedFound {
trans.FmtMonthsNarrow = inherited.FmtMonthsNarrow
}
if len(trans.FmtMonthsNarrow) == 0 && baseFound {
trans.FmtMonthsNarrow = base.FmtMonthsNarrow
}
if len(trans.FmtMonthsWide) == 0 && inheritedFound {
trans.FmtMonthsWide = inherited.FmtMonthsWide
}
if len(trans.FmtMonthsWide) == 0 && baseFound {
trans.FmtMonthsWide = base.FmtMonthsWide
}
// day values
if len(trans.FmtDaysAbbreviated) == 0 && inheritedFound {
trans.FmtDaysAbbreviated = inherited.FmtDaysAbbreviated
}
if len(trans.FmtDaysAbbreviated) == 0 && baseFound {
trans.FmtDaysAbbreviated = base.FmtDaysAbbreviated
}
if len(trans.FmtDaysNarrow) == 0 && inheritedFound {
trans.FmtDaysNarrow = inherited.FmtDaysNarrow
}
if len(trans.FmtDaysNarrow) == 0 && baseFound {
trans.FmtDaysNarrow = base.FmtDaysNarrow
}
if len(trans.FmtDaysShort) == 0 && inheritedFound {
trans.FmtDaysShort = inherited.FmtDaysShort
}
if len(trans.FmtDaysShort) == 0 && baseFound {
trans.FmtDaysShort = base.FmtDaysShort
}
if len(trans.FmtDaysWide) == 0 && inheritedFound {
trans.FmtDaysWide = inherited.FmtDaysWide
}
if len(trans.FmtDaysWide) == 0 && baseFound {
trans.FmtDaysWide = base.FmtDaysWide
}
// period values
if len(trans.FmtPeriodsAbbreviated) == 0 && inheritedFound {
trans.FmtPeriodsAbbreviated = inherited.FmtPeriodsAbbreviated
}
if len(trans.FmtPeriodsAbbreviated) == 0 && baseFound {
trans.FmtPeriodsAbbreviated = base.FmtPeriodsAbbreviated
}
if len(trans.FmtPeriodsNarrow) == 0 && inheritedFound {
trans.FmtPeriodsNarrow = inherited.FmtPeriodsNarrow
}
if len(trans.FmtPeriodsNarrow) == 0 && baseFound {
trans.FmtPeriodsNarrow = base.FmtPeriodsNarrow
}
if len(trans.FmtPeriodsShort) == 0 && inheritedFound {
trans.FmtPeriodsShort = inherited.FmtPeriodsShort
}
if len(trans.FmtPeriodsShort) == 0 && baseFound {
trans.FmtPeriodsShort = base.FmtPeriodsShort
}
if len(trans.FmtPeriodsWide) == 0 && inheritedFound {
trans.FmtPeriodsWide = inherited.FmtPeriodsWide
}
if len(trans.FmtPeriodsWide) == 0 && baseFound {
trans.FmtPeriodsWide = base.FmtPeriodsWide
}
// era values
if len(trans.FmtErasAbbreviated) == 0 && inheritedFound {
trans.FmtErasAbbreviated = inherited.FmtErasAbbreviated
}
if len(trans.FmtErasAbbreviated) == 0 && baseFound {
trans.FmtErasAbbreviated = base.FmtErasAbbreviated
}
if len(trans.FmtErasNarrow) == 0 && inheritedFound {
trans.FmtErasNarrow = inherited.FmtErasNarrow
}
if len(trans.FmtErasNarrow) == 0 && baseFound {
trans.FmtErasNarrow = base.FmtErasNarrow
}
if len(trans.FmtErasWide) == 0 && inheritedFound {
trans.FmtErasWide = inherited.FmtErasWide
}
if len(trans.FmtErasWide) == 0 && baseFound {
trans.FmtErasWide = base.FmtErasWide
}
ldml := cldr.RawLDML(trans.Locale)
currencies := make([]string, len(globalCurrencies), len(globalCurrencies))
var kval string
for k, v := range globCurrencyIdxMap {
kval = k
// if kval[:len(kval)-1] != " " {
// kval += " "
// }
currencies[v] = kval
}
// some just have no data...
if ldml.Numbers != nil {
if ldml.Numbers.Currencies != nil {
for _, currency := range ldml.Numbers.Currencies.Currency {
if len(currency.Symbol) == 0 {
continue
}
if len(currency.Symbol[0].Data()) == 0 {
continue
}
if len(currency.Type) == 0 {
continue
}
currencies[globCurrencyIdxMap[currency.Type]] = currency.Symbol[0].Data()
}
}
}
trans.Currencies = fmt.Sprintf("%#v", currencies)
// timezones
if (trans.timezones == nil || len(trans.timezones) == 0) && inheritedFound {
trans.timezones = inherited.timezones
}
if (trans.timezones == nil || len(trans.timezones) == 0) && baseFound {
trans.timezones = base.timezones
}
// make sure all inherited timezones are part of sub locale timezones
if inheritedFound {
var ok bool
for k, v := range inherited.timezones {
if _, ok = trans.timezones[k]; ok {
continue
}
trans.timezones[k] = v
}
}
// make sure all base timezones are part of sub locale timezones
if baseFound {
var ok bool
for k, v := range base.timezones {
if _, ok = trans.timezones[k]; ok {
continue
}
trans.timezones[k] = v
}
}
applyOverrides(trans)
parseDecimalNumberFormat(trans)
parsePercentNumberFormat(trans)
parseCurrencyNumberFormat(trans)
}
for _, trans := range translators {
fmt.Println("Final Processing:", trans.Locale)
// if it's still nill.....
if trans.timezones == nil {
trans.timezones = make(map[string]*zoneAbbrev)
}
tz := make(map[string]string) // key = abbrev locale eg. EST, EDT, MST, PST... value = long locale eg. Eastern Standard Time, Pacific Time.....
for k, v := range timezones {
ttz, ok := trans.timezones[k]
if !ok {
ttz = v
trans.timezones[k] = v
}
tz[v.standard] = ttz.standard
tz[v.daylight] = ttz.daylight
}
trans.FmtTimezones = fmt.Sprintf("%#v", tz)
if len(trans.TimeSeparator) == 0 {
trans.TimeSeparator = ":"
}
trans.FmtDateShort, trans.FmtDateMedium, trans.FmtDateLong, trans.FmtDateFull = parseDateFormats(trans, trans.FmtDateShort, trans.FmtDateMedium, trans.FmtDateLong, trans.FmtDateFull)
trans.FmtTimeShort, trans.FmtTimeMedium, trans.FmtTimeLong, trans.FmtTimeFull = parseDateFormats(trans, trans.FmtTimeShort, trans.FmtTimeMedium, trans.FmtTimeLong, trans.FmtTimeFull)
}
}
// preprocesses maps, array etc... just requires multiple passes no choice....
func preProcess(cldrVar *cldr.CLDR) {
for _, l := range cldrVar.Locales() {
fmt.Println("Pre Processing:", l)
split := strings.SplitN(l, "_", 2)
baseLocale := split[0]
// inheritedLocale := baseLocale
// // one of the inherited english locales
// // http://cldr.unicode.org/development/development-process/design-proposals/english-inheritance
// if l == "en_001" || l == "en_GB" {
// inheritedLocale = l
// }
trans := &translator{
Locale: l,
BaseLocale: baseLocale,
// InheritedLocale: inheritedLocale,
}
// if is a base locale
if len(split) == 1 {
baseTranslators[baseLocale] = trans
}
// baseTranslators[l] = trans
// baseTranslators[baseLocale] = trans // allowing for unofficial fallback if none exists
translators[l] = trans
// get number, currency and datetime symbols
// number values
ldml := cldrVar.RawLDML(l)
// some just have no data...
if ldml.Numbers != nil {
if len(ldml.Numbers.Symbols) > 0 {
symbol := ldml.Numbers.Symbols[0]
// Try to get the default numbering system instead of the first one
systems := ldml.Numbers.DefaultNumberingSystem
// There shouldn't really be more than one DefaultNumberingSystem
if len(systems) > 0 {
if dns := systems[0].Data(); dns != "" {
for k := range ldml.Numbers.Symbols {
if ldml.Numbers.Symbols[k].NumberSystem == dns {
symbol = ldml.Numbers.Symbols[k]
break
}
}
}
}
if len(symbol.Decimal) > 0 {
trans.Decimal = symbol.Decimal[0].Data()
}
if len(symbol.Group) > 0 {
trans.Group = symbol.Group[0].Data()
}
if len(symbol.MinusSign) > 0 {
trans.Minus = symbol.MinusSign[0].Data()
}
if len(symbol.PercentSign) > 0 {
trans.Percent = symbol.PercentSign[0].Data()
}
if len(symbol.PerMille) > 0 {
trans.PerMille = symbol.PerMille[0].Data()
}
if len(symbol.TimeSeparator) > 0 {
trans.TimeSeparator = symbol.TimeSeparator[0].Data()
}
if len(symbol.Infinity) > 0 {
trans.Infinity = symbol.Infinity[0].Data()
}
}
if ldml.Numbers.Currencies != nil {
for _, currency := range ldml.Numbers.Currencies.Currency {
if len(strings.TrimSpace(currency.Type)) == 0 {
continue
}
globalCurrenciesMap[currency.Type] = struct{}{}
}
}
if len(ldml.Numbers.DecimalFormats) > 0 && len(ldml.Numbers.DecimalFormats[0].DecimalFormatLength) > 0 {
for _, dfl := range ldml.Numbers.DecimalFormats[0].DecimalFormatLength {
if len(dfl.Type) == 0 {
trans.DecimalNumberFormat = dfl.DecimalFormat[0].Pattern[0].Data()
break
}
}
}
if len(ldml.Numbers.PercentFormats) > 0 && len(ldml.Numbers.PercentFormats[0].PercentFormatLength) > 0 {
for _, dfl := range ldml.Numbers.PercentFormats[0].PercentFormatLength {
if len(dfl.Type) == 0 {
trans.PercentNumberFormat = dfl.PercentFormat[0].Pattern[0].Data()
break
}
}
}
if len(ldml.Numbers.CurrencyFormats) > 0 && len(ldml.Numbers.CurrencyFormats[0].CurrencyFormatLength) > 0 {
if len(ldml.Numbers.CurrencyFormats[0].CurrencyFormatLength[0].CurrencyFormat) > 1 {
split := strings.SplitN(ldml.Numbers.CurrencyFormats[0].CurrencyFormatLength[0].CurrencyFormat[1].Pattern[0].Data(), ";", 2)
trans.CurrencyNumberFormat = split[0]
if len(split) > 1 && len(split[1]) > 0 {
trans.NegativeCurrencyNumberFormat = split[1]
} else {
trans.NegativeCurrencyNumberFormat = trans.CurrencyNumberFormat
}
} else {
trans.CurrencyNumberFormat = ldml.Numbers.CurrencyFormats[0].CurrencyFormatLength[0].CurrencyFormat[0].Pattern[0].Data()
trans.NegativeCurrencyNumberFormat = trans.CurrencyNumberFormat
}
}
}
if ldml.Dates != nil {
if ldml.Dates.TimeZoneNames != nil {
for _, zone := range ldml.Dates.TimeZoneNames.Metazone {
for _, short := range zone.Short {
if len(short.Standard) > 0 {
za, ok := timezones[zone.Type]
if !ok {
za = new(zoneAbbrev)
timezones[zone.Type] = za
}
za.standard = short.Standard[0].Data()
}
if len(short.Daylight) > 0 {
za, ok := timezones[zone.Type]
if !ok {
za = new(zoneAbbrev)
timezones[zone.Type] = za
}
za.daylight = short.Daylight[0].Data()
}
}
for _, long := range zone.Long {
if trans.timezones == nil {
trans.timezones = make(map[string]*zoneAbbrev)
}
if len(long.Standard) > 0 {
za, ok := trans.timezones[zone.Type]
if !ok {
za = new(zoneAbbrev)
trans.timezones[zone.Type] = za
}
za.standard = long.Standard[0].Data()
}
za, ok := trans.timezones[zone.Type]
if !ok {
za = new(zoneAbbrev)
trans.timezones[zone.Type] = za
}
if len(long.Daylight) > 0 {
za.daylight = long.Daylight[0].Data()
} else {