-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlang.lua
86 lines (78 loc) · 2.83 KB
/
lang.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
--------------------------------------------------
-- HTTP Accept-Language header handler --
-- @author [email protected] --
-- @license MIT --
-- @requires: --
-- -gnu find --
-- @description: --
-- redirects to subfolders according --
-- to the http Accept-Language header --
-- @example coinfguration: --
-- --
-- server { --
-- listen 8080 default_server; --
-- index index.html index.htm; --
-- server_name localhost; --
-- --
-- set $root /usr/share/nginx/html; --
-- root $root; --
-- --
-- location /index.html { --
-- # lua_code_cache off; --
-- set $default_lang "cz"; --
-- set $ngx_html_path $root; --
-- rewrite_by_lua_file lang.lua; --
-- } --
-- } --
-- --
--------------------------------------------------
function scandir(directory)
local t = {}
for filename in io.popen('find "'..directory..'" -type d -mindepth 1 -maxdepth 1 -printf "%f\n"'):lines() do
table.insert(t, filename)
end
return t
end
function inTable(tbl, item)
for key, value in pairs(tbl) do
if value == item then return key end
end
return false
end
local default_lang = ngx.var.default_lang or "en"
local folders = scandir(ngx.var.ngx_html_path)
local lang_header = ngx.var.http_accept_language
if ( lang_header == nil ) then
ngx.redirect( "/" .. default_lang )
return
end
local cleaned = ngx.re.sub(lang_header, "^.*:", "")
local options = {}
local iterator, err = ngx.re.gmatch(cleaned, "\\s*([a-z]+(?:-[a-z])*)\\s*(?:;q=([0-9]+(.[0-9]*)?))?\\s*(,|$)", "i")
for m, err in iterator do
local lang = m[1]
local priority = 1
if m[2] ~= nil then
priority = tonumber(m[2])
if priority == nil then
priority = 1
end
end
table.insert(options, {lang, priority})
end
--for index, lang in pairs(options) do
-- ngx.print(lang[1] .. " := " .. lang[2] .. "<br>")
--end
--ngx.print("\n")
table.sort(options, function(a,b) return b[2] < a[2] end)
local redirected = false
for index, lang in pairs(options) do
if inTable(folders, lang[1]) then
ngx.redirect( "/" .. lang[1] )
redirected = true
break
end
end
if not redirected then
ngx.redirect( "/" .. default_lang )
end