| 1 |
package i18n_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
|
| 6 |
"github.com/BurntSushi/toml" |
| 7 |
"github.com/gohugoio/go-i18n/v2/i18n" |
| 8 |
"golang.org/x/text/language" |
| 9 |
) |
| 10 |
|
| 11 |
func ExampleLocalizer_MustLocalize() { |
| 12 |
bundle := i18n.NewBundle(language.English) |
| 13 |
localizer := i18n.NewLocalizer(bundle, "en") |
| 14 |
fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{ |
| 15 |
DefaultMessage: &i18n.Message{ |
| 16 |
ID: "HelloWorld", |
| 17 |
Other: "Hello World!", |
| 18 |
}, |
| 19 |
})) |
| 20 |
// Output: |
| 21 |
// Hello World! |
| 22 |
} |
| 23 |
|
| 24 |
func ExampleLocalizer_MustLocalize_noDefaultMessage() { |
| 25 |
bundle := i18n.NewBundle(language.English) |
| 26 |
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal) |
| 27 |
bundle.MustParseMessageFileBytes([]byte(` |
| 28 |
HelloWorld = "Hello World!" |
| 29 |
`), "en.toml") |
| 30 |
bundle.MustParseMessageFileBytes([]byte(` |
| 31 |
HelloWorld = "Hola Mundo!" |
| 32 |
`), "es.toml") |
| 33 |
|
| 34 |
{ |
| 35 |
localizer := i18n.NewLocalizer(bundle, "en-US") |
| 36 |
fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "HelloWorld"})) |
| 37 |
} |
| 38 |
{ |
| 39 |
localizer := i18n.NewLocalizer(bundle, "es-ES") |
| 40 |
fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "HelloWorld"})) |
| 41 |
} |
| 42 |
// Output: |
| 43 |
// Hello World! |
| 44 |
// Hola Mundo! |
| 45 |
} |
| 46 |
|
| 47 |
func ExampleLocalizer_MustLocalize_plural() { |
| 48 |
bundle := i18n.NewBundle(language.English) |
| 49 |
localizer := i18n.NewLocalizer(bundle, "en") |
| 50 |
catsMessage := &i18n.Message{ |
| 51 |
ID: "Cats", |
| 52 |
One: "I have {{.PluralCount}} cat.", |
| 53 |
Other: "I have {{.PluralCount}} cats.", |
| 54 |
} |
| 55 |
fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{ |
| 56 |
DefaultMessage: catsMessage, |
| 57 |
PluralCount: 1, |
| 58 |
})) |
| 59 |
fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{ |
| 60 |
DefaultMessage: catsMessage, |
| 61 |
PluralCount: 2, |
| 62 |
})) |
| 63 |
fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{ |
| 64 |
DefaultMessage: catsMessage, |
| 65 |
PluralCount: "2.5", |
| 66 |
})) |
| 67 |
// Output: |
| 68 |
// I have 1 cat. |
| 69 |
// I have 2 cats. |
| 70 |
// I have 2.5 cats. |
| 71 |
} |
| 72 |
|
| 73 |
func ExampleLocalizer_MustLocalize_template() { |
| 74 |
bundle := i18n.NewBundle(language.English) |
| 75 |
localizer := i18n.NewLocalizer(bundle, "en") |
| 76 |
helloPersonMessage := &i18n.Message{ |
| 77 |
ID: "HelloPerson", |
| 78 |
Other: "Hello {{.Name}}!", |
| 79 |
} |
| 80 |
fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{ |
| 81 |
DefaultMessage: helloPersonMessage, |
| 82 |
TemplateData: map[string]string{"Name": "Nick"}, |
| 83 |
})) |
| 84 |
// Output: |
| 85 |
// Hello Nick! |
| 86 |
} |
| 87 |
|
| 88 |
func ExampleLocalizer_MustLocalize_plural_template() { |
| 89 |
bundle := i18n.NewBundle(language.English) |
| 90 |
localizer := i18n.NewLocalizer(bundle, "en") |
| 91 |
personCatsMessage := &i18n.Message{ |
| 92 |
ID: "PersonCats", |
| 93 |
One: "{{.Name}} has {{.Count}} cat.", |
| 94 |
Other: "{{.Name}} has {{.Count}} cats.", |
| 95 |
} |
| 96 |
fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{ |
| 97 |
DefaultMessage: personCatsMessage, |
| 98 |
PluralCount: 1, |
| 99 |
TemplateData: map[string]interface{}{ |
| 100 |
"Name": "Nick", |
| 101 |
"Count": 1, |
| 102 |
}, |
| 103 |
})) |
| 104 |
fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{ |
| 105 |
DefaultMessage: personCatsMessage, |
| 106 |
PluralCount: 2, |
| 107 |
TemplateData: map[string]interface{}{ |
| 108 |
"Name": "Nick", |
| 109 |
"Count": 2, |
| 110 |
}, |
| 111 |
})) |
| 112 |
fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{ |
| 113 |
DefaultMessage: personCatsMessage, |
| 114 |
PluralCount: "2.5", |
| 115 |
TemplateData: map[string]interface{}{ |
| 116 |
"Name": "Nick", |
| 117 |
"Count": "2.5", |
| 118 |
}, |
| 119 |
})) |
| 120 |
// Output: |
| 121 |
// Nick has 1 cat. |
| 122 |
// Nick has 2 cats. |
| 123 |
// Nick has 2.5 cats. |
| 124 |
} |
| 125 |
|
| 126 |
func ExampleLocalizer_MustLocalize_customTemplateDelims() { |
| 127 |
bundle := i18n.NewBundle(language.English) |
| 128 |
localizer := i18n.NewLocalizer(bundle, "en") |
| 129 |
helloPersonMessage := &i18n.Message{ |
| 130 |
ID: "HelloPerson", |
| 131 |
Other: "Hello <<.Name>>!", |
| 132 |
LeftDelim: "<<", |
| 133 |
RightDelim: ">>", |
| 134 |
} |
| 135 |
fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{ |
| 136 |
DefaultMessage: helloPersonMessage, |
| 137 |
TemplateData: map[string]string{"Name": "Nick"}, |
| 138 |
})) |
| 139 |
// Output: |
| 140 |
// Hello Nick! |
| 141 |
} |