Example #1
0
func TestTfunc(t *testing.T) {
	b := New()
	translationID := "translation_id"
	englishTranslation := "en-US(translation_id)"
	b.AddTranslation(locale.MustNew("en-US"), testNewTranslation(t, map[string]interface{}{
		"id":          translationID,
		"translation": englishTranslation,
	}))
	frenchTranslation := "fr-FR(translation_id)"
	b.AddTranslation(locale.MustNew("fr-FR"), testNewTranslation(t, map[string]interface{}{
		"id":          translationID,
		"translation": frenchTranslation,
	}))

	tests := []struct {
		localeIDs []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,
		},
	}

	for _, test := range tests {
		tf, err := b.Tfunc(test.localeIDs[0], test.localeIDs[1:]...)
		if err != nil && test.valid {
			t.Errorf("Tfunc for %v returned error %s", test.localeIDs, err)
		}
		if err == nil && !test.valid {
			t.Errorf("Tfunc for %v returned nil error", test.localeIDs)
		}
		if result := tf(translationID); result != test.result {
			t.Errorf("translation was %s; expected %s", result, test.result)
		}
	}
}
Example #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
}
Example #3
0
// LoadJSON takes a json document of translations and manually
// loads them into the system
func LoadJSON(userLocale string, translationDocument string) error {
	tracelog.Startedf("localize", "LoadJSON", "UserLocale[%s] Length[%d]", userLocale, len(translationDocument))

	tranDocuments := []map[string]interface{}{}
	err := json.Unmarshal([]byte(translationDocument), &tranDocuments)
	if err != nil {
		tracelog.CompletedErrorf(err, "localize", "LoadJSON", "**************>")
		return err
	}

	for _, tranDocument := range tranDocuments {
		tran, err := translation.NewTranslation(tranDocument)
		if err != nil {
			tracelog.CompletedError(err, "localize", "LoadJSON")
			return err
		}

		i18n.AddTranslation(locale.MustNew(userLocale), tran)
	}

	tracelog.Completed("localize", "LoadJSON")
	return nil
}