ac7f1979af06d0e9afc9d77881728c15b1dde6c8
[project/luci.git] / applications / luci-app-statistics / luasrc / statistics / i18n.lua
1 -- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
2 -- Licensed to the public under the Apache License 2.0.
3
4 module("luci.statistics.i18n", package.seeall)
5
6 require("luci.util")
7 require("luci.i18n")
8
9
10 Instance = luci.util.class()
11
12
13 function Instance.__init__( self, graph )
14 self.i18n = luci.i18n
15 self.graph = graph
16 end
17
18 function Instance._subst( self, str, val )
19 str = str:gsub( "%%H", self.graph.opts.host or "" )
20 str = str:gsub( "%%pn", val.plugin or "" )
21 str = str:gsub( "%%pi", val.pinst or "" )
22 str = str:gsub( "%%dt", val.dtype or "" )
23 str = str:gsub( "%%di", val.dinst or "" )
24 str = str:gsub( "%%ds", val.dsrc or "" )
25
26 return str
27 end
28
29 function Instance._translate( self, key, alt )
30 local val = self.i18n.string(key)
31 if val ~= key then
32 return val
33 else
34 return alt
35 end
36 end
37
38 function Instance.title( self, plugin, pinst, dtype, dinst, user_title )
39
40 local title = user_title or
41 "p=%s/pi=%s/dt=%s/di=%s" % {
42 plugin,
43 (pinst and #pinst > 0) and pinst or "(nil)",
44 (dtype and #dtype > 0) and dtype or "(nil)",
45 (dinst and #dinst > 0) and dinst or "(nil)"
46 }
47
48 return self:_subst( title, {
49 plugin = plugin,
50 pinst = pinst,
51 dtype = dtype,
52 dinst = dinst
53 } )
54
55 end
56
57 function Instance.label( self, plugin, pinst, dtype, dinst, user_label )
58
59 local label = user_label or
60 "dt=%s/di=%s" % {
61 (dtype and #dtype > 0) and dtype or "(nil)",
62 (dinst and #dinst > 0) and dinst or "(nil)"
63 }
64
65 return self:_subst( label, {
66 plugin = plugin,
67 pinst = pinst,
68 dtype = dtype,
69 dinst = dinst
70 } )
71
72 end
73
74 function Instance.ds( self, source )
75
76 local label = source.title or self:_translate(
77 string.format( "stat_ds_%s_%s_%s", source.type, source.instance, source.ds ),
78 self:_translate(
79 string.format( "stat_ds_%s_%s", source.type, source.instance ),
80 self:_translate(
81 string.format( "stat_ds_label_%s__%s", source.type, source.ds ),
82 self:_translate(
83 string.format( "stat_ds_%s", source.type ),
84 source.type .. "_" .. source.instance:gsub("[^%w]","_") .. "_" .. source.ds
85 )
86 )
87 )
88 )
89
90 return self:_subst( label, {
91 dtype = source.type,
92 dinst = source.instance,
93 dsrc = source.ds
94 } )
95
96 end