luci-base: dispatcher.lua: factor out language check into own function
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
parent
92eecedc8a
commit
a6b214f873
1 changed files with 34 additions and 30 deletions
|
@ -503,6 +503,38 @@ function error500(message)
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function determine_request_language()
|
||||||
|
local conf = require "luci.config"
|
||||||
|
assert(conf.main, "/etc/config/luci seems to be corrupt, unable to find section 'main'")
|
||||||
|
|
||||||
|
local lang = conf.main.lang or "auto"
|
||||||
|
if lang == "auto" then
|
||||||
|
local aclang = http.getenv("HTTP_ACCEPT_LANGUAGE") or ""
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
if lang == "auto" then
|
||||||
|
lang = i18n.default
|
||||||
|
end
|
||||||
|
|
||||||
|
i18n.setlanguage(lang)
|
||||||
|
end
|
||||||
|
|
||||||
function httpdispatch(request, prefix)
|
function httpdispatch(request, prefix)
|
||||||
http.context.request = request
|
http.context.request = request
|
||||||
|
|
||||||
|
@ -522,6 +554,8 @@ function httpdispatch(request, prefix)
|
||||||
r[#r+1] = node
|
r[#r+1] = node
|
||||||
end
|
end
|
||||||
|
|
||||||
|
determine_request_language()
|
||||||
|
|
||||||
local stat, err = util.coxpcall(function()
|
local stat, err = util.coxpcall(function()
|
||||||
dispatch(context.request)
|
dispatch(context.request)
|
||||||
end, error500)
|
end, error500)
|
||||||
|
@ -639,36 +673,6 @@ function dispatch(request)
|
||||||
local ctx = context
|
local ctx = context
|
||||||
ctx.path = request
|
ctx.path = request
|
||||||
|
|
||||||
local conf = require "luci.config"
|
|
||||||
assert(conf.main,
|
|
||||||
"/etc/config/luci seems to be corrupt, unable to find section 'main'")
|
|
||||||
|
|
||||||
local i18n = require "luci.i18n"
|
|
||||||
local lang = conf.main.lang or "auto"
|
|
||||||
if lang == "auto" then
|
|
||||||
local aclang = http.getenv("HTTP_ACCEPT_LANGUAGE") or ""
|
|
||||||
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
|
|
||||||
end
|
|
||||||
if lang == "auto" then
|
|
||||||
lang = i18n.default
|
|
||||||
end
|
|
||||||
i18n.setlanguage(lang)
|
|
||||||
|
|
||||||
local c = ctx.tree
|
local c = ctx.tree
|
||||||
local stat
|
local stat
|
||||||
if not c then
|
if not c then
|
||||||
|
|
Loading…
Reference in a new issue