Ejemplo n.º 1
0
func createBenchmarkTranslateFunc(b *testing.B, translationTemplate interface{}, count interface{}, expected string) func(data interface{}) {
	bundle := New()
	lang := "en-US"
	translationID := "translation_id"
	translation, err := translation.NewTranslation(map[string]interface{}{
		"id":          translationID,
		"translation": translationTemplate,
	})
	if err != nil {
		b.Fatal(err)
	}
	bundle.AddTranslation(languageWithTag(lang), translation)
	tf, err := bundle.Tfunc(lang)
	if err != nil {
		b.Fatal(err)
	}
	return func(data interface{}) {
		var result string
		if count == nil {
			result = tf(translationID, data)
		} else {
			result = tf(translationID, count, data)
		}
		if result != expected {
			b.Fatalf("expected %q, got %q", expected, result)
		}
	}
}
Ejemplo n.º 2
0
func parseTranslationFile(filename string) ([]translation.Translation, error) {
	var unmarshalFunc func([]byte, interface{}) error
	switch format := filepath.Ext(filename); format {
	case ".json":
		unmarshalFunc = json.Unmarshal
	/*
		case ".yaml":
			unmarshalFunc = goyaml.Unmarshal
	*/
	default:
		return nil, fmt.Errorf("unsupported file extension %s", format)
	}
	fileBytes, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	var translationsData []map[string]interface{}
	if len(fileBytes) > 0 {
		if err := unmarshalFunc(fileBytes, &translationsData); err != nil {
			return nil, err
		}
	}

	translations := make([]translation.Translation, 0, len(translationsData))
	for i, translationData := range translationsData {
		t, err := translation.NewTranslation(translationData)
		if err != nil {
			return nil, fmt.Errorf("unable to parse translation #%d in %s because %s\n%v", i, filename, err, translationData)
		}
		translations = append(translations, t)
	}
	return translations, nil
}
Ejemplo n.º 3
0
func testNewTranslation(t *testing.T, data map[string]interface{}) translation.Translation {
	translation, err := translation.NewTranslation(data)
	if err != nil {
		t.Fatal(err)
	}
	return translation
}
Ejemplo n.º 4
0
func (s *GoI18nSifter) Load() (StringMap, error) {
	fileBytes, err := ioutil.ReadFile(s.json)
	if err != nil {
		return nil, err
	}
	var translationsData []map[string]interface{}
	if len(fileBytes) > 0 {
		if err := json.Unmarshal(fileBytes, &translationsData); err != nil {
			return nil, err
		}
	}
	translations := make([]translation.Translation, 0, len(translationsData))
	for i, translationData := range translationsData {
		t, err := translation.NewTranslation(translationData)
		if err != nil {
			return nil, fmt.Errorf("unable to parse translation #%d in %s because %s\n%v", i, s.json, err, translationData)
		}
		translations = append(translations, t)
	}
	result := make(StringMap)
	for _, t := range translations {
		result[t.ID()] = t
	}
	return result, nil
}
Ejemplo n.º 5
0
func createBenchmarkTranslateFunc(b *testing.B) func(data interface{}) {
	bundle := New()
	lang := "en-US"
	translationID := "translation_id"
	translation, err := translation.NewTranslation(map[string]interface{}{
		"id": translationID,
		"translation": map[string]interface{}{
			"one":   "{{.Person}} is {{.Count}} year old.",
			"other": "{{.Person}} is {{.Count}} years old.",
		},
	})
	if err != nil {
		b.Fatal(err)
	}
	bundle.AddTranslation(languageWithTag(lang), translation)
	expected := "Bob is 26 years old."

	tf, err := bundle.Tfunc(lang)
	if err != nil {
		b.Fatal(err)
	}

	return func(data interface{}) {
		if result := tf(translationID, 26, data); result != expected {
			b.Fatalf("expected %q, got %q", expected, result)
		}
	}
}
Ejemplo n.º 6
0
func (s *GoI18nSifter) Filter(translated StringMap, sifted []*LocalizedString) StringMap {
	untranslated := make(StringMap)
	for _, xl := range sifted {
		_, ok := translated[xl.String]
		_, ok2 := untranslated[xl.String]
		if !ok && !ok2 {
			fmt.Printf("%s\n", xl.String)
			t, err := translation.NewTranslation(map[string]interface{}{
				"id":          xl.String,
				"translation": "",
			})
			if err != nil {
				panic(err)
			}
			untranslated[xl.String] = t
		}
	}
	return untranslated
}
Ejemplo n.º 7
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
}