diff options
author | Jo-Philipp Wich <jo@mein.io> | 2017-08-13 15:54:01 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2017-08-13 15:55:22 +0200 |
commit | 15cb504b446add1d62bb318c683ed2cf78cd7041 (patch) | |
tree | 30338b671b656b7a1c3ebd83c1cdbb3167a84b55 /modules/luci-base/luasrc | |
parent | f03bee5a91b09b81b4de9536d5dacdc670c7fb26 (diff) |
luci-base: improve language detection
Properly deal with client accept languages containing a culture identifier
such as "zh-CN" or "pt-BR".
Fixes #1226.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules/luci-base/luasrc')
-rw-r--r-- | modules/luci-base/luasrc/dispatcher.lua | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/modules/luci-base/luasrc/dispatcher.lua b/modules/luci-base/luasrc/dispatcher.lua index 1b684aa79c..e4f77f18d8 100644 --- a/modules/luci-base/luasrc/dispatcher.lua +++ b/modules/luci-base/luasrc/dispatcher.lua @@ -219,10 +219,19 @@ function dispatch(request) local lang = conf.main.lang or "auto" if lang == "auto" then local aclang = http.getenv("HTTP_ACCEPT_LANGUAGE") or "" - for lpat in aclang:gmatch("[%w-]+") do - lpat = lpat and lpat:gsub("-", "_") - if conf.languages[lpat] then - lang = lpat + for aclang in aclang:gmatch("[%w_-]+") do + local country, culture = aclang:match("^([a-z][a-z])[_-]([a-zA-Z][a-zA-Z])$") + if country and culture then + local cc = "%s_%s" %{ country, culture:lower() } + if conf.languages[cc] then + lang = cc + break + elseif conf.languages[country] then + lang = country + break + end + elseif conf.languages[aclang] then + lang = aclang break end end |