generate_resources.go 33 KB

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