luci-base: improve language detection
authorJo-Philipp Wich <jo@mein.io>
Sun, 13 Aug 2017 13:54:01 +0000 (15:54 +0200)
committerJo-Philipp Wich <jo@mein.io>
Sun, 13 Aug 2017 13:55:22 +0000 (15:55 +0200)
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>
modules/luci-base/luasrc/dispatcher.lua

index 1b684aa79c93271fca9a0f02511451d6af212d88..e4f77f18d89d27cfcf525e5d40e5f08d905adfdb 100644 (file)
@@ -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