本文整理匯總了Golang中github.com/nicksnyder/go-i18n/i18n/translation.NewTranslation函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewTranslation函數的具體用法?Golang NewTranslation怎麽用?Golang NewTranslation使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewTranslation函數的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: createBenchmarkTranslateFunc
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)
}
}
}
示例2: parseTranslationFile
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
}
示例3: testNewTranslation
func testNewTranslation(t *testing.T, data map[string]interface{}) translation.Translation {
translation, err := translation.NewTranslation(data)
if err != nil {
t.Fatal(err)
}
return translation
}
示例4: Load
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
}
示例5: createBenchmarkTranslateFunc
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)
}
}
}
示例6: Filter
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
}
示例7: LoadJSON
// 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
}