Ejemplo n.º 1
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.º 2
0
func languageWithTag(tag string) *language.Language {
	return language.MustParse(tag)[0]
}
Ejemplo n.º 3
0
func TestTfunc(t *testing.T) {
	b := New()
	translationID := "translation_id"
	englishTranslation := "en-US(translation_id)"
	b.AddTranslation(language.MustParse("en-US")[0], testNewTranslation(t, map[string]interface{}{
		"id":          translationID,
		"translation": englishTranslation,
	}))
	frenchTranslation := "fr-FR(translation_id)"
	b.AddTranslation(language.MustParse("fr-FR")[0], testNewTranslation(t, map[string]interface{}{
		"id":          translationID,
		"translation": frenchTranslation,
	}))
	spanishTranslation := "es(translation_id)"
	b.AddTranslation(language.MustParse("es")[0], testNewTranslation(t, map[string]interface{}{
		"id":          translationID,
		"translation": spanishTranslation,
	}))

	tests := []struct {
		languageIDs []string
		valid       bool
		result      string
	}{
		{
			[]string{"invalid"},
			false,
			translationID,
		},
		{
			[]string{"invalid", "invalid2"},
			false,
			translationID,
		},
		{
			[]string{"invalid", "en-US"},
			true,
			englishTranslation,
		},
		{
			[]string{"en-US", "invalid"},
			true,
			englishTranslation,
		},
		{
			[]string{"en-US", "fr-FR"},
			true,
			englishTranslation,
		},
		{
			[]string{"invalid", "es"},
			true,
			spanishTranslation,
		},
		{
			[]string{"zh-CN,fr-XX,es"},
			true,
			spanishTranslation,
		},
	}

	for i, test := range tests {
		tf, err := b.Tfunc(test.languageIDs[0], test.languageIDs[1:]...)
		if err != nil && test.valid {
			t.Errorf("Tfunc(%v) = error{%q}; expected no error", test.languageIDs, err)
		}
		if err == nil && !test.valid {
			t.Errorf("Tfunc(%v) = nil error; expected error", test.languageIDs)
		}
		if result := tf(translationID); result != test.result {
			t.Errorf("translation %d was %s; expected %s", i, result, test.result)
		}
	}
}