Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang committed Mar 10, 2020
1 parent 908b51d commit eac1ade
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -7,7 +7,7 @@ This package is built on top of [text/language](https://pkg.go.dev/golang.org/x/
Please take a look of the following [example](example):

```shell
$ cd i18n/example
$ cd example
$ go run main.go
```

Expand All @@ -20,6 +20,12 @@ Home
$ curl "http://localhost:1234?lang=zh"
主页
$ curl "http://localhost:1234?lang=zh-TW"
主頁
$ curl "http://localhost:1234?lang=zh-HK"
主頁
## retrieve prefered language Cookie
$ curl -b "lang=zh-Hant" "http://localhost:1234"
主頁
Expand Down
8 changes: 8 additions & 0 deletions example/go.mod
@@ -0,0 +1,8 @@
module github.com/clevergo/i18n/example

go 1.14

require (
github.com/clevergo/clevergo v1.4.0 // indirect
github.com/clevergo/i18n v1.0.1 // indirect
)
7 changes: 7 additions & 0 deletions example/go.sum
@@ -0,0 +1,7 @@
github.com/clevergo/clevergo v1.4.0 h1:FNRUZPRBLDMrwn+giPsc5+usEqJ+vqhnFSH5KX97AUE=
github.com/clevergo/clevergo v1.4.0/go.mod h1:/BB51A8ohBG/GCHbN7mUPK6vJNm6XXC+4V/yS0c7RsA=
github.com/clevergo/i18n v1.0.1 h1:Qb2cq0JWzzWNBmep3kdNF38PfHXfPn44Npaowcsaopw=
github.com/clevergo/i18n v1.0.1/go.mod h1:1bALbvlWLHCWxQIL/of9I2uqLivc9CUWrun/S3y2zb0=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
25 changes: 14 additions & 11 deletions example/main.go
Expand Up @@ -3,22 +3,25 @@ package main
import (
"net/http"

"github.com/clevergo/clevergo"
"github.com/clevergo/i18n"
)

var (
translators *i18n.Translators
)

func index(w http.ResponseWriter, r *http.Request) {
translator := i18n.GetTranslator(r)
translator.Fprintf(w, "%m", "home")
func index(ctx *clevergo.Context) error {
translator := i18n.GetTranslator(ctx.Request)
translator.Fprintf(ctx.Response, "%m", "home")
return nil
}

func hello(w http.ResponseWriter, r *http.Request) {
translator := i18n.GetTranslator(r)
name := r.URL.Query().Get("name")
translator.Fprintf(w, "hello %s", name)
func hello(ctx *clevergo.Context) error {
translator := i18n.GetTranslator(ctx.Request)
name := ctx.Request.URL.Query().Get("name")
translator.Fprintf(ctx.Response, "hello %s", name)
return nil
}

func main() {
Expand All @@ -31,14 +34,14 @@ func main() {
panic(err)
}

mux := http.DefaultServeMux
mux.HandleFunc("/", index)
mux.HandleFunc("/hello", hello)
rotuer := clevergo.NewRouter()
rotuer.Get("/", index)
rotuer.Get("/hello", hello)
parsers := []i18n.LanguageParser{
i18n.NewURLLanguageParser("lang"), // from URL query
i18n.NewCookieLanguageParser("lang"), // from cookie
i18n.HeaderLanguageParser{}, // from Accept-Language header
}
handler := i18n.Handler(translators, mux, parsers...)
handler := i18n.Handler(translators, rotuer, parsers...)
http.ListenAndServe(":1234", handler)
}

0 comments on commit eac1ade

Please sign in to comment.