a279e91006bce58846c2de7a81647efb4b5d22fc
[project/luci.git] / applications / luci-statistics / luasrc / statistics / i18n.lua
1 --[[
2
3 Luci statistics - diagram i18n helper
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 ]]--
15
16 module("luci.statistics.i18n", package.seeall)
17
18 require("luci.util")
19 require("luci.i18n")
20
21
22 Instance = luci.util.class()
23
24
25 function Instance.__init__( self, graph )
26         self.i18n  = luci.i18n
27         self.graph = graph
28
29         self.i18n.loadc("rrdtool")
30         self.i18n.loadc("statistics")
31 end
32
33 function Instance._subst( self, str, val )
34         str = str:gsub( "%%H",  self.graph.opts.host or "" )
35         str = str:gsub( "%%pn", val.plugin or "" )
36         str = str:gsub( "%%pi", val.pinst  or "" )
37         str = str:gsub( "%%dt", val.dtype  or "" )
38         str = str:gsub( "%%di", val.dinst  or "" )
39         str = str:gsub( "%%ds", val.dsrc   or "" )
40
41         return str
42 end
43
44 function Instance._translate( self, key, alt )
45         local val = self.i18n.string(key)
46         if val ~= key then
47                 return val
48         else
49                 return alt
50         end
51 end
52
53 function Instance.title( self, plugin, pinst, dtype, dinst, user_title )
54
55         local title = user_title or
56                 "p=%s/pi=%s/dt=%s/di=%s" % {
57                         plugin,
58                         (pinst and #pinst > 0) and pinst or "(nil)",
59                         (dtype and #dtype > 0) and dtype or "(nil)",
60                         (dinst and #dinst > 0) and dinst or "(nil)"
61                 }
62
63         return self:_subst( title, {
64                 plugin = plugin,
65                 pinst  = pinst,
66                 dtype  = dtype,
67                 dinst  = dinst
68         } )
69
70 end
71
72 function Instance.label( self, plugin, pinst, dtype, dinst, user_label )
73
74         local label = user_label or
75                 "dt=%s/di=%s" % {
76                         (dtype and #dtype > 0) and dtype or "(nil)",
77                         (dinst and #dinst > 0) and dinst or "(nil)"
78                 }
79
80         return self:_subst( label, {
81                 plugin = plugin,
82                 pinst  = pinst,
83                 dtype  = dtype,
84                 dinst  = dinst
85         } )
86
87 end
88
89 function Instance.ds( self, source )
90
91         local label = source.title or self:_translate(
92                 string.format( "stat_ds_%s_%s_%s", source.type, source.instance, source.ds ),
93                 self:_translate(
94                         string.format( "stat_ds_%s_%s", source.type, source.instance ),
95                         self:_translate(
96                                 string.format( "stat_ds_label_%s__%s", source.type, source.ds ),
97                                 self:_translate(
98                                         string.format( "stat_ds_%s", source.type ),
99                                         source.type .. "_" .. source.instance:gsub("[^%w]","_") .. "_" .. source.ds
100                                 )
101                         )
102                 )
103         )
104
105         return self:_subst( label, {
106                 dtype = source.type,
107                 dinst = source.instance,
108                 dsrc  = source.ds
109         } )
110
111 end