generate_resources.go 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "os/exec"
  7. "sort"
  8. "strings"
  9. "github.com/go-playground/universal-translator/resources/locales"
  10. "golang.org/x/text/unicode/cldr"
  11. "text/template"
  12. )
  13. // numbers:
  14. // symbols:
  15. // decimal: .
  16. // group: ','
  17. // negative: '-'
  18. // percent: '%'
  19. // permille: "\u2030"
  20. // formats:
  21. // decimal: '#,##0.###'
  22. // currency: "\xA4#,##0.00;(\xA4#,##0.00)"
  23. // percent: '#,##0%'
  24. // currencies:
  25. // JPY:
  26. // symbol: "\xA5"
  27. // USD:
  28. // symbol: $
  29. // type pluralInfo struct {
  30. // path string
  31. // locale string
  32. // plural string
  33. // }
  34. // type translator struct {
  35. // locale string
  36. // }
  37. //
  38. const (
  39. locDir = "../../resources/locales/%s"
  40. locFilename = locDir + "/%s.go"
  41. )
  42. var (
  43. prVarFuncs = map[string]string{
  44. "n": "n := math.Abs(num)\n",
  45. "i": "i := int64(n)\n",
  46. // "v": "v := ...", // inherently available as argument
  47. "w": "w := locales.W(n, v)\n",
  48. "f": "f := locales.F(n, v)\n",
  49. "t": "t := locales.T(n, v)\n",
  50. }
  51. )
  52. type translator struct {
  53. Locale string
  54. Plurals string
  55. CardinalFunc string
  56. }
  57. var tmpl *template.Template
  58. func main() {
  59. var err error
  60. // load template
  61. tmpl, err = template.ParseGlob("*.tmpl")
  62. if err != nil {
  63. log.Fatal(err)
  64. }
  65. // load CLDR recourses
  66. var decoder cldr.Decoder
  67. cldr, err := decoder.DecodePath("data/core")
  68. if err != nil {
  69. panic(err)
  70. }
  71. for _, l := range cldr.Locales() {
  72. fmt.Println(l)
  73. baseLocale := strings.SplitN(l, "_", 2)[0]
  74. trans := &translator{
  75. Locale: l,
  76. }
  77. // plural rules
  78. trans.CardinalFunc, trans.Plurals = parseCardinalPluralRuleFunc(cldr, baseLocale)
  79. // // number values
  80. // ldml := cldr.RawLDML(l)
  81. // var decimal, group, minus, percent, permille string
  82. // // some just have no data...
  83. // if ldml.Numbers != nil {
  84. // symbol := ldml.Numbers.Symbols[0]
  85. // if len(symbol.Decimal) > 0 {
  86. // decimal = symbol.Decimal[0].Data()
  87. // }
  88. // if len(symbol.Group) > 0 {
  89. // group = symbol.Group[0].Data()
  90. // }
  91. // if len(symbol.MinusSign) > 0 {
  92. // minus = symbol.MinusSign[0].Data()
  93. // }
  94. // if len(symbol.PercentSign) > 0 {
  95. // percent = symbol.PercentSign[0].Data()
  96. // }
  97. // if len(symbol.PerMille) > 0 {
  98. // permille = symbol.PerMille[0].Data()
  99. // }
  100. // }
  101. // var decimalFormat, currencyFormat, currencyAccountingFormat, percentageFormat string
  102. // if len(ldml.Numbers.DecimalFormats) > 0 && len(ldml.Numbers.DecimalFormats[0].DecimalFormatLength) > 0 {
  103. // decimalFormat = ldml.Numbers.DecimalFormats[0].DecimalFormatLength[0].DecimalFormat[0].Pattern[0].Data()
  104. // }
  105. // if len(ldml.Numbers.CurrencyFormats) > 0 && len(ldml.Numbers.CurrencyFormats[0].CurrencyFormatLength) > 0 {
  106. // currencyFormat = ldml.Numbers.CurrencyFormats[0].CurrencyFormatLength[0].CurrencyFormat[0].Pattern[0].Data()
  107. // currencyAccountingFormat = currencyFormat
  108. // if len(ldml.Numbers.CurrencyFormats[0].CurrencyFormatLength[0].CurrencyFormat) > 1 {
  109. // currencyAccountingFormat = ldml.Numbers.CurrencyFormats[0].CurrencyFormatLength[0].CurrencyFormat[1].Pattern[0].Data()
  110. // }
  111. // }
  112. // if len(ldml.Numbers.PercentFormats) > 0 && len(ldml.Numbers.PercentFormats[0].PercentFormatLength) > 0 {
  113. // percentageFormat = ldml.Numbers.PercentFormats[0].PercentFormatLength[0].PercentFormat[0].Pattern[0].Data()
  114. // }
  115. // // parse Number values
  116. // parseNumbers(decimal, group, minus, percent, permille, decimalFormat, currencyFormat, currencyAccountingFormat, percentageFormat)
  117. // end number values
  118. if err = os.MkdirAll(fmt.Sprintf(locDir, l), 0777); err != nil {
  119. log.Fatal(err)
  120. }
  121. filename := fmt.Sprintf(locFilename, l, l)
  122. output, err := os.Create(filename)
  123. if err != nil {
  124. log.Fatal(err)
  125. }
  126. defer output.Close()
  127. if err := tmpl.ExecuteTemplate(output, "translator", trans); err != nil {
  128. log.Fatal(err)
  129. }
  130. output.Close()
  131. // after file written run gofmt on file to ensure best formatting
  132. cmd := exec.Command("gofmt", "-s", "-w", filename)
  133. if err = cmd.Run(); err != nil {
  134. log.Panic(err)
  135. }
  136. }
  137. }
  138. // func parseNumbers(decimal, group, minus, percent, permille, decimalFormat, currencyFormat, currencyAccountingFormat, percentageFormat string) {
  139. // if includeDecimalDigits {
  140. // nfMutex.RLock()
  141. // if format, exists := numberFormats[pattern]; exists {
  142. // nfMutex.RUnlock()
  143. // return format
  144. // }
  145. // nfMutex.RUnlock()
  146. // } else {
  147. // nfndMutex.RLock()
  148. // if format, exists := numberFormatsNoDecimals[pattern]; exists {
  149. // nfndMutex.RUnlock()
  150. // return format
  151. // }
  152. // nfndMutex.RUnlock()
  153. // }
  154. // format := new(numberFormat)
  155. // patterns := strings.Split(pattern, ";")
  156. // matches := prefixSuffixRegex.FindAllStringSubmatch(patterns[0], -1)
  157. // if len(matches) > 0 {
  158. // if len(matches[0]) > 1 {
  159. // format.positivePrefix = matches[0][1]
  160. // }
  161. // if len(matches[0]) > 2 {
  162. // format.positiveSuffix = matches[0][2]
  163. // }
  164. // }
  165. // // default values for negative prefix & suffix
  166. // format.negativePrefix = string(n.Symbols.Negative) + string(format.positivePrefix)
  167. // format.negativeSuffix = format.positiveSuffix
  168. // // see if they are in the pattern
  169. // if len(patterns) > 1 {
  170. // matches = prefixSuffixRegex.FindAllStringSubmatch(patterns[1], -1)
  171. // if len(matches) > 0 {
  172. // if len(matches[0]) > 1 {
  173. // format.negativePrefix = matches[0][1]
  174. // }
  175. // if len(matches[0]) > 2 {
  176. // format.negativeSuffix = matches[0][2]
  177. // }
  178. // }
  179. // }
  180. // pat := patterns[0]
  181. // if strings.Index(pat, "%") != -1 {
  182. // format.multiplier = 100
  183. // } else if strings.Index(pat, "‰") != -1 {
  184. // format.multiplier = 1000
  185. // } else {
  186. // format.multiplier = 1
  187. // }
  188. // pos := strings.Index(pat, ".")
  189. // if pos != -1 {
  190. // pos2 := strings.LastIndex(pat, "0")
  191. // if pos2 > pos {
  192. // format.minDecimalDigits = pos2 - pos
  193. // }
  194. // pos3 := strings.LastIndex(pat, "#")
  195. // if pos3 >= pos2 {
  196. // format.maxDecimalDigits = pos3 - pos
  197. // } else {
  198. // format.maxDecimalDigits = format.minDecimalDigits
  199. // }
  200. // pat = pat[0:pos]
  201. // }
  202. // p := strings.Replace(pat, ",", "", -1)
  203. // pos = strings.Index(p, "0")
  204. // if pos != -1 {
  205. // format.minIntegerDigits = strings.LastIndex(p, "0") - pos + 1
  206. // }
  207. // p = strings.Replace(pat, "#", "0", -1)
  208. // pos = strings.LastIndex(pat, ",")
  209. // if pos != -1 {
  210. // format.groupSizeFinal = strings.LastIndex(p, "0") - pos
  211. // pos2 := strings.LastIndex(p[0:pos], ",")
  212. // if pos2 != -1 {
  213. // format.groupSizeMain = pos - pos2 - 1
  214. // } else {
  215. // format.groupSizeMain = format.groupSizeFinal
  216. // }
  217. // }
  218. // if includeDecimalDigits {
  219. // nfMutex.Lock()
  220. // numberFormats[pattern] = format
  221. // nfMutex.Unlock()
  222. // return format
  223. // }
  224. // format.maxDecimalDigits = 0
  225. // format.minDecimalDigits = 0
  226. // nfndMutex.Lock()
  227. // numberFormatsNoDecimals[pattern] = format
  228. // nfndMutex.Unlock()
  229. // return format
  230. // }
  231. type sortRank struct {
  232. Rank uint8
  233. Value string
  234. }
  235. type ByRank []sortRank
  236. func (a ByRank) Len() int { return len(a) }
  237. func (a ByRank) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  238. func (a ByRank) Less(i, j int) bool { return a[i].Rank < a[j].Rank }
  239. // TODO: cleanup function logic perhaps write a lexer... but it's working right now, and
  240. // I'm already farther down the rabbit hole than I'd like and so pulling the chute here.
  241. func parseCardinalPluralRuleFunc(current *cldr.CLDR, baseLocale string) (results string, plurals string) {
  242. var prCardinal *struct {
  243. cldr.Common
  244. Locales string "xml:\"locales,attr\""
  245. PluralRule []*struct {
  246. cldr.Common
  247. Count string "xml:\"count,attr\""
  248. } "xml:\"pluralRule\""
  249. }
  250. var pluralArr []locales.PluralRule
  251. for _, p := range current.Supplemental().Plurals {
  252. for _, pr := range p.PluralRules {
  253. locs := strings.Split(pr.Locales, " ")
  254. for _, loc := range locs {
  255. if loc == baseLocale {
  256. prCardinal = pr
  257. }
  258. }
  259. }
  260. }
  261. // no plural rules for locale
  262. if prCardinal == nil {
  263. plurals = "nil"
  264. results = "return locales.PluralRuleUnknown,nil"
  265. return
  266. }
  267. vals := make(map[string]struct{})
  268. first := true
  269. // pre parse for variables
  270. for _, rule := range prCardinal.PluralRule {
  271. ps1 := pluralStringToString(rule.Count)
  272. psI := pluralStringToInt(rule.Count)
  273. pluralArr = append(pluralArr, psI)
  274. data := strings.Replace(strings.Replace(strings.Replace(strings.TrimSpace(strings.SplitN(rule.Common.Data(), "@", 2)[0]), " = ", " == ", -1), " or ", " || ", -1), " and ", " && ", -1)
  275. if len(data) == 0 {
  276. if len(prCardinal.PluralRule) == 1 {
  277. results = "return locales." + ps1
  278. } else {
  279. results += "\n\nreturn locales." + ps1
  280. // results += "else {\nreturn locales." + locales.PluralStringToString(rule.Count) + ", nil\n}"
  281. }
  282. continue
  283. }
  284. // // All need n, so always add
  285. // if strings.Contains(data, "n") {
  286. // vals[prVarFuncs["n"]] = struct{}{}
  287. // }
  288. if strings.Contains(data, "i") {
  289. vals[prVarFuncs["i"]] = struct{}{}
  290. }
  291. // v is inherently avaialable as an argument
  292. // if strings.Contains(data, "v") {
  293. // vals[prVarFuncs["v"]] = struct{}{}
  294. // }
  295. if strings.Contains(data, "w") {
  296. vals[prVarFuncs["w"]] = struct{}{}
  297. }
  298. if strings.Contains(data, "f") {
  299. vals[prVarFuncs["f"]] = struct{}{}
  300. }
  301. if strings.Contains(data, "t") {
  302. vals[prVarFuncs["t"]] = struct{}{}
  303. }
  304. if first {
  305. results += "if "
  306. first = false
  307. } else {
  308. results += "else if "
  309. }
  310. stmt := ""
  311. // real work here
  312. //
  313. // split by 'or' then by 'and' allowing to better
  314. // determine bracketing for formula
  315. ors := strings.Split(data, "||")
  316. for _, or := range ors {
  317. stmt += "("
  318. ands := strings.Split(strings.TrimSpace(or), "&&")
  319. for _, and := range ands {
  320. inArg := false
  321. pre := ""
  322. lft := ""
  323. preOperator := ""
  324. args := strings.Split(strings.TrimSpace(and), " ")
  325. for _, a := range args {
  326. if inArg {
  327. // check to see if is a value range 2..9
  328. multiRange := strings.Count(a, "..") > 1
  329. cargs := strings.Split(strings.TrimSpace(a), ",")
  330. hasBracket := len(cargs) > 1
  331. bracketAdded := false
  332. lastWasRange := false
  333. for _, carg := range cargs {
  334. if rng := strings.Split(carg, ".."); len(rng) > 1 {
  335. if multiRange {
  336. pre += " ("
  337. } else {
  338. pre += " "
  339. }
  340. switch preOperator {
  341. case "==":
  342. pre += lft + " >= " + rng[0] + " && " + lft + "<=" + rng[1]
  343. case "!=":
  344. pre += lft + " < " + rng[0] + " && " + lft + " > " + rng[1]
  345. }
  346. if multiRange {
  347. pre += ") || "
  348. } else {
  349. pre += " || "
  350. }
  351. lastWasRange = true
  352. continue
  353. }
  354. if lastWasRange {
  355. pre = strings.TrimRight(pre, " || ") + " && "
  356. }
  357. lastWasRange = false
  358. if hasBracket && !bracketAdded {
  359. pre += "("
  360. bracketAdded = true
  361. }
  362. // single comma separated values
  363. switch preOperator {
  364. case "==":
  365. pre += " " + lft + preOperator + carg + " || "
  366. case "!=":
  367. pre += " " + lft + preOperator + carg + " && "
  368. }
  369. }
  370. pre = strings.TrimRight(pre, " || ")
  371. pre = strings.TrimRight(pre, " && ")
  372. pre = strings.TrimRight(pre, " || ")
  373. if hasBracket && bracketAdded {
  374. pre += ")"
  375. }
  376. continue
  377. }
  378. if strings.Contains(a, "=") || a == ">" || a == "<" {
  379. inArg = true
  380. preOperator = a
  381. continue
  382. }
  383. lft += a
  384. }
  385. stmt += pre + " && "
  386. }
  387. stmt = strings.TrimRight(stmt, " && ") + ") || "
  388. }
  389. stmt = strings.TrimRight(stmt, " || ")
  390. results += stmt
  391. results += " {\n"
  392. // return plural rule here
  393. results += "return locales." + ps1 + "\n"
  394. results += "}"
  395. }
  396. pre := "\n"
  397. // always needed
  398. vals[prVarFuncs["n"]] = struct{}{}
  399. sorted := make([]sortRank, 0, len(vals))
  400. for k := range vals {
  401. switch k[:1] {
  402. case "n":
  403. sorted = append(sorted, sortRank{
  404. Value: prVarFuncs["n"],
  405. Rank: 1,
  406. })
  407. case "i":
  408. sorted = append(sorted, sortRank{
  409. Value: prVarFuncs["i"],
  410. Rank: 2,
  411. })
  412. case "w":
  413. sorted = append(sorted, sortRank{
  414. Value: prVarFuncs["w"],
  415. Rank: 3,
  416. })
  417. case "f":
  418. sorted = append(sorted, sortRank{
  419. Value: prVarFuncs["f"],
  420. Rank: 4,
  421. })
  422. case "t":
  423. sorted = append(sorted, sortRank{
  424. Value: prVarFuncs["t"],
  425. Rank: 5,
  426. })
  427. }
  428. }
  429. sort.Sort(ByRank(sorted))
  430. for _, k := range sorted {
  431. pre += k.Value
  432. }
  433. pre += "\n"
  434. if len(results) == 0 {
  435. results = "return locales.PluralRuleUnknown"
  436. }
  437. results = pre + results
  438. if len(pluralArr) == 0 {
  439. plurals = "nil"
  440. } else {
  441. plurals = fmt.Sprintf("%#v", pluralArr)
  442. }
  443. return
  444. }
  445. // pluralStringToInt returns the enum value of 'plural' provided
  446. func pluralStringToInt(plural string) locales.PluralRule {
  447. switch plural {
  448. case "zero":
  449. return locales.PluralRuleZero
  450. case "one":
  451. return locales.PluralRuleOne
  452. case "two":
  453. return locales.PluralRuleTwo
  454. case "few":
  455. return locales.PluralRuleFew
  456. case "many":
  457. return locales.PluralRuleMany
  458. case "other":
  459. return locales.PluralRuleOther
  460. default:
  461. return locales.PluralRuleUnknown
  462. }
  463. }
  464. func pluralStringToString(pr string) string {
  465. pr = strings.TrimSpace(pr)
  466. switch pr {
  467. case "zero":
  468. return "PluralRuleZero"
  469. case "one":
  470. return "PluralRuleOne"
  471. case "two":
  472. return "PluralRuleTwo"
  473. case "few":
  474. return "PluralRuleFew"
  475. case "many":
  476. return "PluralRuleMany"
  477. case "other":
  478. return "PluralRuleOther"
  479. default:
  480. return "PluralRuleUnknown"
  481. }
  482. }
  483. // //plurals
  484. // rules := "data/rules"
  485. // plurals := map[string]*pluralInfo{}
  486. // basePlurals := map[string]string{}
  487. // err := filepath.Walk(rules, func(path string, info os.FileInfo, err error) error {
  488. // if err != nil {
  489. // panic(err)
  490. // }
  491. // if info.IsDir() {
  492. // return nil
  493. // }
  494. // in, err := ioutil.ReadFile(path)
  495. // if err != nil {
  496. // panic(err)
  497. // }
  498. // var out yaml.MapSlice
  499. // if err = yaml.Unmarshal(in, &out); err != nil {
  500. // panic(err)
  501. // }
  502. // var plural string
  503. // for _, item := range out {
  504. // if item.Key == "plural" {
  505. // plural = item.Value.(string)
  506. // }
  507. // }
  508. // locale := strings.Replace(info.Name(), filepath.Ext(info.Name()), "", 1)
  509. // locale = strings.ToLower(strings.Replace(locale, "-", "_", -1))
  510. // plurals[locale] = &pluralInfo{
  511. // path: path,
  512. // locale: locale,
  513. // plural: plural,
  514. // }
  515. // if plural == "" {
  516. // return nil
  517. // }
  518. // basePlurals[locale] = plural
  519. // return nil
  520. // })
  521. // if err != nil {
  522. // panic(err)
  523. // }
  524. // for _, p := range plurals {
  525. // if p.plural == "" {
  526. // var ok bool
  527. // fmt.Print("can't find plurals in ", p.path, " attempting to locate base language plural rules...")
  528. // base := strings.SplitN(p.locale, "_", 2)
  529. // p.plural, ok = basePlurals[base[0]]
  530. // if !ok {
  531. // fmt.Println("Not Found")
  532. // continue
  533. // }
  534. // fmt.Println("Found")
  535. // }
  536. // }
  537. // cldr
  538. // var decoder cldr.Decoder
  539. // cldr, err := decoder.DecodePath("data/core")
  540. // if err != nil {
  541. // panic(err)
  542. // }
  543. // locs := map[string]string{}
  544. // numbers := map[string]i18n.Number{}
  545. // calendars := map[string]i18n.Calendar{}
  546. // locales := cldr.Locales()
  547. // for _, loc := range locales {
  548. // ldml := cldr.RawLDML(loc)
  549. // if ldml.Numbers == nil {
  550. // continue
  551. // }
  552. // var number i18n.Number
  553. // number.Currencies = make(i18n.CurrencyFormatValue)
  554. // if len(ldml.Numbers.Symbols) > 0 {
  555. // symbol := ldml.Numbers.Symbols[0]
  556. // if len(symbol.Decimal) > 0 {
  557. // number.Symbols.Decimal = symbol.Decimal[0].Data()
  558. // }
  559. // if len(symbol.Group) > 0 {
  560. // number.Symbols.Group = symbol.Group[0].Data()
  561. // }
  562. // if len(symbol.MinusSign) > 0 {
  563. // number.Symbols.Negative = symbol.MinusSign[0].Data()
  564. // }
  565. // if len(symbol.PercentSign) > 0 {
  566. // number.Symbols.Percent = symbol.PercentSign[0].Data()
  567. // }
  568. // if len(symbol.PerMille) > 0 {
  569. // number.Symbols.PerMille = symbol.PerMille[0].Data()
  570. // }
  571. // }
  572. // if len(ldml.Numbers.DecimalFormats) > 0 && len(ldml.Numbers.DecimalFormats[0].DecimalFormatLength) > 0 {
  573. // number.Formats.Decimal = ldml.Numbers.DecimalFormats[0].DecimalFormatLength[0].DecimalFormat[0].Pattern[0].Data()
  574. // }
  575. // if len(ldml.Numbers.CurrencyFormats) > 0 && len(ldml.Numbers.CurrencyFormats[0].CurrencyFormatLength) > 0 {
  576. // number.Formats.Currency = ldml.Numbers.CurrencyFormats[0].CurrencyFormatLength[0].CurrencyFormat[0].Pattern[0].Data()
  577. // number.Formats.CurrencyAccounting = number.Formats.Currency
  578. // if len(ldml.Numbers.CurrencyFormats[0].CurrencyFormatLength[0].CurrencyFormat) > 1 {
  579. // number.Formats.CurrencyAccounting = ldml.Numbers.CurrencyFormats[0].CurrencyFormatLength[0].CurrencyFormat[1].Pattern[0].Data()
  580. // }
  581. // }
  582. // if len(ldml.Numbers.PercentFormats) > 0 && len(ldml.Numbers.PercentFormats[0].PercentFormatLength) > 0 {
  583. // number.Formats.Percent = ldml.Numbers.PercentFormats[0].PercentFormatLength[0].PercentFormat[0].Pattern[0].Data()
  584. // }
  585. // if ldml.Numbers.Currencies != nil {
  586. // for _, currency := range ldml.Numbers.Currencies.Currency {
  587. // var c i18n.Currency
  588. // c.Currency = currency.Type
  589. // if len(currency.DisplayName) > 0 {
  590. // c.DisplayName = currency.DisplayName[0].Data()
  591. // }
  592. // if len(currency.Symbol) > 0 {
  593. // c.Symbol = currency.Symbol[0].Data()
  594. // }
  595. // number.Currencies[c.Currency] = c
  596. // }
  597. // }
  598. // numbers[loc] = number
  599. // locs[loc] = strings.ToLower(strings.Replace(loc, "_", "", -1))
  600. // if ldml.Dates != nil && ldml.Dates.Calendars != nil {
  601. // var calendar i18n.Calendar
  602. // ldmlCar := ldml.Dates.Calendars.Calendar[0]
  603. // for _, cal := range ldml.Dates.Calendars.Calendar {
  604. // if cal.Type == "gregorian" {
  605. // ldmlCar = cal
  606. // }
  607. // }
  608. // if ldmlCar.DateFormats != nil {
  609. // for _, datefmt := range ldmlCar.DateFormats.DateFormatLength {
  610. // switch datefmt.Type {
  611. // case "full":
  612. // calendar.Formats.DateEra.BC.Full = datefmt.DateFormat[0].Pattern[0].Data()
  613. // calendar.Formats.DateEra.AD.Full = calendar.Formats.DateEra.BC.Full
  614. // case "long":
  615. // calendar.Formats.DateEra.BC.Long = datefmt.DateFormat[0].Pattern[0].Data()
  616. // calendar.Formats.DateEra.AD.Long = calendar.Formats.DateEra.BC.Long
  617. // // Overrides TODO: Split Each Section into separate function, getting to big to maintain
  618. // case "medium":
  619. // calendar.Formats.DateEra.BC.Medium = datefmt.DateFormat[0].Pattern[0].Data()
  620. // calendar.Formats.DateEra.AD.Medium = calendar.Formats.DateEra.BC.Medium
  621. // case "short":
  622. // calendar.Formats.DateEra.BC.Short = datefmt.DateFormat[0].Pattern[0].Data()
  623. // calendar.Formats.DateEra.AD.Short = calendar.Formats.DateEra.BC.Short
  624. // }
  625. // }
  626. // // Overrides TODO: Split Each Section into separate function, getting to big to maintain
  627. // switch loc {
  628. // case "th":
  629. // calendar.Formats.DateEra.BC.Full = "EEEEที่ d MMMM y GGGG"
  630. // calendar.Formats.DateEra.AD.Full = "EEEEที่ d MMMM GGGG y"
  631. // calendar.Formats.DateEra.BC.Long = "d MMMM y GG"
  632. // calendar.Formats.DateEra.AD.Long = "d MMMM GG y"
  633. // case "en":
  634. // calendar.Formats.DateEra.BC.Full = "EEEE, MMMM d, y GGGG"
  635. // calendar.Formats.DateEra.BC.Long = "MMMM d, y GG"
  636. // // calendar.Formats.DateEra.BC.Medium = "MMM d, y GG"
  637. // // calendar.Formats.DateEra.BC.Short = "M/d/yy G"
  638. // }
  639. // }
  640. // if ldmlCar.TimeFormats != nil {
  641. // for _, datefmt := range ldmlCar.TimeFormats.TimeFormatLength {
  642. // switch datefmt.Type {
  643. // case "full":
  644. // calendar.Formats.Time.Full = datefmt.TimeFormat[0].Pattern[0].Data()
  645. // case "long":
  646. // calendar.Formats.Time.Long = datefmt.TimeFormat[0].Pattern[0].Data()
  647. // case "medium":
  648. // calendar.Formats.Time.Medium = datefmt.TimeFormat[0].Pattern[0].Data()
  649. // case "short":
  650. // calendar.Formats.Time.Short = datefmt.TimeFormat[0].Pattern[0].Data()
  651. // }
  652. // }
  653. // }
  654. // if ldmlCar.DateTimeFormats != nil {
  655. // for _, datefmt := range ldmlCar.DateTimeFormats.DateTimeFormatLength {
  656. // switch datefmt.Type {
  657. // case "full":
  658. // calendar.Formats.DateTime.Full = datefmt.DateTimeFormat[0].Pattern[0].Data()
  659. // case "long":
  660. // calendar.Formats.DateTime.Long = datefmt.DateTimeFormat[0].Pattern[0].Data()
  661. // case "medium":
  662. // calendar.Formats.DateTime.Medium = datefmt.DateTimeFormat[0].Pattern[0].Data()
  663. // case "short":
  664. // calendar.Formats.DateTime.Short = datefmt.DateTimeFormat[0].Pattern[0].Data()
  665. // }
  666. // }
  667. // }
  668. // if ldmlCar.Months != nil {
  669. // for _, monthctx := range ldmlCar.Months.MonthContext {
  670. // for _, months := range monthctx.MonthWidth {
  671. // i18nMonth := make(i18n.CalendarMonthFormatNameValue)
  672. // for _, m := range months.Month {
  673. // switch m.Type {
  674. // case "1":
  675. // i18nMonth[time.January] = m.Data()
  676. // case "2":
  677. // i18nMonth[time.February] = m.Data()
  678. // case "3":
  679. // i18nMonth[time.March] = m.Data()
  680. // case "4":
  681. // i18nMonth[time.April] = m.Data()
  682. // case "5":
  683. // i18nMonth[time.May] = m.Data()
  684. // case "6":
  685. // i18nMonth[time.June] = m.Data()
  686. // case "7":
  687. // i18nMonth[time.July] = m.Data()
  688. // case "8":
  689. // i18nMonth[time.August] = m.Data()
  690. // case "9":
  691. // i18nMonth[time.September] = m.Data()
  692. // case "10":
  693. // i18nMonth[time.October] = m.Data()
  694. // case "11":
  695. // i18nMonth[time.November] = m.Data()
  696. // case "12":
  697. // i18nMonth[time.December] = m.Data()
  698. // }
  699. // }
  700. // switch months.Type {
  701. // case "abbreviated":
  702. // calendar.FormatNames.Months.Abbreviated = i18nMonth
  703. // case "narrow":
  704. // calendar.FormatNames.Months.Narrow = i18nMonth
  705. // case "short":
  706. // calendar.FormatNames.Months.Short = i18nMonth
  707. // case "wide":
  708. // calendar.FormatNames.Months.Wide = i18nMonth
  709. // }
  710. // }
  711. // }
  712. // }
  713. // if ldmlCar.Days != nil {
  714. // for _, dayctx := range ldmlCar.Days.DayContext {
  715. // for _, days := range dayctx.DayWidth {
  716. // i18nDay := make(i18n.CalendarDayFormatNameValue)
  717. // for _, d := range days.Day {
  718. // switch d.Type {
  719. // case "sun":
  720. // i18nDay[time.Sunday] = d.Data()
  721. // case "mon":
  722. // i18nDay[time.Monday] = d.Data()
  723. // case "tue":
  724. // i18nDay[time.Tuesday] = d.Data()
  725. // case "wed":
  726. // i18nDay[time.Wednesday] = d.Data()
  727. // case "thu":
  728. // i18nDay[time.Thursday] = d.Data()
  729. // case "fri":
  730. // i18nDay[time.Friday] = d.Data()
  731. // case "sat":
  732. // i18nDay[time.Saturday] = d.Data()
  733. // }
  734. // }
  735. // switch days.Type {
  736. // case "abbreviated":
  737. // calendar.FormatNames.Days.Abbreviated = i18nDay
  738. // case "narrow":
  739. // calendar.FormatNames.Days.Narrow = i18nDay
  740. // case "short":
  741. // calendar.FormatNames.Days.Short = i18nDay
  742. // case "wide":
  743. // calendar.FormatNames.Days.Wide = i18nDay
  744. // }
  745. // }
  746. // }
  747. // }
  748. // if ldmlCar.DayPeriods != nil {
  749. // for _, ctx := range ldmlCar.DayPeriods.DayPeriodContext {
  750. // for _, width := range ctx.DayPeriodWidth {
  751. // // var i18nPeriod i18n.CalendarPeriodFormatNameValue
  752. // i18nPeriod := make(i18n.CalendarPeriodFormatNameValue)
  753. // for _, d := range width.DayPeriod {
  754. // if _, ok := i18nPeriod[d.Type]; !ok {
  755. // i18nPeriod[d.Type] = d.Data()
  756. // }
  757. // }
  758. // switch width.Type {
  759. // case "abbreviated":
  760. // calendar.FormatNames.Periods.Abbreviated = i18nPeriod
  761. // case "narrow":
  762. // calendar.FormatNames.Periods.Narrow = i18nPeriod
  763. // case "short":
  764. // calendar.FormatNames.Periods.Short = i18nPeriod
  765. // case "wide":
  766. // calendar.FormatNames.Periods.Wide = i18nPeriod
  767. // }
  768. // }
  769. // }
  770. // // var empty i18n.CalendarPeriodFormatNameValue
  771. // // if calendar.FormatNames.Periods.Abbreviated == empty {
  772. // // calendar.FormatNames.Periods.Abbreviated = calendar.FormatNames.Periods.Wide
  773. // // }
  774. // }
  775. // if ldmlCar.Eras != nil {
  776. // var eras i18n.Eras
  777. // if ldmlCar.Eras.EraNames != nil && len(ldmlCar.Eras.EraNames.Era) > 0 {
  778. // eras.BC.Full = ldmlCar.Eras.EraNames.Era[0].Data()
  779. // }
  780. // if ldmlCar.Eras.EraAbbr != nil && len(ldmlCar.Eras.EraAbbr.Era) > 0 {
  781. // eras.BC.Abbrev = ldmlCar.Eras.EraAbbr.Era[0].Data()
  782. // }
  783. // if ldmlCar.Eras.EraNarrow != nil && len(ldmlCar.Eras.EraNarrow.Era) > 0 {
  784. // eras.BC.Narrow = ldmlCar.Eras.EraNarrow.Era[0].Data()
  785. // }
  786. // if ldmlCar.Eras.EraNames != nil && len(ldmlCar.Eras.EraNames.Era) > 2 {
  787. // eras.AD.Full = ldmlCar.Eras.EraNames.Era[2].Data()
  788. // }
  789. // if ldmlCar.Eras.EraAbbr != nil && len(ldmlCar.Eras.EraAbbr.Era) > 2 {
  790. // eras.AD.Abbrev = ldmlCar.Eras.EraAbbr.Era[2].Data()
  791. // }
  792. // if ldmlCar.Eras.EraNarrow != nil && len(ldmlCar.Eras.EraNarrow.Era) > 2 {
  793. // eras.AD.Narrow = ldmlCar.Eras.EraNarrow.Era[2].Data()
  794. // }
  795. // calendar.FormatNames.Eras = eras
  796. // }
  797. // calendars[loc] = calendar
  798. // }
  799. // }
  800. // for locale := range locs {
  801. // if !strings.Contains(locale, "_") {
  802. // continue
  803. // }
  804. // calendar := calendars[locale]
  805. // bString := strings.SplitN(locale, "_", 2)
  806. // base := bString[0]
  807. // baseCal := calendars[base]
  808. // // copy base calendar objects
  809. // // Dates
  810. // if calendar.Formats.DateEra.AD.Full == "" {
  811. // calendar.Formats.DateEra.BC.Full = baseCal.Formats.DateEra.BC.Full
  812. // calendar.Formats.DateEra.AD.Full = baseCal.Formats.DateEra.AD.Full
  813. // }
  814. // if calendar.Formats.DateEra.AD.Long == "" {
  815. // calendar.Formats.DateEra.BC.Long = baseCal.Formats.DateEra.BC.Long
  816. // calendar.Formats.DateEra.AD.Long = baseCal.Formats.DateEra.AD.Long
  817. // }
  818. // if calendar.Formats.DateEra.AD.Medium == "" {
  819. // calendar.Formats.DateEra.BC.Medium = baseCal.Formats.DateEra.BC.Medium
  820. // calendar.Formats.DateEra.AD.Medium = baseCal.Formats.DateEra.AD.Medium
  821. // }
  822. // if calendar.Formats.DateEra.AD.Short == "" {
  823. // calendar.Formats.DateEra.BC.Short = baseCal.Formats.DateEra.BC.Short
  824. // calendar.Formats.DateEra.AD.Short = baseCal.Formats.DateEra.AD.Short
  825. // }
  826. // // times
  827. // if calendar.Formats.Time.Full == "" {
  828. // calendar.Formats.Time.Full = baseCal.Formats.Time.Full
  829. // }
  830. // if calendar.Formats.Time.Long == "" {
  831. // calendar.Formats.Time.Long = baseCal.Formats.Time.Long
  832. // }
  833. // if calendar.Formats.Time.Medium == "" {
  834. // calendar.Formats.Time.Medium = baseCal.Formats.Time.Medium
  835. // }
  836. // if calendar.Formats.Time.Short == "" {
  837. // calendar.Formats.Time.Short = baseCal.Formats.Time.Short
  838. // }
  839. // // date & times
  840. // if calendar.Formats.DateTime.Full == "" {
  841. // calendar.Formats.DateTime.Full = baseCal.Formats.DateTime.Full
  842. // }
  843. // if calendar.Formats.DateTime.Long == "" {
  844. // calendar.Formats.DateTime.Long = baseCal.Formats.DateTime.Long
  845. // }
  846. // if calendar.Formats.DateTime.Medium == "" {
  847. // calendar.Formats.DateTime.Medium = baseCal.Formats.DateTime.Medium
  848. // }
  849. // if calendar.Formats.DateTime.Short == "" {
  850. // calendar.Formats.DateTime.Short = baseCal.Formats.DateTime.Short
  851. // }
  852. // // months
  853. // if calendar.FormatNames.Months.Abbreviated == nil {
  854. // calendar.FormatNames.Months.Abbreviated = make(i18n.CalendarMonthFormatNameValue)
  855. // }
  856. // if calendar.FormatNames.Months.Narrow == nil {
  857. // calendar.FormatNames.Months.Narrow = make(i18n.CalendarMonthFormatNameValue)
  858. // }
  859. // if calendar.FormatNames.Months.Short == nil {
  860. // calendar.FormatNames.Months.Short = make(i18n.CalendarMonthFormatNameValue)
  861. // }
  862. // if calendar.FormatNames.Months.Wide == nil {
  863. // calendar.FormatNames.Months.Wide = make(i18n.CalendarMonthFormatNameValue)
  864. // }
  865. // for k, v := range baseCal.FormatNames.Months.Abbreviated {
  866. // val, ok := calendar.FormatNames.Months.Abbreviated[k]
  867. // if !ok {
  868. // calendar.FormatNames.Months.Abbreviated[k] = v
  869. // continue
  870. // }
  871. // if val == "" {
  872. // calendar.FormatNames.Months.Abbreviated[k] = v
  873. // }
  874. // }
  875. // for k, v := range baseCal.FormatNames.Months.Narrow {
  876. // val, ok := calendar.FormatNames.Months.Narrow[k]
  877. // if !ok {
  878. // calendar.FormatNames.Months.Narrow[k] = v
  879. // continue
  880. // }
  881. // if val == "" {
  882. // calendar.FormatNames.Months.Narrow[k] = v
  883. // }
  884. // }
  885. // for k, v := range baseCal.FormatNames.Months.Short {
  886. // val, ok := calendar.FormatNames.Months.Short[k]
  887. // if !ok {
  888. // calendar.FormatNames.Months.Short[k] = v
  889. // continue
  890. // }
  891. // if val == "" {
  892. // calendar.FormatNames.Months.Short[k] = v
  893. // }
  894. // }
  895. // for k, v := range baseCal.FormatNames.Months.Wide {
  896. // val, ok := calendar.FormatNames.Months.Wide[k]
  897. // if !ok {
  898. // calendar.FormatNames.Months.Wide[k] = v
  899. // continue
  900. // }
  901. // if val == "" {
  902. // calendar.FormatNames.Months.Wide[k] = v
  903. // }
  904. // }
  905. // // days
  906. // if calendar.FormatNames.Days.Abbreviated == nil {
  907. // calendar.FormatNames.Days.Abbreviated = make(i18n.CalendarDayFormatNameValue)
  908. // }
  909. // if calendar.FormatNames.Days.Narrow == nil {
  910. // calendar.FormatNames.Days.Narrow = make(i18n.CalendarDayFormatNameValue)
  911. // }
  912. // if calendar.FormatNames.Days.Short == nil {
  913. // calendar.FormatNames.Days.Short = make(i18n.CalendarDayFormatNameValue)
  914. // }
  915. // if calendar.FormatNames.Days.Wide == nil {
  916. // calendar.FormatNames.Days.Wide = make(i18n.CalendarDayFormatNameValue)
  917. // }
  918. // for k, v := range baseCal.FormatNames.Days.Abbreviated {
  919. // val, ok := calendar.FormatNames.Days.Abbreviated[k]
  920. // if !ok {
  921. // calendar.FormatNames.Days.Abbreviated[k] = v
  922. // continue
  923. // }
  924. // if val == "" {
  925. // calendar.FormatNames.Days.Abbreviated[k] = v
  926. // }
  927. // }
  928. // for k, v := range baseCal.FormatNames.Days.Narrow {
  929. // val, ok := calendar.FormatNames.Days.Narrow[k]
  930. // if !ok {
  931. // calendar.FormatNames.Days.Narrow[k] = v
  932. // continue
  933. // }
  934. // if val == "" {
  935. // calendar.FormatNames.Days.Narrow[k] = v
  936. // }
  937. // }
  938. // for k, v := range baseCal.FormatNames.Days.Short {
  939. // val, ok := calendar.FormatNames.Days.Short[k]
  940. // if !ok {
  941. // calendar.FormatNames.Days.Short[k] = v
  942. // continue
  943. // }
  944. // if val == "" {
  945. // calendar.FormatNames.Days.Short[k] = v
  946. // }
  947. // }
  948. // for k, v := range baseCal.FormatNames.Days.Wide {
  949. // val, ok := calendar.FormatNames.Days.Wide[k]
  950. // if !ok {
  951. // calendar.FormatNames.Days.Wide[k] = v
  952. // continue
  953. // }
  954. // if val == "" {
  955. // calendar.FormatNames.Days.Wide[k] = v
  956. // }
  957. // }
  958. // // periods
  959. // if calendar.FormatNames.Periods.Abbreviated == nil {
  960. // calendar.FormatNames.Periods.Abbreviated = make(i18n.CalendarPeriodFormatNameValue)
  961. // }
  962. // if calendar.FormatNames.Periods.Narrow == nil {
  963. // calendar.FormatNames.Periods.Narrow = make(i18n.CalendarPeriodFormatNameValue)
  964. // }
  965. // if calendar.FormatNames.Periods.Short == nil {
  966. // calendar.FormatNames.Periods.Short = make(i18n.CalendarPeriodFormatNameValue)
  967. // }
  968. // if calendar.FormatNames.Periods.Wide == nil {
  969. // calendar.FormatNames.Periods.Wide = make(i18n.CalendarPeriodFormatNameValue)
  970. // }
  971. // for k, v := range baseCal.FormatNames.Periods.Abbreviated {
  972. // val, ok := calendar.FormatNames.Periods.Abbreviated[k]
  973. // if !ok {
  974. // calendar.FormatNames.Periods.Abbreviated[k] = v
  975. // continue
  976. // }
  977. // if val == "" {
  978. // calendar.FormatNames.Periods.Abbreviated[k] = v
  979. // }
  980. // }
  981. // for k, v := range baseCal.FormatNames.Periods.Narrow {
  982. // val, ok := calendar.FormatNames.Periods.Narrow[k]
  983. // if !ok {
  984. // calendar.FormatNames.Periods.Narrow[k] = v
  985. // continue
  986. // }
  987. // if val == "" {
  988. // calendar.FormatNames.Periods.Narrow[k] = v
  989. // }
  990. // }
  991. // for k, v := range baseCal.FormatNames.Periods.Short {
  992. // val, ok := calendar.FormatNames.Periods.Short[k]
  993. // if !ok {
  994. // calendar.FormatNames.Periods.Short[k] = v
  995. // continue
  996. // }
  997. // if val == "" {
  998. // calendar.FormatNames.Periods.Short[k] = v
  999. // }
  1000. // }
  1001. // for k, v := range baseCal.FormatNames.Periods.Wide {
  1002. // val, ok := calendar.FormatNames.Periods.Wide[k]
  1003. // if !ok {
  1004. // calendar.FormatNames.Periods.Wide[k] = v
  1005. // continue
  1006. // }
  1007. // if val == "" {
  1008. // calendar.FormatNames.Periods.Wide[k] = v
  1009. // }
  1010. // }
  1011. // calendars[locale] = calendar
  1012. // number := numbers[locale]
  1013. // baseNum := numbers[base]
  1014. // // symbols
  1015. // if number.Symbols.Decimal == "" {
  1016. // number.Symbols.Decimal = baseNum.Symbols.Decimal
  1017. // }
  1018. // if number.Symbols.Group == "" {
  1019. // number.Symbols.Group = baseNum.Symbols.Group
  1020. // }
  1021. // if number.Symbols.Negative == "" {
  1022. // number.Symbols.Negative = baseNum.Symbols.Negative
  1023. // }
  1024. // if number.Symbols.Percent == "" {
  1025. // number.Symbols.Percent = baseNum.Symbols.Percent
  1026. // }
  1027. // if number.Symbols.PerMille == "" {
  1028. // number.Symbols.PerMille = baseNum.Symbols.PerMille
  1029. // }
  1030. // // formats
  1031. // if number.Formats.Decimal == "" {
  1032. // number.Formats.Decimal = baseNum.Formats.Decimal
  1033. // }
  1034. // if number.Formats.Currency == "" {
  1035. // number.Formats.Currency = baseNum.Formats.Currency
  1036. // }
  1037. // if number.Formats.CurrencyAccounting == "" {
  1038. // number.Formats.CurrencyAccounting = baseNum.Formats.CurrencyAccounting
  1039. // }
  1040. // if number.Formats.Percent == "" {
  1041. // number.Formats.Percent = baseNum.Formats.Percent
  1042. // }
  1043. // // currency
  1044. // for k, v := range baseNum.Currencies {
  1045. // val, ok := number.Currencies[k]
  1046. // if !ok {
  1047. // // number.Currencies[k] = v
  1048. // continue
  1049. // }
  1050. // if val.Currency == "" {
  1051. // val.Currency = v.Currency
  1052. // }
  1053. // if val.DisplayName == "" {
  1054. // val.DisplayName = v.DisplayName
  1055. // }
  1056. // if val.Symbol == "" {
  1057. // val.Symbol = v.Symbol
  1058. // }
  1059. // number.Currencies[k] = val
  1060. // }
  1061. // numbers[locale] = number
  1062. // }
  1063. // var wg sync.WaitGroup
  1064. // wg.Add(len(numbers))
  1065. // for locale, number := range numbers {
  1066. // go func(locale string, number i18n.Number) {
  1067. // localeLowercase := strings.ToLower(locale)
  1068. // defer func() { wg.Done() }()
  1069. // path := "../../resources/locales/" + locale
  1070. // if _, err := os.Stat(path); err != nil {
  1071. // if err = os.MkdirAll(path, 0777); err != nil {
  1072. // panic(err)
  1073. // }
  1074. // }
  1075. // path += "/"
  1076. // mainFile, err := os.Create(path + "main.go")
  1077. // if err != nil {
  1078. // panic(err)
  1079. // }
  1080. // defer mainFile.Close()
  1081. // calendar := calendars[locale]
  1082. // mainCodes, err := format.Source([]byte(fmt.Sprintf(`package %s
  1083. // import "github.com/go-playground/universal-translator"
  1084. // var locale = &ut.Locale{
  1085. // Locale: %q,
  1086. // Number: ut.Number{
  1087. // Symbols: symbols,
  1088. // Formats: formats,
  1089. // Currencies: currencies,
  1090. // },
  1091. // Calendar: calendar,
  1092. // PluralRule: pluralRule,
  1093. // }
  1094. // func init() {
  1095. // ut.RegisterLocale(locale)
  1096. // }
  1097. // `, locale, locale)))
  1098. // if err != nil {
  1099. // panic(err)
  1100. // }
  1101. // fmt.Fprintf(mainFile, "%s", mainCodes)
  1102. // numberFile, err := os.Create(path + "number.go")
  1103. // if err != nil {
  1104. // panic(err)
  1105. // }
  1106. // defer numberFile.Close()
  1107. // numberCodes, err := format.Source([]byte(fmt.Sprintf(`package %s
  1108. // import "github.com/go-playground/universal-translator"
  1109. // var (
  1110. // symbols = %#v
  1111. // formats = %#v
  1112. // )
  1113. // `, locale, number.Symbols, number.Formats)))
  1114. // if err != nil {
  1115. // panic(err)
  1116. // }
  1117. // fmt.Fprintf(numberFile, "%s", numberCodes)
  1118. // currencyFile, err := os.Create(path + "currency.go")
  1119. // if err != nil {
  1120. // panic(err)
  1121. // }
  1122. // defer currencyFile.Close()
  1123. // currencyCodes, err := format.Source([]byte(fmt.Sprintf(`package %s
  1124. // import "github.com/go-playground/universal-translator"
  1125. // var currencies = %# v
  1126. // `, locale, number.Currencies)))
  1127. // if err != nil {
  1128. // panic(err)
  1129. // }
  1130. // fmt.Fprintf(currencyFile, "%s", currencyCodes)
  1131. // calendarFile, err := os.Create(path + "calendar.go")
  1132. // if err != nil {
  1133. // panic(err)
  1134. // }
  1135. // defer calendarFile.Close()
  1136. // calendarCodes, err := format.Source([]byte(fmt.Sprintf(`package %s
  1137. // import "github.com/go-playground/universal-translator"
  1138. // var calendar = %#v
  1139. // `, locale, calendar)))
  1140. // if err != nil {
  1141. // panic(err)
  1142. // }
  1143. // fmt.Fprintf(calendarFile, "%s", calendarCodes)
  1144. // var ok bool
  1145. // pluralCode := "1"
  1146. // pInfo, ok := plurals[localeLowercase]
  1147. // if ok && pInfo.plural != "" {
  1148. // pluralCode = pInfo.plural
  1149. // }
  1150. // pluralFile, err := os.Create(path + "plural.go")
  1151. // if err != nil {
  1152. // panic(err)
  1153. // }
  1154. // defer pluralFile.Close()
  1155. // pluralCodes, err := format.Source([]byte(fmt.Sprintf(`package %s
  1156. // var pluralRule = %q
  1157. // `, locale, pluralCode)))
  1158. // if err != nil {
  1159. // panic(err)
  1160. // }
  1161. // fmt.Fprintf(pluralFile, "%s", pluralCodes)
  1162. // }(locale, number)
  1163. // }
  1164. // wg.Wait()
  1165. // localesFile, err := os.Create("../../resources/locales/all.go")
  1166. // if err != nil {
  1167. // panic(err)
  1168. // }
  1169. // defer localesFile.Close()
  1170. // tmpl, err := template.New("").Parse(`package locales
  1171. // // Imports for all locales
  1172. // import (
  1173. // {{range $locale, $_ := .}}// Locale "{{$locale}}" import that automatically registers itslef with the universal-translator package
  1174. // _ "github.com/go-playground/universal-translator/resources/locales/{{$locale}}"
  1175. // {{end}})
  1176. // `)
  1177. // if err != nil {
  1178. // panic(err)
  1179. // }
  1180. // var buf bytes.Buffer
  1181. // if err := tmpl.Execute(&buf, locs); err != nil {
  1182. // panic(err)
  1183. // }
  1184. // allCodes, err := format.Source(buf.Bytes())
  1185. // if err != nil {
  1186. // panic(err)
  1187. // }
  1188. // _, err = localesFile.Write(allCodes)
  1189. // if err != nil {
  1190. // panic(err)
  1191. // }
  1192. // }