瀏覽代碼

Update logic to store locale data in maps

joeybloggs 9 年之前
父節點
當前提交
3aa1014f5d
共有 4 個文件被更改,包括 102 次插入69 次删除
  1. 59 39
      cmd/resources/generate_resources.go
  2. 9 10
      locale.go
  3. 22 8
      plurals.go
  4. 12 12
      translator.go

+ 59 - 39
cmd/resources/generate_resources.go

@@ -8,11 +8,11 @@ import (
 	"strings"
 	"sync"
 	"text/template"
+	"time"
 
 	"golang.org/x/text/unicode/cldr"
 
 	i18n "github.com/go-playground/universal-translator"
-	"github.com/kr/pretty"
 )
 
 // numbers:
@@ -50,6 +50,9 @@ func main() {
 			continue
 		}
 		var number i18n.Number
+
+		number.Currencies = make(i18n.CurrencyFormatValue)
+
 		if len(ldml.Numbers.Symbols) > 0 {
 			symbol := ldml.Numbers.Symbols[0]
 			if len(symbol.Decimal) > 0 {
@@ -78,16 +81,22 @@ func main() {
 			number.Formats.Percent = ldml.Numbers.PercentFormats[0].PercentFormatLength[0].PercentFormat[0].Pattern[0].Data()
 		}
 		if ldml.Numbers.Currencies != nil {
+
 			for _, currency := range ldml.Numbers.Currencies.Currency {
+
 				var c i18n.Currency
+
 				c.Currency = currency.Type
+
 				if len(currency.DisplayName) > 0 {
 					c.DisplayName = currency.DisplayName[0].Data()
 				}
+
 				if len(currency.Symbol) > 0 {
 					c.Symbol = currency.Symbol[0].Data()
 				}
-				number.Currencies = append(number.Currencies, c)
+
+				number.Currencies[c.Currency] = c
 			}
 		}
 		numbers[loc] = number
@@ -145,35 +154,39 @@ func main() {
 				}
 			}
 			if ldmlCar.Months != nil {
+
 				for _, monthctx := range ldmlCar.Months.MonthContext {
+
 					for _, months := range monthctx.MonthWidth {
-						var i18nMonth i18n.CalendarMonthFormatNameValue
+
+						i18nMonth := make(i18n.CalendarMonthFormatNameValue)
+
 						for _, m := range months.Month {
 							switch m.Type {
 							case "1":
-								i18nMonth.Jan = m.Data()
+								i18nMonth[time.January] = m.Data()
 							case "2":
-								i18nMonth.Feb = m.Data()
+								i18nMonth[time.February] = m.Data()
 							case "3":
-								i18nMonth.Mar = m.Data()
+								i18nMonth[time.March] = m.Data()
 							case "4":
-								i18nMonth.Apr = m.Data()
+								i18nMonth[time.April] = m.Data()
 							case "5":
-								i18nMonth.May = m.Data()
+								i18nMonth[time.May] = m.Data()
 							case "6":
-								i18nMonth.Jun = m.Data()
+								i18nMonth[time.June] = m.Data()
 							case "7":
-								i18nMonth.Jul = m.Data()
+								i18nMonth[time.July] = m.Data()
 							case "8":
-								i18nMonth.Aug = m.Data()
+								i18nMonth[time.August] = m.Data()
 							case "9":
-								i18nMonth.Sep = m.Data()
+								i18nMonth[time.September] = m.Data()
 							case "10":
-								i18nMonth.Oct = m.Data()
+								i18nMonth[time.October] = m.Data()
 							case "11":
-								i18nMonth.Nov = m.Data()
+								i18nMonth[time.November] = m.Data()
 							case "12":
-								i18nMonth.Dec = m.Data()
+								i18nMonth[time.December] = m.Data()
 							}
 						}
 						switch months.Type {
@@ -191,26 +204,31 @@ func main() {
 			}
 			if ldmlCar.Days != nil {
 				for _, dayctx := range ldmlCar.Days.DayContext {
+
 					for _, days := range dayctx.DayWidth {
-						var i18nDay i18n.CalendarDayFormatNameValue
+
+						i18nDay := make(i18n.CalendarDayFormatNameValue)
+
 						for _, d := range days.Day {
+
 							switch d.Type {
 							case "sun":
-								i18nDay.Sun = d.Data()
+								i18nDay[time.Sunday] = d.Data()
 							case "mon":
-								i18nDay.Mon = d.Data()
+								i18nDay[time.Monday] = d.Data()
 							case "tue":
-								i18nDay.Tue = d.Data()
+								i18nDay[time.Tuesday] = d.Data()
 							case "wed":
-								i18nDay.Wed = d.Data()
+								i18nDay[time.Wednesday] = d.Data()
 							case "thu":
-								i18nDay.Thu = d.Data()
+								i18nDay[time.Thursday] = d.Data()
 							case "fri":
-								i18nDay.Fri = d.Data()
+								i18nDay[time.Friday] = d.Data()
 							case "sat":
-								i18nDay.Sat = d.Data()
+								i18nDay[time.Saturday] = d.Data()
 							}
 						}
+
 						switch days.Type {
 						case "abbreviated":
 							calendar.FormatNames.Days.Abbreviated = i18nDay
@@ -224,22 +242,23 @@ func main() {
 					}
 				}
 			}
+
 			if ldmlCar.DayPeriods != nil {
+
 				for _, ctx := range ldmlCar.DayPeriods.DayPeriodContext {
+
 					for _, width := range ctx.DayPeriodWidth {
-						var i18nPeriod i18n.CalendarPeriodFormatNameValue
+
+						// var i18nPeriod i18n.CalendarPeriodFormatNameValue
+						i18nPeriod := make(i18n.CalendarPeriodFormatNameValue)
+
 						for _, d := range width.DayPeriod {
-							switch d.Type {
-							case "am":
-								if i18nPeriod.AM == "" {
-									i18nPeriod.AM = d.Data()
-								}
-							case "pm":
-								if i18nPeriod.PM == "" {
-									i18nPeriod.PM = d.Data()
-								}
+
+							if _, ok := i18nPeriod[d.Type]; !ok {
+								i18nPeriod[d.Type] = d.Data()
 							}
 						}
+
 						switch width.Type {
 						case "abbreviated":
 							calendar.FormatNames.Periods.Abbreviated = i18nPeriod
@@ -295,15 +314,15 @@ func main() {
 			}
 
 			func new%sSymbols() Symbols {
-				return %#v
+				return %# v
 			}
 
 			func new%sFormats() NumberFormats {
-				return %#v
+				return %# v
 			}
 
-			func new%sCurrencies() []Currency {
-				return %#v
+			func new%sCurrencies() CurrencyFormatValue {
+				return %# v
 			}
 
 			func new%sCalendar() Calendar {
@@ -311,8 +330,9 @@ func main() {
 			}
 
 		`, localeNoUnderscore, locale, localeNoUnderscore, localeNoUnderscore, localeNoUnderscore, localeNoUnderscore, localeNoUnderscore,
-				localeNoUnderscore, pretty.Formatter(number.Symbols), localeNoUnderscore, pretty.Formatter(number.Formats), localeNoUnderscore,
-				pretty.Formatter(number.Currencies), localeNoUnderscore, pretty.Formatter(calendar))))
+				localeNoUnderscore, number.Symbols, localeNoUnderscore, number.Formats, localeNoUnderscore,
+				number.Currencies, localeNoUnderscore, calendar)))
+
 			if err != nil {
 				panic(err)
 			}

+ 9 - 10
locale.go

@@ -1,5 +1,7 @@
 package ut
 
+import "time"
+
 // Locale contains all of the locale info needed for translation,
 // number and date formatting
 type Locale struct {
@@ -13,9 +15,12 @@ type Locale struct {
 type Number struct {
 	Symbols    Symbols
 	Formats    NumberFormats
-	Currencies []Currency
+	Currencies CurrencyFormatValue
 }
 
+// CurrencyFormatValue contains the currency information
+type CurrencyFormatValue map[string]Currency
+
 // Symbols contains the number symbols related to formatting
 type Symbols struct {
 	Decimal  string
@@ -71,9 +76,7 @@ type CalendarMonthFormatNames struct {
 }
 
 // CalendarMonthFormatNameValue contains the DateTime month name information
-type CalendarMonthFormatNameValue struct {
-	Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec string
-}
+type CalendarMonthFormatNameValue map[time.Month]string
 
 // CalendarDayFormatNames contains the DateTime month name information
 type CalendarDayFormatNames struct {
@@ -84,9 +87,7 @@ type CalendarDayFormatNames struct {
 }
 
 // CalendarDayFormatNameValue contains the DateTime day name information
-type CalendarDayFormatNameValue struct {
-	Sun, Mon, Tue, Wed, Thu, Fri, Sat string
-}
+type CalendarDayFormatNameValue map[time.Weekday]string
 
 // CalendarPeriodFormatNames contains the DateTime period information
 type CalendarPeriodFormatNames struct {
@@ -97,6 +98,4 @@ type CalendarPeriodFormatNames struct {
 }
 
 // CalendarPeriodFormatNameValue contains the DateTime period name information
-type CalendarPeriodFormatNameValue struct {
-	AM, PM string
-}
+type CalendarPeriodFormatNameValue map[string]string

+ 22 - 8
plurals.go

@@ -3,20 +3,29 @@ package ut
 import "math"
 
 // PluralRule denotes the type of plural rules
-type PluralRule string
+type PluralRule int
 
 // PluralRule types
 
 // TODO: change this to integer for efficiency and use stringify lib to generate string values.
 const (
-	PluralRuleZero  PluralRule = "zero"  // zero
-	PluralRuleOne              = "one"   // singular
-	PluralRuleTwo              = "two"   // dual
-	PluralRuleFew              = "few"   // paucal
-	PluralRuleMany             = "many"  // also used for fractions if they have a separate class
-	PluralRuleOther            = "other" // required—general plural form—also used if the language only has a single form
+	PluralRuleZero  PluralRule = iota // zero
+	PluralRuleOne                     // one - singular
+	PluralRuleTwo                     // two - dual
+	PluralRuleFew                     // few - paucal
+	PluralRuleMany                    // many - also used for fractions if they have a separate class
+	PluralRuleOther                   // other - required—general plural form—also used if the language only has a single form
 )
 
+// const (
+// 	PluralRuleZero  PluralRule = "zero"  // zero
+// 	PluralRuleOne              = "one"   // singular
+// 	PluralRuleTwo              = "two"   // dual
+// 	PluralRuleFew              = "few"   // paucal
+// 	PluralRuleMany             = "many"  // also used for fractions if they have a separate class
+// 	PluralRuleOther            = "other" // required—general plural form—also used if the language only has a single form
+// )
+
 // NumberValue should be one of these types:
 // int, float
 type NumberValue interface{}
@@ -55,6 +64,12 @@ var pluralRules = map[string]PluralRuler{
 	"6B": PluralRulerFunc(pluralRule6B),
 }
 
+// func GetPluralInt(code string) int {
+// 	switch code {
+// 	case "":
+// 	}
+// }
+
 // func getPluralFunc(code string) PluralRulerFunc {
 // 	fn, ok := pluralRules[code]
 // 	if !ok {
@@ -139,7 +154,6 @@ func (p PluralRulerFunc) FindRule(n NumberValue) PluralRule {
 
 // isInt checks if a float64 is an integer value
 func isInt(n NumberValue) bool {
-	// return n == float64(int64(n))
 	switch n.(type) {
 	case int, int16, int32, int64, uint, uint16, uint32, uint64:
 		return true

+ 12 - 12
translator.go

@@ -22,18 +22,18 @@ type Translator struct {
 }
 
 func newTranslator(locale string) (*Translator, error) {
-
-	loc, err := GetLocale(locale)
-	if err != nil {
-		return nil, err
-	}
-
-	return &Translator{
-		Locale:       loc,
-		ruler:        pluralRules[loc.PluralRule],
-		translations: make(translations),
-		groups:       make(groups),
-	}, nil
+	return nil, nil
+	// loc, err := GetLocale(locale)
+	// if err != nil {
+	// 	return nil, err
+	// }
+
+	// return &Translator{
+	// 	Locale:       loc,
+	// 	ruler:        pluralRules[loc.PluralRule],
+	// 	translations: make(translations),
+	// 	groups:       make(groups),
+	// }, nil
 }
 
 // Add registers a new translation to the Translator using the