From 5de0c58bd9da282de31934a2fe3b12240d5238a2 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Tue, 20 Dec 2011 00:08:07 +0000 Subject: [PATCH] applications/luci-firewall: make rule descriptions fully translateable --- .../luasrc/model/cbi/firewall/forwards.lua | 47 ++++---- .../luasrc/model/cbi/firewall/rules.lua | 101 ++++++++---------- .../luci-firewall/luasrc/tools/firewall.lua | 80 ++++++++------ 3 files changed, 113 insertions(+), 115 deletions(-) diff --git a/applications/luci-firewall/luasrc/model/cbi/firewall/forwards.lua b/applications/luci-firewall/luasrc/model/cbi/firewall/forwards.lua index 3ab3658e9..b07aa9041 100644 --- a/applications/luci-firewall/luasrc/model/cbi/firewall/forwards.lua +++ b/applications/luci-firewall/luasrc/model/cbi/firewall/forwards.lua @@ -62,7 +62,7 @@ function s.parse(self, ...) if created then m.uci:save("firewall") luci.http.redirect(ds.build_url( - "admin", "network", "firewall", "redirect", created + "admin/network/firewall/redirect", created )) end end @@ -87,55 +87,48 @@ src = s:option(DummyValue, "src", translate("Source")) src.rawhtml = true src.width = "20%" function src.cfgvalue(self, s) - local z = ft.fmt_zone(self.map:get(s, "src")) - local a = ft.fmt_ip(self.map:get(s, "src_ip")) + local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone")) + 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 m = ft.fmt_mac(self.map:get(s, "src_mac")) - local s = "From %s in %s " %{ - (a or "any host"), - (z or "any zone") - } - 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 - 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 - - return s end via = s:option(DummyValue, "via", translate("Via")) via.rawhtml = true via.width = "20%" 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 z = self.map:get(s, "src") - --local s = "To %s " %(a or "any %s IP" %( z or "router" )) - - return "To %s%s" %{ - (a or "any router IP"), - (p and " at %s" % p or "") - } + if p then + return translatef("To %s at %s", a, p) + else + return translatef("To %s", a) + end end dest = s:option(DummyValue, "dest", translate("Destination")) dest.rawhtml = true dest.width = "30%" function dest.cfgvalue(self, s) - local z = ft.fmt_zone(self.map:get(s, "dest")) - local a = ft.fmt_ip(self.map:get(s, "dest_ip")) + local z = ft.fmt_zone(self.map:get(s, "dest"), translate("any zone")) + 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 ft.fmt_port(self.map:get(s, "src_dport")) - return "Forward to %s%s in %s " %{ - (a or "any host"), - (p and ", %s" % p or ""), - (z or "any zone") - } + if p then + return translatef("Forward to %s, %s in %s", a, p, z) + else + return translatef("Forward to %s in %s", a, z) + end end return m diff --git a/applications/luci-firewall/luasrc/model/cbi/firewall/rules.lua b/applications/luci-firewall/luasrc/model/cbi/firewall/rules.lua index 05e98a9db..f76380c79 100644 --- a/applications/luci-firewall/luasrc/model/cbi/firewall/rules.lua +++ b/applications/luci-firewall/luasrc/model/cbi/firewall/rules.lua @@ -77,7 +77,7 @@ function s.parse(self, ...) if created then m.uci:save("firewall") luci.http.redirect(ds.build_url( - "admin", "network", "firewall", "rules", created + "admin/network/firewall/rules", created )) end end @@ -111,23 +111,18 @@ src = s:option(DummyValue, "src", translate("Source")) src.rawhtml = true src.width = "20%" function src.cfgvalue(self, s) - local z = ft.fmt_zone(self.map:get(s, "src")) - local a = ft.fmt_ip(self.map:get(s, "src_ip")) + local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone")) + 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 m = ft.fmt_mac(self.map:get(s, "src_mac")) - local s = "From %s in %s " %{ - (a or "any host"), - (z or "any zone") - } - 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 - 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 - - return s end dest = s:option(DummyValue, "dest", translate("Destination")) @@ -135,23 +130,27 @@ dest.rawhtml = true dest.width = "20%" function dest.cfgvalue(self, s) 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")) -- Forward if z then - return "To %s%s in %s" %{ - (a or "any host"), - (p and ", %s" % p or ""), - z - } + local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host")) + if p then + return translatef("To %s, %s in %s", a, p, z) + else + return translatef("To %s in %s", a, z) + end -- Input else - return "To %s%s on this device" %{ - (a or "any router IP"), - (p and " at %s" % p or "") - } + local a = ft.fmt_ip(self.map:get(s, "dest_ip"), + translate("any router IP")) + + if p then + return translatef("To %s at %s on this device", a, p) + else + return translatef("To %s on this device", a) + end end end @@ -160,15 +159,15 @@ target = s:option(DummyValue, "target", translate("Action")) target.rawhtml = true target.width = "20%" function target.cfgvalue(self, s) - local z = ft.fmt_zone(self.map:get(s, "dest")) - local l = ft.fmt_limit(self.map:get(s, "limit"), self.map:get(s, "limit_burst")) - local t = ft.fmt_target(self.map:get(s, "target")) - - return "%s %s%s" %{ - t, - (z and "forward" or "input"), - (l and " and limit to %s" % l or "") - } + 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")) + + if l then + return translatef("%s and limit to %s", t, l) + else + return "%s" % t + end end @@ -242,39 +241,34 @@ src = s:option(DummyValue, "src", translate("Source")) src.rawhtml = true src.width = "20%" function src.cfgvalue(self, s) - local z = ft.fmt_zone(self.map:get(s, "src")) - local a = ft.fmt_ip(self.map:get(s, "src_ip")) + local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone")) + 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 m = ft.fmt_mac(self.map:get(s, "src_mac")) - local s = "From %s in %s " %{ - (a or "any host"), - (z or "any zone") - } - 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 - 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 - - return s end dest = s:option(DummyValue, "dest", translate("Destination")) dest.rawhtml = true dest.width = "30%" function dest.cfgvalue(self, s) - local z = ft.fmt_zone(self.map:get(s, "dest")) - local a = ft.fmt_ip(self.map:get(s, "dest_ip")) + local z = ft.fmt_zone(self.map:get(s, "dest"), translate("any zone")) + 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 ft.fmt_port(self.map:get(s, "src_dport")) - return "To %s%s in %s " %{ - (a or "any host"), - (p and ", %s" % p or ""), - (z or "any zone") - } + if p then + return translatef("To %s, %s in %s", a, p, z) + else + return translatef("To %s in %s", a, z) + end end 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 p = ft.fmt_port(self.map:get(s, "src_dport")) - --local z = self.map:get(s, "src") - --local s = "To %s " %(a or "any %s IP" %( z or "router" )) - if a and p then - return "Rewrite to source %s, %s" %{ a, p } - elseif a or p then - return "Rewrite to source %s" %( a or p ) + return translatef("Rewrite to source %s, %s", a, p) else - return "Bug" + return translatef("Rewrite to source %s", a or p) end end diff --git a/applications/luci-firewall/luasrc/tools/firewall.lua b/applications/luci-firewall/luasrc/tools/firewall.lua index a2e3bce34..1847d14a7 100644 --- a/applications/luci-firewall/luasrc/tools/firewall.lua +++ b/applications/luci-firewall/luasrc/tools/firewall.lua @@ -18,13 +18,13 @@ local ut = require "luci.util" local ip = require "luci.ip" 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) if type(x) == "string" then local v, neg = x:gsub("^ *! *", "") if neg > 0 then - return v, "%s " % tr("not") + return v, "%s " % translate("not") else return x, "" end @@ -35,7 +35,7 @@ end function fmt_mac(x) if x and #x > 0 then local m, n - local l = { tr("MAC"), " " } + local l = { translate("MAC"), " " } for m in ut.imatch(x) do m, n = fmt_neg(m) l[#l+1] = "%s%s" %{ n, m } @@ -44,22 +44,22 @@ function fmt_mac(x) if #l > 1 then l[#l] = nil if #l > 3 then - l[1] = tr("MACs") + l[1] = translate("MACs") end return table.concat(l, "") end end end -function fmt_port(x) +function fmt_port(x, d) if x and #x > 0 then local p, n - local l = { tr("port"), " " } + local l = { translate("port"), " " } for p in ut.imatch(x) do p, n = fmt_neg(p) local a, b = p:match("(%d+)%D+(%d+)") if a and b then - l[1] = tr("ports") + l[1] = translate("ports") l[#l+1] = "%s%d-%d" %{ n, a, b } else l[#l+1] = "%s%d" %{ n, p } @@ -69,16 +69,17 @@ function fmt_port(x) if #l > 1 then l[#l] = nil if #l > 3 then - l[1] = tr("ports") + l[1] = translate("ports") end return table.concat(l, "") end end + return d and "%s" % d end -function fmt_ip(x) +function fmt_ip(x, d) if x and #x > 0 then - local l = { tr("IP"), " " } + local l = { translate("IP"), " " } local v, a, n for v in ut.imatch(x) do v, n = fmt_neg(v) @@ -86,7 +87,7 @@ function fmt_ip(x) a = a or v a = a:match(":") and ip.IPv6(a, m) or ip.IPv4(a, m) if a and (a:is6() or a:prefix() < 32) then - l[1] = tr("IP range") + l[1] = translate("IP range") l[#l+1] = "%s%s" %{ a:minhost():string(), a:maxhost():string(), @@ -103,25 +104,28 @@ function fmt_ip(x) if #l > 1 then l[#l] = nil if #l > 3 then - l[1] = tr("IPs") + l[1] = translate("IPs") end return table.concat(l, "") end end + return d and "%s" % d end -function fmt_zone(x) +function fmt_zone(x, d) if x == "*" then - return "%s" % tr("any zone") + return "%s" % translate("any zone") elseif x and #x > 0 then return "%s" % x + elseif d then + return "%s" % d end end function fmt_icmp_type(x) if x and #x > 0 then local t, v, n - local l = { tr("type"), " " } + local l = { translate("type"), " " } for v in ut.imatch(x) do v, n = fmt_neg(v) l[#l+1] = "%s%s" %{ n, v } @@ -130,7 +134,7 @@ function fmt_icmp_type(x) if #l > 1 then l[#l] = nil if #l > 3 then - l[1] = tr("types") + l[1] = translate("types") end return table.concat(l, "") end @@ -153,7 +157,7 @@ function fmt_proto(x, icmp_types) if p then -- ICMP if (p.proto == 1 or p.proto == 58) and t then - l[#l+1] = trf( + l[#l+1] = translatef( "%s%s with %s", n, p.aliases[1] or p.name, t ) @@ -182,32 +186,44 @@ function fmt_limit(limit, burst) u = u or "second" if l then if u:match("^s") then - u = tr("second") + u = translate("second") elseif u:match("^m") then - u = tr("minute") + u = translate("minute") elseif u:match("^h") then - u = tr("hour") + u = translate("hour") elseif u:match("^d") then - u = tr("day") + u = translate("day") end if burst and burst > 0 then - return trf("%d pkts. per %s, \ + return translatef("%d pkts. per %s, \ burst %d pkts.", l, u, burst) else - return trf("%d pkts. per %s", l, u) + return translatef("%d pkts. per %s", l, u) end end end end -function fmt_target(x) - if x == "ACCEPT" then - return tr("Accept") - elseif x == "REJECT" then - return tr("Refuse") - elseif x == "NOTRACK" then - return tr("Do not track") - else --if x == "DROP" then - return tr("Discard") +function fmt_target(x, dest) + if dest and #dest > 0 then + if x == "ACCEPT" then + return translate("Accept forward") + elseif x == "REJECT" then + return translate("Refuse forward") + elseif x == "NOTRACK" then + return translate("Do not track forward") + 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 -- 2.25.1