generate_resources.go 41 KB

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