Skip to content

Commit caf6730

Browse files
authored
Merge pull request revel#1186 from notzippy/i18-lang-by-param
Adds the ability to set the language by a url parameter.
2 parents 02732b5 + 8e5c431 commit caf6730

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

i18n.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const (
2929

3030
var (
3131
// All currently loaded message configs.
32-
messages map[string]*config.Config
32+
messages map[string]*config.Config
33+
localeParameterName string
3334
)
3435

3536
// MessageFunc allows you to override the translation interface.
@@ -182,19 +183,31 @@ func parseLocaleFromFileName(file string) string {
182183
func init() {
183184
OnAppStart(func() {
184185
loadMessages(filepath.Join(BasePath, messageFilesDirectory))
185-
})
186+
localeParameterName = Config.StringDefault("i18n.locale.parameter", "")
187+
}, 0)
186188
}
187189

188190
func I18nFilter(c *Controller, fc []Filter) {
189-
if foundCookie, cookieValue := hasLocaleCookie(c.Request); foundCookie {
190-
TRACE.Printf("Found locale cookie value: %s", cookieValue)
191-
setCurrentLocaleControllerArguments(c, cookieValue)
192-
} else if foundHeader, headerValue := hasAcceptLanguageHeader(c.Request); foundHeader {
193-
TRACE.Printf("Found Accept-Language header value: %s", headerValue)
194-
setCurrentLocaleControllerArguments(c, headerValue)
195-
} else {
196-
TRACE.Println("Unable to find locale in cookie or header, using empty string")
197-
setCurrentLocaleControllerArguments(c, "")
191+
foundLocale := false
192+
// Search for a parameter first
193+
if localeParameterName != "" {
194+
if locale, found := c.Params.Values[localeParameterName]; found && len(locale[0]) > 0 {
195+
setCurrentLocaleControllerArguments(c, locale[0])
196+
foundLocale = true
197+
TRACE.Printf("Found locale parameter value: %s", locale[0])
198+
}
199+
}
200+
if !foundLocale {
201+
if foundCookie, cookieValue := hasLocaleCookie(c.Request); foundCookie {
202+
TRACE.Printf("Found locale cookie value: %s", cookieValue)
203+
setCurrentLocaleControllerArguments(c, cookieValue)
204+
} else if foundHeader, headerValue := hasAcceptLanguageHeader(c.Request); foundHeader {
205+
TRACE.Printf("Found Accept-Language header value: %s", headerValue)
206+
setCurrentLocaleControllerArguments(c, headerValue)
207+
} else {
208+
TRACE.Println("Unable to find locale in cookie or header, using empty string")
209+
setCurrentLocaleControllerArguments(c, "")
210+
}
198211
}
199212
fc[0](c, fc[1:])
200213
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy