Esempio n. 1
0
func Compile(filename string, translations []*po.Po, opts *CompileOptions) error {
	var buf bytes.Buffer
	dir := filepath.Dir(filename)
	p, err := build.ImportDir(dir, 0)
	if err == nil {
		fmt.Fprintf(&buf, "package %s\n", p.Name)
	}
	buf.WriteString("import \"gnd.la/i18n/table\"\n")
	buf.WriteString(genutil.AutogenString())
	buf.WriteString("func init() {\n")
	var defaultContext string
	if opts != nil {
		defaultContext = opts.DefaultContext
	}
	for _, v := range translations {
		table := poToTable(v, defaultContext)
		form, err := funcFromFormula(v.Attrs["Plural-Forms"])
		if err != nil {
			return err
		}
		data, err := table.Encode()
		if err != nil {
			return err
		}
		fmt.Fprintf(&buf, "table.Register(%q, func (n int) int {\n%s\n}, %q)\n", v.Attrs["Language"], form, data)
	}
	buf.WriteString("\n}\n")
	return genutil.WriteAutogen(filename, buf.Bytes())
}
Esempio n. 2
0
func bakeCommand(_ *command.Args, opts *bakeOptions) error {
	extensions := []string{".html", ".css", ".js"}
	if opts.Dir == "" {
		return errors.New("dir can't be empty")
	}
	if opts.Name == "" {
		base := filepath.Base(opts.Dir)
		if opts.VFS {
			opts.Name = base + "FS"
		} else {
			opts.Name = base + "Data"
		}
	}
	if opts.Out == "" {
		opts.Out = filepath.Base(opts.Dir) + "_baked.go"
	}
	// go ignores files starting with _
	opts.Out = strings.TrimLeft(opts.Out, "_")
	extensions = append(extensions, strings.Split(opts.Extensions, ",")...)
	var buf bytes.Buffer
	odir := filepath.Dir(opts.Out)
	p, err := build.ImportDir(odir, 0)
	if err == nil {
		buf.WriteString(fmt.Sprintf("package %s\n", p.Name))
	}
	buf.WriteString(genutil.AutogenString())
	if err := writeBakedFSCode(&buf, opts, extensions); err != nil {
		return err
	}
	if err := genutil.WriteAutogen(opts.Out, buf.Bytes()); err != nil {
		return err
	}
	log.Debugf("Assets written to %s (%d bytes)", opts.Out, buf.Len())
	return nil
}