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