Skip to content

Commit

Permalink
#71 Allow i18n URL query param to be configurable (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
adelowo authored and jeevatkm committed Jun 19, 2017
1 parent fa52a65 commit 725c87e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
6 changes: 4 additions & 2 deletions param.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ func (e *engine) parseRequestParams(ctx *Context) {
}(req)
}

// i18n option override by Query parameter `lang`
if lang := ctx.Req.QueryValue(keyOverrideLocale); !ess.IsStrEmpty(lang) {
// i18n option via the config value "i18n.url_param_name".
// If that value is missing, we default to the `lang` query parameter
queryParam := AppConfig().StringDefault("i18n.param_name.query", keyOverrideLocale)
if lang := ctx.Req.QueryValue(queryParam); !ess.IsStrEmpty(lang) {
ctx.Req.Locale = ahttp.NewLocale(lang)
}

Expand Down
31 changes: 31 additions & 0 deletions param_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"

"aahframework.org/ahttp.v0"
"aahframework.org/config.v0"
"aahframework.org/essentials.v0"
"aahframework.org/test.v0/assert"
)
Expand Down Expand Up @@ -79,3 +80,33 @@ func TestParamParse(t *testing.T) {
e.parseRequestParams(ctx2)
assert.NotNil(t, ctx2.Req.Params.Form)
}

func TestParamParseLocaleFromAppConfiguration(t *testing.T) {
defer ess.DeleteFiles("testapp.pid")

cfg, err := config.ParseString(`
i18n {
param_name {
query = "language"
}
}
`)
appConfig = cfg

assert.Nil(t, err)

r := httptest.NewRequest("GET", "http://localhost:8080/index.html?language=en-CA", nil)
ctx1 := &Context{
Req: ahttp.ParseRequest(r, &ahttp.Request{}),
viewArgs: make(map[string]interface{}),
}

e := &engine{}

assert.Nil(t, ctx1.Req.Locale)
e.parseRequestParams(ctx1)
assert.NotNil(t, ctx1.Req.Locale)
assert.Equal(t, "en", ctx1.Req.Locale.Language)
assert.Equal(t, "CA", ctx1.Req.Locale.Region)
assert.Equal(t, "en-CA", ctx1.Req.Locale.String())
}

0 comments on commit 725c87e

Please sign in to comment.