Ejemplo n.º 1
0
// CreateNamespace creates translation namespaces.
func CreateNamespace(namespaces ...string) {
	for _, namespace := range namespaces {
		if defaultbundles[namespace] == nil {
			defaultbundles[namespace] = bundle.New()
		}
	}
}
Ejemplo n.º 2
0
func (mc *mergeCommand) execute() error {
	if len(mc.translationFiles) < 1 {
		return fmt.Errorf("need at least one translation file to parse")
	}

	if lang := language.Parse(mc.sourceLanguage); lang == nil {
		return fmt.Errorf("invalid source locale: %s", mc.sourceLanguage)
	}

	marshal, err := newMarshalFunc(mc.format)
	if err != nil {
		return err
	}

	bundle := bundle.New()
	for _, tf := range mc.translationFiles {
		if err := bundle.LoadTranslationFile(tf); err != nil {
			return fmt.Errorf("failed to load translation file %s because %s\n", tf, err)
		}
	}

	translations := bundle.Translations()
	sourceLanguageTag := language.NormalizeTag(mc.sourceLanguage)
	sourceTranslations := translations[sourceLanguageTag]
	if sourceTranslations == nil {
		return fmt.Errorf("no translations found for source locale %s", sourceLanguageTag)
	}
	for translationID, src := range sourceTranslations {
		for _, localeTranslations := range translations {
			if dst := localeTranslations[translationID]; dst == nil || reflect.TypeOf(src) != reflect.TypeOf(dst) {
				localeTranslations[translationID] = src.UntranslatedCopy()
			}
		}
	}

	for localeID, localeTranslations := range translations {
		lang := language.MustParse(localeID)[0]
		all := filter(localeTranslations, func(t translation.Translation) translation.Translation {
			return t.Normalize(lang)
		})
		if err := mc.writeFile("all", all, localeID, marshal); err != nil {
			return err
		}

		untranslated := filter(localeTranslations, func(t translation.Translation) translation.Translation {
			if t.Incomplete(lang) {
				return t.Normalize(lang).Backfill(sourceTranslations[t.ID()])
			}
			return nil
		})
		if err := mc.writeFile("untranslated", untranslated, localeID, marshal); err != nil {
			return err
		}
	}
	return nil
}
Ejemplo n.º 3
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
}
Ejemplo n.º 4
0
func loadDictionary(locale string) (*bundle.TranslateFunc, error) {
	locale = strings.ToLower(locale)
	translations, err := fetchTranslations(locale)
	if err != nil {
		return nil, err
	}
	bdl := bundle.New()
	if err := bdl.ParseTranslationFileBytes(locale+".all.json", translations); err != nil {
		return nil, err
	}
	t, err := bdl.Tfunc(locale, locale) // stupid API, requires a second parameter
	if err != nil {
		return nil, err
	}
	return &t, nil
}
Ejemplo n.º 5
0
func loadI18n(sources []source.Input) error {
	jww.DEBUG.Printf("Load I18n from %q", sources)

	i18nBundle := bundle.New()

	for _, currentSource := range sources {
		for _, r := range currentSource.Files() {
			err := i18nBundle.ParseTranslationFileBytes(r.LogicalName(), r.Bytes())
			if err != nil {
				return fmt.Errorf("Failed to load translations in file %q: %s", r.LogicalName(), err)
			}
		}
	}

	tpl.SetI18nTfuncs(i18nBundle)

	return nil

}
Ejemplo n.º 6
0
func doTestI18nTranslate(t *testing.T, data map[string][]byte, lang, id string, args interface{}) string {
	i18nBundle := bundle.New()

	for file, content := range data {
		err := i18nBundle.ParseTranslationFileBytes(file, content)
		if err != nil {
			t.Errorf("Error parsing translation file: %s", err)
		}
	}

	SetI18nTfuncs(i18nBundle)
	SetTranslateLang(helpers.NewLanguage(lang))

	translated, err := I18nTranslate(id, args)
	if err != nil {
		t.Errorf("Error translating '%s': %s", id, err)
	}
	return translated
}
Ejemplo n.º 7
0
// If translationID is a plural form, then the first variadic argument must be an integer type
// (int, int8, int16, int32, int64) or a float formatted as a string (e.g. "123.45").
// The second variadic argument may be a map[string]interface{} that contains template data.
type TranslateFunc func(translationID string, args ...interface{}) string

// IdentityTfunc returns a TranslateFunc that always returns the translationID passed to it.
//
// It is a useful placeholder when parsing a text/template or html/template
// before the actual Tfunc is available.
func IdentityTfunc() TranslateFunc {
	return func(translationID string, args ...interface{}) string {
		return translationID
	}
}

var defaultBundle = bundle.New()

// MustLoadTranslationFile is similar to LoadTranslationFile
// except it panics if an error happens.
func MustLoadTranslationFile(filename string) {
	defaultBundle.MustLoadTranslationFile(filename)
}

// LoadTranslationFile loads the translations from filename into memory.
//
// The language that the translations are associated with is parsed from the filename (e.g. en-US.json).
//
// Generally you should load translation files once during your program's initialization.
func LoadTranslationFile(filename string) error {
	return defaultBundle.LoadTranslationFile(filename)
}