Ejemplo n.º 1
0
func (b *Bundle) Tfunc(localeID string, localeIDs ...string) (tf TranslateFunc, err error) {
	var l *locale.Locale
	l, err = locale.New(localeID)
	if err != nil {
		for _, localeID := range localeIDs {
			l, err = locale.New(localeID)
			if err == nil {
				break
			}
		}
	}
	return func(translationID string, args ...interface{}) string {
		return b.translate(l, translationID, args...)
	}, err
}
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 _, err := locale.New(mc.sourceLocaleID); err != nil {
		return fmt.Errorf("invalid source locale %s: %s", mc.sourceLocaleID, err)
	}

	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()
	sourceTranslations := translations[mc.sourceLocaleID]
	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 {
		locale := locale.MustNew(localeID)
		all := filter(localeTranslations, func(t translation.Translation) translation.Translation {
			return t.Normalize(locale.Language)
		})
		if err := mc.writeFile("all", all, localeID, marshal); err != nil {
			return err
		}

		untranslated := filter(localeTranslations, func(t translation.Translation) translation.Translation {
			if t.Incomplete(locale.Language) {
				return t.Normalize(locale.Language).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 (b *Bundle) LoadTranslationFile(filename string) error {
	locale, err := locale.New(filename)
	if err != nil {
		return err
	}

	translations, err := parseTranslationFile(filename)
	if err != nil {
		return err
	}

	b.AddTranslation(locale, translations...)
	return nil
}