Merge pull request #606 from SvenRoederer/update_community-berlin
[project/luci.git] / documentation / LuCI-0.10.md
1 [[PageOutline(2-5, Table of Contents, floated)]]
2
3
4 This document describes new features and incompatibilities to LuCI 0.9.x.
5 It is targeted at module authors developing external addons to LuCI.
6
7 # I18N Changes
8
9 ## API
10
11 The call conventions for the i18n api changed, there is no dedicated translation
12 key anymore and the english text is used for lookup instead. This was done to
13 ease the maintenance of language files.
14
15 Code that uses _translate()'' or ''i18n()_ must be changed as follows:
16
17         
18         -- old style:
19         translate("some_text", "Some Text")
20         translatef("some_format_text", "Some formatted Text: %d", 123)
21         
22         -- new style:
23         translate("Some Text")
24         translatef("Some formatted Text: %d", 123)
25         
26
27 Likewise for templates:
28
29         
30         <!-- old style: -->
31         <%:some_text Some Text%>
32         
33         <!-- new style: -->
34         <%:Some Text%>
35         
36
37 If code must support both LuCI 0.9.x and 0.10.x versions, it is suggested to write the calls as follows:
38         
39         translate("Some Text", "Some Text")
40         
41
42 An alternative is wrapping translate() calls into a helper function:
43         
44         function tr(key, alt)
45             return translate(key) or translate(alt) or alt
46         end
47         
48
49 ... which is used as follows:
50         
51         tr("some_key", "Some Text")
52         
53
54 ## Translation File Format
55
56 Translation catalogs are now maintained in *.po format files. During build those get translated
57 into [*.lmo archives](http://luci.subsignal.org/trac/wiki/Documentation/LMO).
58
59 LuCI ships a [utility script](http://luci.subsignal.org/trac/browser/luci/branches/luci-0.10/build/i18n-lua2po.pl)
60 in the build/ directory to convert old Lua translation files to the *.po format. The generated *.po files should
61 be placed in the appropriate subdirectories within the top po/ file in the LuCI source tree.
62
63 ### Components built within the LuCI tree
64
65 If components using translations are built along with the LuCI tree, the newly added *.po file are automatically
66 compiled into *.lmo archives during the build process. In order to bundle the appropriate *.lmo files into the
67 corresponding *.ipk packages, component Makefiles must include a "PO" variable specifying the files to include.
68
69 Given a module _applications/example/'' which uses ''po/en/example.po'' and ''po/en/example-extra.po_,
70 the _applications/example/Makefile_ must be changed as follows:
71
72         
73         PO = example example-extra
74         
75         include ../../build/config.mk
76         include ../../build/module.mk
77         
78
79 ### Standalone components
80
81 Authors who externally package LuCI components must prepare required *.lmo archives themselves.
82 To convert existing Lua based message catalogs to the *.po format, the build/i18n-lua2po.pl helper script can be used.
83 In order to convert *.po files into *.lmo files, the standalone "po2lmo" utility must be compiled as follows:
84
85         
86         $ svn co http://svn.luci.subsignal.org/luci/branches/luci-0.10/libs/lmo
87         $ cd lmo/
88         $ make
89         $ ./src/po2lmo translations.po translations.lmo
90         
91
92 Note that at the time of writing, the utility program needs Lua headers installed on the system in order to compile properly.
93
94 # CBI
95
96 ## Datatypes
97
98 The server side UVL validation has been dropped to reduce space requirements on the target.
99 Instead it is possible to define datatypes for CBI widgets now:
100
101         
102         opt = section:option(Value, "optname", "Title Text")
103         opt.datatype = "ip4addr"
104         
105
106 User provided data is validated once on the frontend via JavaScript and on the server side prior to saving it.
107 A list of possible datatypes can be found in the [luci.cbi.datatypes](http://luci.subsignal.org/trac/browser/luci/branches/luci-0.10/libs/web/luasrc/cbi/datatypes.lua#L26) class.
108
109 ## Validation
110
111 Server-sided validator function can now return custom error messages to provide better feedback on invalid input.
112
113         
114         opt = section:option(Value, "optname", "Title Text")
115         
116         function opt.validate(self, value, section)
117             if input_is_valid(value) then
118                 return value
119             else
120                 return nil, "The value is invalid because ..."
121             end
122         end
123         
124
125 ## Tabs
126
127 It is now possible to break up CBI sections into multiple tabs to better organize longer forms.
128 The TypedSection and NamedSection classes gained two new functions to define tabs, _tab()'' and ''taboption()_.
129
130         
131         sct = map:section(TypedSection, "name", "type", "Title Text")
132         
133         sct:tab("general", "General Tab Title", "General Tab Description")
134         sct:tab("advanced", "Advanced Tab Title", "Advanced Tab Description")
135         
136         opt = sct:taboption("general", Value, "optname", "Title Text")
137         ...
138         
139
140 The _tab()_ function is declares a new tab and takes up to three arguments:
141   * Internal name of the tab, must be unique within the section
142   * Title text of the tab
143   * Optional description text for the tab
144
145 The _taboption()'' function wraps ''option()_ and assigns the option object to the given tab.
146 It takes up to five arguments:
147
148   * Name of the tab to assign the option to
149   * Option type, e.g. Value or DynamicList
150   * Option name
151   * Title text of the option
152   * Optional description text of the option
153
154 If tabs are used within a particular section, the _option()_ function must not be used,
155 doing so results in undefined behaviour.
156
157 ## Hooks
158
159 The CBI gained support for _hooks_ which can be used to trigger additional actions during the
160 life-cycle of a map:
161
162         
163         map = Map("config", "Title Text")
164         
165         function map.on_commit(self)
166             -- do something if the UCI configuration got committed
167         end
168         
169
170 The following hooks are defined:
171
172 || on_cancel || The user pressed cancel within a multi-step Delegator or a SimpleForm instance || 
173 || on_init || The CBI is about to render the Map object ||
174 || on_parse || The CBI is about to read received HTTP form values ||
175 || on_save, on_before_save || The CBI is about to save modified UCI configuration files ||
176 || on_after_save || Modified UCI configuration files just got saved
177 || on_before_commit || The CBI is about to commit the changes ||
178 || on_commit, on_after_commit, on_before_apply || Modified configurations got committed and the CBI is about to restart associated services ||
179 || on_apply, on_after_apply || All changes where completely applied (only works on Map instances with the apply_on_parse attribute set) ||
180
181 ## Sortable Tables
182
183 TypedSection instances which use the "cbi/tblsection" template may now use a new attribute _sortable_ to allow the user to reorder table rows.
184
185         
186         sct = map:section(TypedSection, "name", "type", "Title Text")
187         sct.template = "cbi/tblsection"
188         sct.sortable = true
189         
190         ...
191         
192
193 # JavaScript
194
195 The LuCI 0.10 branch introduced a new JavaScript file _xhr.js_ which provides support routines for XMLHttpRequest operations.
196 Each theme must include this file in the <head> area of the document for forms to work correctly.
197
198 It should be included like this:
199
200         
201         <script type="text/javascript" src="<%=resource%>/xhr.js"></script>
202