示例#1
0
文件: tplengin.go 项目: catgatp/gol
// Loade template
func (t *Ttpl) Load(patch string) {
	var (
		fileSource []byte
	)
	base := "/" + filepath.Base(patch) + "/"

	toparse := new(parser.ToParse)
	toparse.Delimiter[0] = []byte("{{")
	toparse.Delimiter[1] = []byte("}}")
	toparse.ParseTag = parseTag
	toparse.ParseText = parseText

	fileList, e := ioutil.ReadDir(patch)
	err.Panic(e)
	for _, item := range fileList {
		fileSource, e = ioutil.ReadFile(patch + "/" + item.Name())
		t.tpl[base+item.Name()] = parser.Parse(fileSource, toparse)
	}
}
示例#2
0
文件: i18n.go 项目: catgatp/gol
// Load load language resources
func (t *Ti18n) Load(patch string) {
	type (
		tmpLang struct {
			PluralRule string
			Plural     map[string][]string
			Phrase     map[string]string
			Lists      map[string][]string
		}
	)

	// создаётся временная структура и в неё парсится json
	tmpLangs := make(map[string]*tmpLang)
	fileList, e := ioutil.ReadDir(patch)
	err.Panic(e)

	var (
		name   string
		valPre map[string]string
		keyPre string
	)

	for _, item := range fileList {
		vtmpLang := new(tmpLang)
		vtmpLang.Plural = make(map[string][]string)
		vtmpLang.Lists = make(map[string][]string)
		jsonConfig.Load(patch+string(filepath.Separator)+item.Name(), &vtmpLang)
		name, _ = gfilepath.Ext(item.Name())
		tmpLangs[name] = vtmpLang
	}

	// chek equivalent all lang resurce
	for key, val := range tmpLangs {
		if valPre != nil && !refl.MapKeysEq(valPre, val.Phrase) {
			err.Panic(err.New("Lang phrase not equivalent: "+keyPre+", "+key, 0))
		}
		valPre = val.Phrase
		keyPre = key
	}

	toparse := new(parser.ToParse)
	toparse.Delimiter[0] = []byte("{{")
	toparse.Delimiter[1] = []byte("}}")
	toparse.ParseTag = parseTag
	toparse.ParseText = parseText

	for key, item := range tmpLangs {
		lang := &Tlang{
			items:      make(map[string]*titem),
			Plural:     item.Plural,
			Lists:      item.Lists,
			PluralRule: plural.PluralRules[item.PluralRule],
			F:          make(map[string]func([]interface{}) []byte),
		}
		if lang.PluralRule == nil && len(lang.Plural) > 0 {
			err.Panic(err.New("Not found plural rule: '"+item.PluralRule+"'", 0))
		}

		for keyPhrase, itemPhrase := range item.Phrase {
			lang.items[keyPhrase] = &titem{items: parser.Parse([]byte(itemPhrase), toparse), contextCount: -1}
		}

		existLang := t.lang[key]
		if existLang == nil {
			t.lang[key] = lang
		} else {
			// add phrase
			for key, val := range lang.items {
				existLang.items[key] = val
			}
			// add plural
			for key, val := range lang.Plural {
				existLang.Plural[key] = val
			}
			// add lists
			for key, val := range lang.Lists {
				existLang.Lists[key] = val
			}
		}
	}
}