generate_resources.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "go/format"
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "sync"
  11. "text/template"
  12. "time"
  13. "gopkg.in/yaml.v2"
  14. "golang.org/x/text/unicode/cldr"
  15. i18n "github.com/go-playground/universal-translator"
  16. )
  17. // numbers:
  18. // symbols:
  19. // decimal: .
  20. // group: ','
  21. // negative: '-'
  22. // percent: '%'
  23. // permille: "\u2030"
  24. // formats:
  25. // decimal: '#,##0.###'
  26. // currency: "\xA4#,##0.00;(\xA4#,##0.00)"
  27. // percent: '#,##0%'
  28. // currencies:
  29. // JPY:
  30. // symbol: "\xA5"
  31. // USD:
  32. // symbol: $
  33. type pluralInfo struct {
  34. path string
  35. locale string
  36. plural string
  37. }
  38. func main() {
  39. //plurals
  40. rules := "data/rules"
  41. plurals := map[string]*pluralInfo{}
  42. basePlurals := map[string]string{}
  43. err := filepath.Walk(rules, func(path string, info os.FileInfo, err error) error {
  44. if err != nil {
  45. panic(err)
  46. }
  47. if info.IsDir() {
  48. return nil
  49. }
  50. in, err := ioutil.ReadFile(path)
  51. if err != nil {
  52. panic(err)
  53. }
  54. var out yaml.MapSlice
  55. if err = yaml.Unmarshal(in, &out); err != nil {
  56. panic(err)
  57. }
  58. var plural string
  59. for _, item := range out {
  60. if item.Key == "plural" {
  61. plural = item.Value.(string)
  62. }
  63. }
  64. locale := strings.Replace(info.Name(), filepath.Ext(info.Name()), "", 1)
  65. locale = strings.ToLower(strings.Replace(locale, "-", "_", -1))
  66. plurals[locale] = &pluralInfo{
  67. path: path,
  68. locale: locale,
  69. plural: plural,
  70. }
  71. if plural == "" {
  72. return nil
  73. }
  74. basePlurals[locale] = plural
  75. return nil
  76. })
  77. if err != nil {
  78. panic(err)
  79. }
  80. for _, p := range plurals {
  81. if p.plural == "" {
  82. var ok bool
  83. fmt.Print("can't find plurals in ", p.path, " attempting to locate base language plural rules...")
  84. base := strings.SplitN(p.locale, "_", 2)
  85. p.plural, ok = basePlurals[base[0]]
  86. if !ok {
  87. fmt.Println("Not Found")
  88. continue
  89. }
  90. fmt.Println("Found")
  91. }
  92. }
  93. // cldr
  94. var decoder cldr.Decoder
  95. cldr, err := decoder.DecodePath("data/core")
  96. if err != nil {
  97. panic(err)
  98. }
  99. locs := map[string]string{}
  100. numbers := map[string]i18n.Number{}
  101. calendars := map[string]i18n.Calendar{}
  102. locales := cldr.Locales()
  103. for _, loc := range locales {
  104. ldml := cldr.RawLDML(loc)
  105. if ldml.Numbers == nil {
  106. continue
  107. }
  108. var number i18n.Number
  109. number.Currencies = make(i18n.CurrencyFormatValue)
  110. if len(ldml.Numbers.Symbols) > 0 {
  111. symbol := ldml.Numbers.Symbols[0]
  112. if len(symbol.Decimal) > 0 {
  113. number.Symbols.Decimal = symbol.Decimal[0].Data()
  114. }
  115. if len(symbol.Group) > 0 {
  116. number.Symbols.Group = symbol.Group[0].Data()
  117. }
  118. if len(symbol.MinusSign) > 0 {
  119. number.Symbols.Negative = symbol.MinusSign[0].Data()
  120. }
  121. if len(symbol.PercentSign) > 0 {
  122. number.Symbols.Percent = symbol.PercentSign[0].Data()
  123. }
  124. if len(symbol.PerMille) > 0 {
  125. number.Symbols.PerMille = symbol.PerMille[0].Data()
  126. }
  127. }
  128. if len(ldml.Numbers.DecimalFormats) > 0 && len(ldml.Numbers.DecimalFormats[0].DecimalFormatLength) > 0 {
  129. number.Formats.Decimal = ldml.Numbers.DecimalFormats[0].DecimalFormatLength[0].DecimalFormat[0].Pattern[0].Data()
  130. }
  131. if len(ldml.Numbers.CurrencyFormats) > 0 && len(ldml.Numbers.CurrencyFormats[0].CurrencyFormatLength) > 0 {
  132. number.Formats.Currency = ldml.Numbers.CurrencyFormats[0].CurrencyFormatLength[0].CurrencyFormat[0].Pattern[0].Data()
  133. number.Formats.CurrencyAccounting = number.Formats.Currency
  134. if len(ldml.Numbers.CurrencyFormats[0].CurrencyFormatLength[0].CurrencyFormat) > 1 {
  135. number.Formats.CurrencyAccounting = ldml.Numbers.CurrencyFormats[0].CurrencyFormatLength[0].CurrencyFormat[1].Pattern[0].Data()
  136. }
  137. }
  138. if len(ldml.Numbers.PercentFormats) > 0 && len(ldml.Numbers.PercentFormats[0].PercentFormatLength) > 0 {
  139. number.Formats.Percent = ldml.Numbers.PercentFormats[0].PercentFormatLength[0].PercentFormat[0].Pattern[0].Data()
  140. }
  141. if ldml.Numbers.Currencies != nil {
  142. for _, currency := range ldml.Numbers.Currencies.Currency {
  143. var c i18n.Currency
  144. c.Currency = currency.Type
  145. if len(currency.DisplayName) > 0 {
  146. c.DisplayName = currency.DisplayName[0].Data()
  147. }
  148. if len(currency.Symbol) > 0 {
  149. c.Symbol = currency.Symbol[0].Data()
  150. }
  151. number.Currencies[c.Currency] = c
  152. }
  153. }
  154. numbers[loc] = number
  155. locs[loc] = strings.ToLower(strings.Replace(loc, "_", "", -1))
  156. if ldml.Dates != nil && ldml.Dates.Calendars != nil {
  157. var calendar i18n.Calendar
  158. ldmlCar := ldml.Dates.Calendars.Calendar[0]
  159. for _, cal := range ldml.Dates.Calendars.Calendar {
  160. if cal.Type == "gregorian" {
  161. ldmlCar = cal
  162. }
  163. }
  164. if ldmlCar.DateFormats != nil {
  165. for _, datefmt := range ldmlCar.DateFormats.DateFormatLength {
  166. switch datefmt.Type {
  167. case "full":
  168. calendar.Formats.Date.Full = datefmt.DateFormat[0].Pattern[0].Data()
  169. case "long":
  170. calendar.Formats.Date.Long = datefmt.DateFormat[0].Pattern[0].Data()
  171. case "medium":
  172. calendar.Formats.Date.Medium = datefmt.DateFormat[0].Pattern[0].Data()
  173. case "short":
  174. calendar.Formats.Date.Short = datefmt.DateFormat[0].Pattern[0].Data()
  175. }
  176. }
  177. }
  178. if ldmlCar.TimeFormats != nil {
  179. for _, datefmt := range ldmlCar.TimeFormats.TimeFormatLength {
  180. switch datefmt.Type {
  181. case "full":
  182. calendar.Formats.Time.Full = datefmt.TimeFormat[0].Pattern[0].Data()
  183. case "long":
  184. calendar.Formats.Time.Long = datefmt.TimeFormat[0].Pattern[0].Data()
  185. case "medium":
  186. calendar.Formats.Time.Medium = datefmt.TimeFormat[0].Pattern[0].Data()
  187. case "short":
  188. calendar.Formats.Time.Short = datefmt.TimeFormat[0].Pattern[0].Data()
  189. }
  190. }
  191. }
  192. if ldmlCar.DateTimeFormats != nil {
  193. for _, datefmt := range ldmlCar.DateTimeFormats.DateTimeFormatLength {
  194. switch datefmt.Type {
  195. case "full":
  196. calendar.Formats.DateTime.Full = datefmt.DateTimeFormat[0].Pattern[0].Data()
  197. case "long":
  198. calendar.Formats.DateTime.Long = datefmt.DateTimeFormat[0].Pattern[0].Data()
  199. case "medium":
  200. calendar.Formats.DateTime.Medium = datefmt.DateTimeFormat[0].Pattern[0].Data()
  201. case "short":
  202. calendar.Formats.DateTime.Short = datefmt.DateTimeFormat[0].Pattern[0].Data()
  203. }
  204. }
  205. }
  206. if ldmlCar.Months != nil {
  207. for _, monthctx := range ldmlCar.Months.MonthContext {
  208. for _, months := range monthctx.MonthWidth {
  209. i18nMonth := make(i18n.CalendarMonthFormatNameValue)
  210. for _, m := range months.Month {
  211. switch m.Type {
  212. case "1":
  213. i18nMonth[time.January] = m.Data()
  214. case "2":
  215. i18nMonth[time.February] = m.Data()
  216. case "3":
  217. i18nMonth[time.March] = m.Data()
  218. case "4":
  219. i18nMonth[time.April] = m.Data()
  220. case "5":
  221. i18nMonth[time.May] = m.Data()
  222. case "6":
  223. i18nMonth[time.June] = m.Data()
  224. case "7":
  225. i18nMonth[time.July] = m.Data()
  226. case "8":
  227. i18nMonth[time.August] = m.Data()
  228. case "9":
  229. i18nMonth[time.September] = m.Data()
  230. case "10":
  231. i18nMonth[time.October] = m.Data()
  232. case "11":
  233. i18nMonth[time.November] = m.Data()
  234. case "12":
  235. i18nMonth[time.December] = m.Data()
  236. }
  237. }
  238. switch months.Type {
  239. case "abbreviated":
  240. calendar.FormatNames.Months.Abbreviated = i18nMonth
  241. case "narrow":
  242. calendar.FormatNames.Months.Narrow = i18nMonth
  243. case "short":
  244. calendar.FormatNames.Months.Short = i18nMonth
  245. case "wide":
  246. calendar.FormatNames.Months.Wide = i18nMonth
  247. }
  248. }
  249. }
  250. }
  251. if ldmlCar.Days != nil {
  252. for _, dayctx := range ldmlCar.Days.DayContext {
  253. for _, days := range dayctx.DayWidth {
  254. i18nDay := make(i18n.CalendarDayFormatNameValue)
  255. for _, d := range days.Day {
  256. switch d.Type {
  257. case "sun":
  258. i18nDay[time.Sunday] = d.Data()
  259. case "mon":
  260. i18nDay[time.Monday] = d.Data()
  261. case "tue":
  262. i18nDay[time.Tuesday] = d.Data()
  263. case "wed":
  264. i18nDay[time.Wednesday] = d.Data()
  265. case "thu":
  266. i18nDay[time.Thursday] = d.Data()
  267. case "fri":
  268. i18nDay[time.Friday] = d.Data()
  269. case "sat":
  270. i18nDay[time.Saturday] = d.Data()
  271. }
  272. }
  273. switch days.Type {
  274. case "abbreviated":
  275. calendar.FormatNames.Days.Abbreviated = i18nDay
  276. case "narrow":
  277. calendar.FormatNames.Days.Narrow = i18nDay
  278. case "short":
  279. calendar.FormatNames.Days.Short = i18nDay
  280. case "wide":
  281. calendar.FormatNames.Days.Wide = i18nDay
  282. }
  283. }
  284. }
  285. }
  286. if ldmlCar.DayPeriods != nil {
  287. for _, ctx := range ldmlCar.DayPeriods.DayPeriodContext {
  288. for _, width := range ctx.DayPeriodWidth {
  289. // var i18nPeriod i18n.CalendarPeriodFormatNameValue
  290. i18nPeriod := make(i18n.CalendarPeriodFormatNameValue)
  291. for _, d := range width.DayPeriod {
  292. if _, ok := i18nPeriod[d.Type]; !ok {
  293. i18nPeriod[d.Type] = d.Data()
  294. }
  295. }
  296. switch width.Type {
  297. case "abbreviated":
  298. calendar.FormatNames.Periods.Abbreviated = i18nPeriod
  299. case "narrow":
  300. calendar.FormatNames.Periods.Narrow = i18nPeriod
  301. case "short":
  302. calendar.FormatNames.Periods.Short = i18nPeriod
  303. case "wide":
  304. calendar.FormatNames.Periods.Wide = i18nPeriod
  305. }
  306. }
  307. }
  308. // var empty i18n.CalendarPeriodFormatNameValue
  309. // if calendar.FormatNames.Periods.Abbreviated == empty {
  310. // calendar.FormatNames.Periods.Abbreviated = calendar.FormatNames.Periods.Wide
  311. // }
  312. }
  313. calendars[loc] = calendar
  314. }
  315. }
  316. for locale := range locs {
  317. if !strings.Contains(locale, "_") {
  318. continue
  319. }
  320. calendar := calendars[locale]
  321. bString := strings.SplitN(locale, "_", 2)
  322. base := bString[0]
  323. baseCal := calendars[base]
  324. // copy base calendar objects
  325. // Dates
  326. if calendar.Formats.Date.Full == "" {
  327. calendar.Formats.Date.Full = baseCal.Formats.Date.Full
  328. }
  329. if calendar.Formats.Date.Long == "" {
  330. calendar.Formats.Date.Long = baseCal.Formats.Date.Long
  331. }
  332. if calendar.Formats.Date.Medium == "" {
  333. calendar.Formats.Date.Medium = baseCal.Formats.Date.Medium
  334. }
  335. if calendar.Formats.Date.Short == "" {
  336. calendar.Formats.Date.Short = baseCal.Formats.Date.Short
  337. }
  338. // times
  339. if calendar.Formats.Time.Full == "" {
  340. calendar.Formats.Time.Full = baseCal.Formats.Time.Full
  341. }
  342. if calendar.Formats.Time.Long == "" {
  343. calendar.Formats.Time.Long = baseCal.Formats.Time.Long
  344. }
  345. if calendar.Formats.Time.Medium == "" {
  346. calendar.Formats.Time.Medium = baseCal.Formats.Time.Medium
  347. }
  348. if calendar.Formats.Time.Short == "" {
  349. calendar.Formats.Time.Short = baseCal.Formats.Time.Short
  350. }
  351. // date & times
  352. if calendar.Formats.DateTime.Full == "" {
  353. calendar.Formats.DateTime.Full = baseCal.Formats.DateTime.Full
  354. }
  355. if calendar.Formats.DateTime.Long == "" {
  356. calendar.Formats.DateTime.Long = baseCal.Formats.DateTime.Long
  357. }
  358. if calendar.Formats.DateTime.Medium == "" {
  359. calendar.Formats.DateTime.Medium = baseCal.Formats.DateTime.Medium
  360. }
  361. if calendar.Formats.DateTime.Short == "" {
  362. calendar.Formats.DateTime.Short = baseCal.Formats.DateTime.Short
  363. }
  364. // months
  365. if calendar.FormatNames.Months.Abbreviated == nil {
  366. calendar.FormatNames.Months.Abbreviated = make(i18n.CalendarMonthFormatNameValue)
  367. }
  368. if calendar.FormatNames.Months.Narrow == nil {
  369. calendar.FormatNames.Months.Narrow = make(i18n.CalendarMonthFormatNameValue)
  370. }
  371. if calendar.FormatNames.Months.Short == nil {
  372. calendar.FormatNames.Months.Short = make(i18n.CalendarMonthFormatNameValue)
  373. }
  374. if calendar.FormatNames.Months.Wide == nil {
  375. calendar.FormatNames.Months.Wide = make(i18n.CalendarMonthFormatNameValue)
  376. }
  377. for k, v := range baseCal.FormatNames.Months.Abbreviated {
  378. val, ok := calendar.FormatNames.Months.Abbreviated[k]
  379. if !ok {
  380. calendar.FormatNames.Months.Abbreviated[k] = v
  381. continue
  382. }
  383. if val == "" {
  384. calendar.FormatNames.Months.Abbreviated[k] = v
  385. }
  386. }
  387. for k, v := range baseCal.FormatNames.Months.Narrow {
  388. val, ok := calendar.FormatNames.Months.Narrow[k]
  389. if !ok {
  390. calendar.FormatNames.Months.Narrow[k] = v
  391. continue
  392. }
  393. if val == "" {
  394. calendar.FormatNames.Months.Narrow[k] = v
  395. }
  396. }
  397. for k, v := range baseCal.FormatNames.Months.Short {
  398. val, ok := calendar.FormatNames.Months.Short[k]
  399. if !ok {
  400. calendar.FormatNames.Months.Short[k] = v
  401. continue
  402. }
  403. if val == "" {
  404. calendar.FormatNames.Months.Short[k] = v
  405. }
  406. }
  407. for k, v := range baseCal.FormatNames.Months.Wide {
  408. val, ok := calendar.FormatNames.Months.Wide[k]
  409. if !ok {
  410. calendar.FormatNames.Months.Wide[k] = v
  411. continue
  412. }
  413. if val == "" {
  414. calendar.FormatNames.Months.Wide[k] = v
  415. }
  416. }
  417. // days
  418. if calendar.FormatNames.Days.Abbreviated == nil {
  419. calendar.FormatNames.Days.Abbreviated = make(i18n.CalendarDayFormatNameValue)
  420. }
  421. if calendar.FormatNames.Days.Narrow == nil {
  422. calendar.FormatNames.Days.Narrow = make(i18n.CalendarDayFormatNameValue)
  423. }
  424. if calendar.FormatNames.Days.Short == nil {
  425. calendar.FormatNames.Days.Short = make(i18n.CalendarDayFormatNameValue)
  426. }
  427. if calendar.FormatNames.Days.Wide == nil {
  428. calendar.FormatNames.Days.Wide = make(i18n.CalendarDayFormatNameValue)
  429. }
  430. for k, v := range baseCal.FormatNames.Days.Abbreviated {
  431. val, ok := calendar.FormatNames.Days.Abbreviated[k]
  432. if !ok {
  433. calendar.FormatNames.Days.Abbreviated[k] = v
  434. continue
  435. }
  436. if val == "" {
  437. calendar.FormatNames.Days.Abbreviated[k] = v
  438. }
  439. }
  440. for k, v := range baseCal.FormatNames.Days.Narrow {
  441. val, ok := calendar.FormatNames.Days.Narrow[k]
  442. if !ok {
  443. calendar.FormatNames.Days.Narrow[k] = v
  444. continue
  445. }
  446. if val == "" {
  447. calendar.FormatNames.Days.Narrow[k] = v
  448. }
  449. }
  450. for k, v := range baseCal.FormatNames.Days.Short {
  451. val, ok := calendar.FormatNames.Days.Short[k]
  452. if !ok {
  453. calendar.FormatNames.Days.Short[k] = v
  454. continue
  455. }
  456. if val == "" {
  457. calendar.FormatNames.Days.Short[k] = v
  458. }
  459. }
  460. for k, v := range baseCal.FormatNames.Days.Wide {
  461. val, ok := calendar.FormatNames.Days.Wide[k]
  462. if !ok {
  463. calendar.FormatNames.Days.Wide[k] = v
  464. continue
  465. }
  466. if val == "" {
  467. calendar.FormatNames.Days.Wide[k] = v
  468. }
  469. }
  470. // periods
  471. if calendar.FormatNames.Periods.Abbreviated == nil {
  472. calendar.FormatNames.Periods.Abbreviated = make(i18n.CalendarPeriodFormatNameValue)
  473. }
  474. if calendar.FormatNames.Periods.Narrow == nil {
  475. calendar.FormatNames.Periods.Narrow = make(i18n.CalendarPeriodFormatNameValue)
  476. }
  477. if calendar.FormatNames.Periods.Short == nil {
  478. calendar.FormatNames.Periods.Short = make(i18n.CalendarPeriodFormatNameValue)
  479. }
  480. if calendar.FormatNames.Periods.Wide == nil {
  481. calendar.FormatNames.Periods.Wide = make(i18n.CalendarPeriodFormatNameValue)
  482. }
  483. for k, v := range baseCal.FormatNames.Periods.Abbreviated {
  484. val, ok := calendar.FormatNames.Periods.Abbreviated[k]
  485. if !ok {
  486. calendar.FormatNames.Periods.Abbreviated[k] = v
  487. continue
  488. }
  489. if val == "" {
  490. calendar.FormatNames.Periods.Abbreviated[k] = v
  491. }
  492. }
  493. for k, v := range baseCal.FormatNames.Periods.Narrow {
  494. val, ok := calendar.FormatNames.Periods.Narrow[k]
  495. if !ok {
  496. calendar.FormatNames.Periods.Narrow[k] = v
  497. continue
  498. }
  499. if val == "" {
  500. calendar.FormatNames.Periods.Narrow[k] = v
  501. }
  502. }
  503. for k, v := range baseCal.FormatNames.Periods.Short {
  504. val, ok := calendar.FormatNames.Periods.Short[k]
  505. if !ok {
  506. calendar.FormatNames.Periods.Short[k] = v
  507. continue
  508. }
  509. if val == "" {
  510. calendar.FormatNames.Periods.Short[k] = v
  511. }
  512. }
  513. for k, v := range baseCal.FormatNames.Periods.Wide {
  514. val, ok := calendar.FormatNames.Periods.Wide[k]
  515. if !ok {
  516. calendar.FormatNames.Periods.Wide[k] = v
  517. continue
  518. }
  519. if val == "" {
  520. calendar.FormatNames.Periods.Wide[k] = v
  521. }
  522. }
  523. calendars[locale] = calendar
  524. number := numbers[locale]
  525. baseNum := numbers[base]
  526. // symbols
  527. if number.Symbols.Decimal == "" {
  528. number.Symbols.Decimal = baseNum.Symbols.Decimal
  529. }
  530. if number.Symbols.Group == "" {
  531. number.Symbols.Group = baseNum.Symbols.Group
  532. }
  533. if number.Symbols.Negative == "" {
  534. number.Symbols.Negative = baseNum.Symbols.Negative
  535. }
  536. if number.Symbols.Percent == "" {
  537. number.Symbols.Percent = baseNum.Symbols.Percent
  538. }
  539. if number.Symbols.PerMille == "" {
  540. number.Symbols.PerMille = baseNum.Symbols.PerMille
  541. }
  542. // formats
  543. if number.Formats.Decimal == "" {
  544. number.Formats.Decimal = baseNum.Formats.Decimal
  545. }
  546. if number.Formats.Currency == "" {
  547. number.Formats.Currency = baseNum.Formats.Currency
  548. }
  549. if number.Formats.CurrencyAccounting == "" {
  550. number.Formats.CurrencyAccounting = baseNum.Formats.CurrencyAccounting
  551. }
  552. if number.Formats.Percent == "" {
  553. number.Formats.Percent = baseNum.Formats.Percent
  554. }
  555. // currency
  556. for k, v := range baseNum.Currencies {
  557. val, ok := number.Currencies[k]
  558. if !ok {
  559. // number.Currencies[k] = v
  560. continue
  561. }
  562. if val.Currency == "" {
  563. val.Currency = v.Currency
  564. }
  565. if val.DisplayName == "" {
  566. val.DisplayName = v.DisplayName
  567. }
  568. if val.Symbol == "" {
  569. val.Symbol = v.Symbol
  570. }
  571. number.Currencies[k] = val
  572. }
  573. numbers[locale] = number
  574. }
  575. var wg sync.WaitGroup
  576. wg.Add(len(numbers))
  577. for locale, number := range numbers {
  578. go func(locale string, number i18n.Number) {
  579. localeLowercase := strings.ToLower(locale)
  580. defer func() { wg.Done() }()
  581. path := "../../resources/locales/" + locale
  582. if _, err := os.Stat(path); err != nil {
  583. if err = os.MkdirAll(path, 0777); err != nil {
  584. panic(err)
  585. }
  586. }
  587. path += "/"
  588. mainFile, err := os.Create(path + "main.go")
  589. if err != nil {
  590. panic(err)
  591. }
  592. defer mainFile.Close()
  593. calendar := calendars[locale]
  594. mainCodes, err := format.Source([]byte(fmt.Sprintf(`package %s
  595. import "github.com/go-playground/universal-translator"
  596. var locale = &ut.Locale{
  597. Locale: %q,
  598. Number: ut.Number{
  599. Symbols: symbols,
  600. Formats: formats,
  601. Currencies: currencies,
  602. },
  603. Calendar: calendar,
  604. PluralRule: pluralRule,
  605. }
  606. func init() {
  607. ut.RegisterLocale(locale)
  608. }
  609. `, locale, locale)))
  610. if err != nil {
  611. panic(err)
  612. }
  613. fmt.Fprintf(mainFile, "%s", mainCodes)
  614. numberFile, err := os.Create(path + "number.go")
  615. if err != nil {
  616. panic(err)
  617. }
  618. defer numberFile.Close()
  619. numberCodes, err := format.Source([]byte(fmt.Sprintf(`package %s
  620. import "github.com/go-playground/universal-translator"
  621. var (
  622. symbols = %#v
  623. formats = %#v
  624. )
  625. `, locale, number.Symbols, number.Formats)))
  626. if err != nil {
  627. panic(err)
  628. }
  629. fmt.Fprintf(numberFile, "%s", numberCodes)
  630. currencyFile, err := os.Create(path + "currency.go")
  631. if err != nil {
  632. panic(err)
  633. }
  634. defer currencyFile.Close()
  635. currencyCodes, err := format.Source([]byte(fmt.Sprintf(`package %s
  636. import "github.com/go-playground/universal-translator"
  637. var currencies = %# v
  638. `, locale, number.Currencies)))
  639. if err != nil {
  640. panic(err)
  641. }
  642. fmt.Fprintf(currencyFile, "%s", currencyCodes)
  643. calendarFile, err := os.Create(path + "calendar.go")
  644. if err != nil {
  645. panic(err)
  646. }
  647. defer calendarFile.Close()
  648. calendarCodes, err := format.Source([]byte(fmt.Sprintf(`package %s
  649. import "github.com/go-playground/universal-translator"
  650. var calendar = %#v
  651. `, locale, calendar)))
  652. if err != nil {
  653. panic(err)
  654. }
  655. fmt.Fprintf(calendarFile, "%s", calendarCodes)
  656. var ok bool
  657. pluralCode := "1"
  658. pInfo, ok := plurals[localeLowercase]
  659. if ok && pInfo.plural != "" {
  660. pluralCode = pInfo.plural
  661. }
  662. pluralFile, err := os.Create(path + "plural.go")
  663. if err != nil {
  664. panic(err)
  665. }
  666. defer pluralFile.Close()
  667. pluralCodes, err := format.Source([]byte(fmt.Sprintf(`package %s
  668. var pluralRule = %q
  669. `, locale, pluralCode)))
  670. if err != nil {
  671. panic(err)
  672. }
  673. fmt.Fprintf(pluralFile, "%s", pluralCodes)
  674. }(locale, number)
  675. }
  676. wg.Wait()
  677. // TODO: make switch with all of the locales + function to return new!
  678. localesFile, err := os.Create("../../resources/locales/all.go")
  679. if err != nil {
  680. panic(err)
  681. }
  682. defer localesFile.Close()
  683. tmpl, err := template.New("").Parse(`package locales
  684. // Imports for all locales
  685. import (
  686. {{range $locale, $_ := .}}// Locale "{{$locale}}" import that automatically registers itslef with the universal-translator package
  687. _ "github.com/go-playground/universal-translator/resources/locales/{{$locale}}"
  688. {{end}})
  689. `)
  690. if err != nil {
  691. panic(err)
  692. }
  693. var buf bytes.Buffer
  694. if err := tmpl.Execute(&buf, locs); err != nil {
  695. panic(err)
  696. }
  697. allCodes, err := format.Source(buf.Bytes())
  698. if err != nil {
  699. panic(err)
  700. }
  701. _, err = localesFile.Write(allCodes)
  702. if err != nil {
  703. panic(err)
  704. }
  705. }