Exemplo n.º 1
0
// LanguageTags returns the tags of all languages that have been added in a specific namespace.
func LanguageTags(namespace string) ([]string, error) {
	bundle := defaultbundles[namespace]
	if bundle == nil {
		return nil, fmt.Errorf("there is no namespace %s", namespace)
	}
	return bundle.LanguageTags(), nil
}
Exemplo n.º 2
0
func (cc *constantsCommand) execute() error {
	if len(cc.translationFiles) != 1 {
		return fmt.Errorf("need one translation file")
	}

	bundle := bundle.New()

	if err := bundle.LoadTranslationFile(cc.translationFiles[0]); err != nil {
		return fmt.Errorf("failed to load translation file %s because %s\n", cc.translationFiles[0], err)
	}

	translations := bundle.Translations()
	lang := translations[bundle.LanguageTags()[0]]

	// create an array of id to organize
	keys := make([]string, len(lang))
	i := 0

	for id := range lang {
		keys[i] = id
		i++
	}
	sort.Strings(keys)

	tmpl := &templateHeader{
		PackageName: cc.packageName,
		Constants:   make([]templateConstants, len(keys)),
	}

	for i, id := range keys {
		tmpl.Constants[i].ID = id
		tmpl.Constants[i].Name = toCamelCase(id)
		tmpl.Constants[i].Comments = toComments(lang[id])
	}

	filename := filepath.Join(cc.outdir, cc.packageName+".go")
	f, err := os.Create(filename)
	if err != nil {
		return fmt.Errorf("failed to create file %s because %s", filename, err)
	}

	defer f.Close()

	if err = constTemplate.Execute(f, tmpl); err != nil {
		return fmt.Errorf("failed to write file %s because %s", filename, err)
	}

	return nil
}