Exemplo n.º 1
0
// New create new generator
func New(outputFile string, imported ...string) *TGen {
	os.Remove(outputFile)
	f, e := os.Create(outputFile)
	err.Panic(e)
	outputFile = filepath.Base(outputFile)
	outputFile, _ = extfilepath.Ext(outputFile)
	f.Write([]byte("// It's file auto generate encodebinaryFast\n\n"))
	f.Write([]byte("package " + outputFile + "\n\n"))

	resultImport := ". \"github.com/Cergoo/gol/encode/binary/primitive\"\n"
	for i := range imported {
		resultImport += "\"" + imported[i] + "\"\n"
	}
	f.Write([]byte("import (\n" + resultImport + ")\n"))

	return &TGen{
		stackName:  make(stack.TStack, 0, 10),
		tmpNameGen: tmpName.New(),
		f:          f,
	}
}
Exemplo n.º 2
0
Arquivo: i18n.go Projeto: 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
			}
		}
	}
}