| 1 |
// Command example runs a sample webserver that uses go-i18n/v2/i18n. |
| 2 |
package main |
| 3 |
|
| 4 |
import ( |
| 5 |
"fmt" |
| 6 |
"html/template" |
| 7 |
"log" |
| 8 |
"net/http" |
| 9 |
"strconv" |
| 10 |
|
| 11 |
"github.com/BurntSushi/toml" |
| 12 |
"github.com/gohugoio/go-i18n/v2/i18n" |
| 13 |
"golang.org/x/text/language" |
| 14 |
) |
| 15 |
|
| 16 |
var page = template.Must(template.New("").Parse(` |
| 17 |
<!DOCTYPE html> |
| 18 |
<html> |
| 19 |
<body> |
| 20 |
|
| 21 |
<h1>{{.Title}}</h1> |
| 22 |
|
| 23 |
{{range .Paragraphs}}<p>{{.}}</p>{{end}} |
| 24 |
|
| 25 |
</body> |
| 26 |
</html> |
| 27 |
`)) |
| 28 |
|
| 29 |
func main() { |
| 30 |
bundle := i18n.NewBundle(language.English) |
| 31 |
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal) |
| 32 |
// No need to load active.en.toml since we are providing default translations. |
| 33 |
// bundle.MustLoadMessageFile("active.en.toml") |
| 34 |
bundle.MustLoadMessageFile("active.es.toml") |
| 35 |
|
| 36 |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 37 |
lang := r.FormValue("lang") |
| 38 |
accept := r.Header.Get("Accept-Language") |
| 39 |
localizer := i18n.NewLocalizer(bundle, lang, accept) |
| 40 |
|
| 41 |
name := r.FormValue("name") |
| 42 |
if name == "" { |
| 43 |
name = "Bob" |
| 44 |
} |
| 45 |
|
| 46 |
unreadEmailCount, _ := strconv.ParseInt(r.FormValue("unreadEmailCount"), 10, 64) |
| 47 |
|
| 48 |
helloPerson := localizer.MustLocalize(&i18n.LocalizeConfig{ |
| 49 |
DefaultMessage: &i18n.Message{ |
| 50 |
ID: "HelloPerson", |
| 51 |
Other: "Hello {{.Name}}", |
| 52 |
}, |
| 53 |
TemplateData: map[string]string{ |
| 54 |
"Name": name, |
| 55 |
}, |
| 56 |
}) |
| 57 |
|
| 58 |
myUnreadEmails := localizer.MustLocalize(&i18n.LocalizeConfig{ |
| 59 |
DefaultMessage: &i18n.Message{ |
| 60 |
ID: "MyUnreadEmails", |
| 61 |
Description: "The number of unread emails I have", |
| 62 |
One: "I have {{.PluralCount}} unread email.", |
| 63 |
Other: "I have {{.PluralCount}} unread emails.", |
| 64 |
}, |
| 65 |
PluralCount: unreadEmailCount, |
| 66 |
}) |
| 67 |
|
| 68 |
personUnreadEmails := localizer.MustLocalize(&i18n.LocalizeConfig{ |
| 69 |
DefaultMessage: &i18n.Message{ |
| 70 |
ID: "PersonUnreadEmails", |
| 71 |
Description: "The number of unread emails a person has", |
| 72 |
One: "{{.Name}} has {{.UnreadEmailCount}} unread email.", |
| 73 |
Other: "{{.Name}} has {{.UnreadEmailCount}} unread emails.", |
| 74 |
}, |
| 75 |
PluralCount: unreadEmailCount, |
| 76 |
TemplateData: map[string]interface{}{ |
| 77 |
"Name": name, |
| 78 |
"UnreadEmailCount": unreadEmailCount, |
| 79 |
}, |
| 80 |
}) |
| 81 |
|
| 82 |
err := page.Execute(w, map[string]interface{}{ |
| 83 |
"Title": helloPerson, |
| 84 |
"Paragraphs": []string{ |
| 85 |
myUnreadEmails, |
| 86 |
personUnreadEmails, |
| 87 |
}, |
| 88 |
}) |
| 89 |
if err != nil { |
| 90 |
panic(err) |
| 91 |
} |
| 92 |
}) |
| 93 |
|
| 94 |
fmt.Println("Listening on http://localhost:8080") |
| 95 |
log.Fatal(http.ListenAndServe(":8080", nil)) |
| 96 |
} |