applications/luci-firewall: make rule descriptions fully translateable

This commit is contained in:
Jo-Philipp Wich 2011-12-20 00:08:07 +00:00
parent 50380b9fb6
commit 5de0c58bd9
3 changed files with 112 additions and 114 deletions

View file

@ -62,7 +62,7 @@ function s.parse(self, ...)
if created then if created then
m.uci:save("firewall") m.uci:save("firewall")
luci.http.redirect(ds.build_url( luci.http.redirect(ds.build_url(
"admin", "network", "firewall", "redirect", created "admin/network/firewall/redirect", created
)) ))
end end
end end
@ -87,55 +87,48 @@ src = s:option(DummyValue, "src", translate("Source"))
src.rawhtml = true src.rawhtml = true
src.width = "20%" src.width = "20%"
function src.cfgvalue(self, s) function src.cfgvalue(self, s)
local z = ft.fmt_zone(self.map:get(s, "src")) local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone"))
local a = ft.fmt_ip(self.map:get(s, "src_ip")) local a = ft.fmt_ip(self.map:get(s, "src_ip"), translate("any host"))
local p = ft.fmt_port(self.map:get(s, "src_port")) local p = ft.fmt_port(self.map:get(s, "src_port"))
local m = ft.fmt_mac(self.map:get(s, "src_mac")) local m = ft.fmt_mac(self.map:get(s, "src_mac"))
local s = "From %s in %s " %{
(a or "<var>any host</var>"),
(z or "<var>any zone</var>")
}
if p and m then if p and m then
s = s .. "with source %s and %s" %{ p, m } return translatef("From %s in %s with source %s and %s", a, z, p, m)
elseif p or m then elseif p or m then
s = s .. "with source %s" %( p or m ) return translatef("From %s in %s with source %s", a, z, p or m)
else
return translatef("From %s in %s", a, z)
end end
return s
end end
via = s:option(DummyValue, "via", translate("Via")) via = s:option(DummyValue, "via", translate("Via"))
via.rawhtml = true via.rawhtml = true
via.width = "20%" via.width = "20%"
function via.cfgvalue(self, s) function via.cfgvalue(self, s)
local a = ft.fmt_ip(self.map:get(s, "src_dip")) local a = ft.fmt_ip(self.map:get(s, "src_dip"), translate("any router IP"))
local p = ft.fmt_port(self.map:get(s, "src_dport")) local p = ft.fmt_port(self.map:get(s, "src_dport"))
--local z = self.map:get(s, "src") if p then
--local s = "To %s " %(a or "<var>any %s IP</var>" %( z or "router" )) return translatef("To %s at %s", a, p)
else
return "To %s%s" %{ return translatef("To %s", a)
(a or "<var>any router IP</var>"), end
(p and " at %s" % p or "")
}
end end
dest = s:option(DummyValue, "dest", translate("Destination")) dest = s:option(DummyValue, "dest", translate("Destination"))
dest.rawhtml = true dest.rawhtml = true
dest.width = "30%" dest.width = "30%"
function dest.cfgvalue(self, s) function dest.cfgvalue(self, s)
local z = ft.fmt_zone(self.map:get(s, "dest")) local z = ft.fmt_zone(self.map:get(s, "dest"), translate("any zone"))
local a = ft.fmt_ip(self.map:get(s, "dest_ip")) local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
local p = ft.fmt_port(self.map:get(s, "dest_port")) or local p = ft.fmt_port(self.map:get(s, "dest_port")) or
ft.fmt_port(self.map:get(s, "src_dport")) ft.fmt_port(self.map:get(s, "src_dport"))
return "Forward to %s%s in %s " %{ if p then
(a or "<var>any host</var>"), return translatef("Forward to %s, %s in %s", a, p, z)
(p and ", %s" % p or ""), else
(z or "<var>any zone</var>") return translatef("Forward to %s in %s", a, z)
} end
end end
return m return m

View file

@ -77,7 +77,7 @@ function s.parse(self, ...)
if created then if created then
m.uci:save("firewall") m.uci:save("firewall")
luci.http.redirect(ds.build_url( luci.http.redirect(ds.build_url(
"admin", "network", "firewall", "rules", created "admin/network/firewall/rules", created
)) ))
end end
end end
@ -111,23 +111,18 @@ src = s:option(DummyValue, "src", translate("Source"))
src.rawhtml = true src.rawhtml = true
src.width = "20%" src.width = "20%"
function src.cfgvalue(self, s) function src.cfgvalue(self, s)
local z = ft.fmt_zone(self.map:get(s, "src")) local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone"))
local a = ft.fmt_ip(self.map:get(s, "src_ip")) local a = ft.fmt_ip(self.map:get(s, "src_ip"), translate("any host"))
local p = ft.fmt_port(self.map:get(s, "src_port")) local p = ft.fmt_port(self.map:get(s, "src_port"))
local m = ft.fmt_mac(self.map:get(s, "src_mac")) local m = ft.fmt_mac(self.map:get(s, "src_mac"))
local s = "From %s in %s " %{
(a or "<var>any host</var>"),
(z or "<var>any zone</var>")
}
if p and m then if p and m then
s = s .. "with source %s and %s" %{ p, m } return translatef("From %s in %s with source %s and %s", a, z, p, m)
elseif p or m then elseif p or m then
s = s .. "with source %s" %( p or m ) return translatef("From %s in %s with source %s", a, z, p or m)
else
return translatef("From %s in %s", a, z)
end end
return s
end end
dest = s:option(DummyValue, "dest", translate("Destination")) dest = s:option(DummyValue, "dest", translate("Destination"))
@ -135,23 +130,27 @@ dest.rawhtml = true
dest.width = "20%" dest.width = "20%"
function dest.cfgvalue(self, s) function dest.cfgvalue(self, s)
local z = ft.fmt_zone(self.map:get(s, "dest")) local z = ft.fmt_zone(self.map:get(s, "dest"))
local a = ft.fmt_ip(self.map:get(s, "dest_ip"))
local p = ft.fmt_port(self.map:get(s, "dest_port")) local p = ft.fmt_port(self.map:get(s, "dest_port"))
-- Forward -- Forward
if z then if z then
return "To %s%s in %s" %{ local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
(a or "<var>any host</var>"), if p then
(p and ", %s" % p or ""), return translatef("To %s, %s in %s", a, p, z)
z else
} return translatef("To %s in %s", a, z)
end
-- Input -- Input
else else
return "To %s%s on <var>this device</var>" %{ local a = ft.fmt_ip(self.map:get(s, "dest_ip"),
(a or "<var>any router IP</var>"), translate("any router IP"))
(p and " at %s" % p or "")
} if p then
return translatef("To %s at %s on <var>this device</var>", a, p)
else
return translatef("To %s on <var>this device</var>", a)
end
end end
end end
@ -160,15 +159,15 @@ target = s:option(DummyValue, "target", translate("Action"))
target.rawhtml = true target.rawhtml = true
target.width = "20%" target.width = "20%"
function target.cfgvalue(self, s) function target.cfgvalue(self, s)
local z = ft.fmt_zone(self.map:get(s, "dest")) local t = ft.fmt_target(self.map:get(s, "target"), self.map:get(s, "dest"))
local l = ft.fmt_limit(self.map:get(s, "limit"), self.map:get(s, "limit_burst")) local l = ft.fmt_limit(self.map:get(s, "limit"),
local t = ft.fmt_target(self.map:get(s, "target")) self.map:get(s, "limit_burst"))
return "<var>%s</var> %s%s" %{ if l then
t, return translatef("<var>%s</var> and limit to %s", t, l)
(z and "forward" or "input"), else
(l and " and limit to %s" % l or "") return "<var>%s</var>" % t
} end
end end
@ -242,39 +241,34 @@ src = s:option(DummyValue, "src", translate("Source"))
src.rawhtml = true src.rawhtml = true
src.width = "20%" src.width = "20%"
function src.cfgvalue(self, s) function src.cfgvalue(self, s)
local z = ft.fmt_zone(self.map:get(s, "src")) local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone"))
local a = ft.fmt_ip(self.map:get(s, "src_ip")) local a = ft.fmt_ip(self.map:get(s, "src_ip"), translate("any host"))
local p = ft.fmt_port(self.map:get(s, "src_port")) local p = ft.fmt_port(self.map:get(s, "src_port"))
local m = ft.fmt_mac(self.map:get(s, "src_mac")) local m = ft.fmt_mac(self.map:get(s, "src_mac"))
local s = "From %s in %s " %{
(a or "<var>any host</var>"),
(z or "<var>any zone</var>")
}
if p and m then if p and m then
s = s .. "with source %s and %s" %{ p, m } return translatef("From %s in %s with source %s and %s", a, z, p, m)
elseif p or m then elseif p or m then
s = s .. "with source %s" %( p or m ) return translatef("From %s in %s with source %s", a, z, p or m)
else
return translatef("From %s in %s", a, z)
end end
return s
end end
dest = s:option(DummyValue, "dest", translate("Destination")) dest = s:option(DummyValue, "dest", translate("Destination"))
dest.rawhtml = true dest.rawhtml = true
dest.width = "30%" dest.width = "30%"
function dest.cfgvalue(self, s) function dest.cfgvalue(self, s)
local z = ft.fmt_zone(self.map:get(s, "dest")) local z = ft.fmt_zone(self.map:get(s, "dest"), translate("any zone"))
local a = ft.fmt_ip(self.map:get(s, "dest_ip")) local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
local p = ft.fmt_port(self.map:get(s, "dest_port")) or local p = ft.fmt_port(self.map:get(s, "dest_port")) or
ft.fmt_port(self.map:get(s, "src_dport")) ft.fmt_port(self.map:get(s, "src_dport"))
return "To %s%s in %s " %{ if p then
(a or "<var>any host</var>"), return translatef("To %s, %s in %s", a, p, z)
(p and ", %s" % p or ""), else
(z or "<var>any zone</var>") return translatef("To %s in %s", a, z)
} end
end end
snat = s:option(DummyValue, "via", translate("SNAT")) snat = s:option(DummyValue, "via", translate("SNAT"))
@ -284,15 +278,10 @@ function snat.cfgvalue(self, s)
local a = ft.fmt_ip(self.map:get(s, "src_dip")) local a = ft.fmt_ip(self.map:get(s, "src_dip"))
local p = ft.fmt_port(self.map:get(s, "src_dport")) local p = ft.fmt_port(self.map:get(s, "src_dport"))
--local z = self.map:get(s, "src")
--local s = "To %s " %(a or "<var>any %s IP</var>" %( z or "router" ))
if a and p then if a and p then
return "Rewrite to source %s, %s" %{ a, p } return translatef("Rewrite to source %s, %s", a, p)
elseif a or p then
return "Rewrite to source %s" %( a or p )
else else
return "Bug" return translatef("Rewrite to source %s", a or p)
end end
end end

View file

@ -18,13 +18,13 @@ local ut = require "luci.util"
local ip = require "luci.ip" local ip = require "luci.ip"
local nx = require "nixio" local nx = require "nixio"
local tr, trf = luci.i18n.translate, luci.i18n.translatef local translate, translatef = luci.i18n.translate, luci.i18n.translatef
function fmt_neg(x) function fmt_neg(x)
if type(x) == "string" then if type(x) == "string" then
local v, neg = x:gsub("^ *! *", "") local v, neg = x:gsub("^ *! *", "")
if neg > 0 then if neg > 0 then
return v, "%s " % tr("not") return v, "%s " % translate("not")
else else
return x, "" return x, ""
end end
@ -35,7 +35,7 @@ end
function fmt_mac(x) function fmt_mac(x)
if x and #x > 0 then if x and #x > 0 then
local m, n local m, n
local l = { tr("MAC"), " " } local l = { translate("MAC"), " " }
for m in ut.imatch(x) do for m in ut.imatch(x) do
m, n = fmt_neg(m) m, n = fmt_neg(m)
l[#l+1] = "<var>%s%s</var>" %{ n, m } l[#l+1] = "<var>%s%s</var>" %{ n, m }
@ -44,22 +44,22 @@ function fmt_mac(x)
if #l > 1 then if #l > 1 then
l[#l] = nil l[#l] = nil
if #l > 3 then if #l > 3 then
l[1] = tr("MACs") l[1] = translate("MACs")
end end
return table.concat(l, "") return table.concat(l, "")
end end
end end
end end
function fmt_port(x) function fmt_port(x, d)
if x and #x > 0 then if x and #x > 0 then
local p, n local p, n
local l = { tr("port"), " " } local l = { translate("port"), " " }
for p in ut.imatch(x) do for p in ut.imatch(x) do
p, n = fmt_neg(p) p, n = fmt_neg(p)
local a, b = p:match("(%d+)%D+(%d+)") local a, b = p:match("(%d+)%D+(%d+)")
if a and b then if a and b then
l[1] = tr("ports") l[1] = translate("ports")
l[#l+1] = "<var>%s%d-%d</var>" %{ n, a, b } l[#l+1] = "<var>%s%d-%d</var>" %{ n, a, b }
else else
l[#l+1] = "<var>%s%d</var>" %{ n, p } l[#l+1] = "<var>%s%d</var>" %{ n, p }
@ -69,16 +69,17 @@ function fmt_port(x)
if #l > 1 then if #l > 1 then
l[#l] = nil l[#l] = nil
if #l > 3 then if #l > 3 then
l[1] = tr("ports") l[1] = translate("ports")
end end
return table.concat(l, "") return table.concat(l, "")
end end
end end
return d and "<var>%s</var>" % d
end end
function fmt_ip(x) function fmt_ip(x, d)
if x and #x > 0 then if x and #x > 0 then
local l = { tr("IP"), " " } local l = { translate("IP"), " " }
local v, a, n local v, a, n
for v in ut.imatch(x) do for v in ut.imatch(x) do
v, n = fmt_neg(v) v, n = fmt_neg(v)
@ -86,7 +87,7 @@ function fmt_ip(x)
a = a or v a = a or v
a = a:match(":") and ip.IPv6(a, m) or ip.IPv4(a, m) a = a:match(":") and ip.IPv6(a, m) or ip.IPv4(a, m)
if a and (a:is6() or a:prefix() < 32) then if a and (a:is6() or a:prefix() < 32) then
l[1] = tr("IP range") l[1] = translate("IP range")
l[#l+1] = "<var title='%s - %s'>%s%s</var>" %{ l[#l+1] = "<var title='%s - %s'>%s%s</var>" %{
a:minhost():string(), a:minhost():string(),
a:maxhost():string(), a:maxhost():string(),
@ -103,25 +104,28 @@ function fmt_ip(x)
if #l > 1 then if #l > 1 then
l[#l] = nil l[#l] = nil
if #l > 3 then if #l > 3 then
l[1] = tr("IPs") l[1] = translate("IPs")
end end
return table.concat(l, "") return table.concat(l, "")
end end
end end
return d and "<var>%s</var>" % d
end end
function fmt_zone(x) function fmt_zone(x, d)
if x == "*" then if x == "*" then
return "<var>%s</var>" % tr("any zone") return "<var>%s</var>" % translate("any zone")
elseif x and #x > 0 then elseif x and #x > 0 then
return "<var>%s</var>" % x return "<var>%s</var>" % x
elseif d then
return "<var>%s</var>" % d
end end
end end
function fmt_icmp_type(x) function fmt_icmp_type(x)
if x and #x > 0 then if x and #x > 0 then
local t, v, n local t, v, n
local l = { tr("type"), " " } local l = { translate("type"), " " }
for v in ut.imatch(x) do for v in ut.imatch(x) do
v, n = fmt_neg(v) v, n = fmt_neg(v)
l[#l+1] = "<var>%s%s</var>" %{ n, v } l[#l+1] = "<var>%s%s</var>" %{ n, v }
@ -130,7 +134,7 @@ function fmt_icmp_type(x)
if #l > 1 then if #l > 1 then
l[#l] = nil l[#l] = nil
if #l > 3 then if #l > 3 then
l[1] = tr("types") l[1] = translate("types")
end end
return table.concat(l, "") return table.concat(l, "")
end end
@ -153,7 +157,7 @@ function fmt_proto(x, icmp_types)
if p then if p then
-- ICMP -- ICMP
if (p.proto == 1 or p.proto == 58) and t then if (p.proto == 1 or p.proto == 58) and t then
l[#l+1] = trf( l[#l+1] = translatef(
"%s%s with %s", "%s%s with %s",
n, p.aliases[1] or p.name, t n, p.aliases[1] or p.name, t
) )
@ -182,32 +186,44 @@ function fmt_limit(limit, burst)
u = u or "second" u = u or "second"
if l then if l then
if u:match("^s") then if u:match("^s") then
u = tr("second") u = translate("second")
elseif u:match("^m") then elseif u:match("^m") then
u = tr("minute") u = translate("minute")
elseif u:match("^h") then elseif u:match("^h") then
u = tr("hour") u = translate("hour")
elseif u:match("^d") then elseif u:match("^d") then
u = tr("day") u = translate("day")
end end
if burst and burst > 0 then if burst and burst > 0 then
return trf("<var>%d</var> pkts. per <var>%s</var>, \ return translatef("<var>%d</var> pkts. per <var>%s</var>, \
burst <var>%d</var> pkts.", l, u, burst) burst <var>%d</var> pkts.", l, u, burst)
else else
return trf("<var>%d</var> pkts. per <var>%s</var>", l, u) return translatef("<var>%d</var> pkts. per <var>%s</var>", l, u)
end end
end end
end end
end end
function fmt_target(x) function fmt_target(x, dest)
if x == "ACCEPT" then if dest and #dest > 0 then
return tr("Accept") if x == "ACCEPT" then
elseif x == "REJECT" then return translate("Accept forward")
return tr("Refuse") elseif x == "REJECT" then
elseif x == "NOTRACK" then return translate("Refuse forward")
return tr("Do not track") elseif x == "NOTRACK" then
else --if x == "DROP" then return translate("Do not track forward")
return tr("Discard") else --if x == "DROP" then
return translate("Discard forward")
end
else
if x == "ACCEPT" then
return translate("Accept input")
elseif x == "REJECT" then
return translate("Refuse input")
elseif x == "NOTRACK" then
return translate("Do not track input")
else --if x == "DROP" then
return translate("Discard input")
end
end end
end end