Rework LuCI build system
[project/luci.git] / applications / luci-app-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 end
29
30 function Instance._subst( self, str, val )
31         str = str:gsub( "%%H",  self.graph.opts.host or "" )
32         str = str:gsub( "%%pn", val.plugin or "" )
33         str = str:gsub( "%%pi", val.pinst  or "" )
34         str = str:gsub( "%%dt", val.dtype  or "" )
35         str = str:gsub( "%%di", val.dinst  or "" )
36         str = str:gsub( "%%ds", val.dsrc   or "" )
37
38         return str
39 end
40
41 function Instance._translate( self, key, alt )
42         local val = self.i18n.string(key)
43         if val ~= key then
44                 return val
45         else
46                 return alt
47         end
48 end
49
50 function Instance.title( self, plugin, pinst, dtype, dinst, user_title )
51
52         local title = user_title or
53                 "p=%s/pi=%s/dt=%s/di=%s" % {
54                         plugin,
55                         (pinst and #pinst > 0) and pinst or "(nil)",
56                         (dtype and #dtype > 0) and dtype or "(nil)",
57                         (dinst and #dinst > 0) and dinst or "(nil)"
58                 }
59
60         return self:_subst( title, {
61                 plugin = plugin,
62                 pinst  = pinst,
63                 dtype  = dtype,
64                 dinst  = dinst
65         } )
66
67 end
68
69 function Instance.label( self, plugin, pinst, dtype, dinst, user_label )
70
71         local label = user_label or
72                 "dt=%s/di=%s" % {
73                         (dtype and #dtype > 0) and dtype or "(nil)",
74                         (dinst and #dinst > 0) and dinst or "(nil)"
75                 }
76
77         return self:_subst( label, {
78                 plugin = plugin,
79                 pinst  = pinst,
80                 dtype  = dtype,
81                 dinst  = dinst
82         } )
83
84 end
85
86 function Instance.ds( self, source )
87
88         local label = source.title or self:_translate(
89                 string.format( "stat_ds_%s_%s_%s", source.type, source.instance, source.ds ),
90                 self:_translate(
91                         string.format( "stat_ds_%s_%s", source.type, source.instance ),
92                         self:_translate(
93                                 string.format( "stat_ds_label_%s__%s", source.type, source.ds ),
94                                 self:_translate(
95                                         string.format( "stat_ds_%s", source.type ),
96                                         source.type .. "_" .. source.instance:gsub("[^%w]","_") .. "_" .. source.ds
97                                 )
98                         )
99                 )
100         )
101
102         return self:_subst( label, {
103                 dtype = source.type,
104                 dinst = source.instance,
105                 dsrc  = source.ds
106         } )
107
108 end