luci的国际化(多语言)(转)
语言的选择在dispatch函数入口出完成,如果配置文件/etc/config/luci中配置的lang为auto,则根据浏览器所带的信息选择一个合适的语言,否则就使用lang定义的语言,然后使用i18n.lua中的setlanguage设置对应的语言。
root@hbg:# cat/etc/config/luci
config core 'main'
option mediaurlbase '/luci-static/openwrt.org'
option resourcebase '/luci-static/resources'
option lang 'en' --此处代表当前使用的语言
config extern 'flash_keep'
option uci '/etc/config/'
option dropbear '/etc/dropbear/'
option openvpn '/etc/openvpn/'
option passwd '/etc/passwd'
option opkg '/etc/opkg.conf'
option firewall '/etc/firewall.user'
option uploads '/lib/uci/upload/'
config internal 'languages' --目前支持的语言选择,(此处为自己在页面上设定的可选语言)
option zh_cn 'chinese' --对应 zh_cn
option en 'English' --对应 en
config internal 'sauth'
option sessionpath '/tmp/luci-sessions'
option sessiontime '3600'
config internal 'ccache'
option enable '1'
config internal 'themes'
option Bootstrap '/luci-static/bootstrap'
dispatcher.lua中dispatch函数中相关实现:
local lang = conf.main.lang or "auto"
if lang == "auto" then --如果选择auto时
local aclang = http.getenv("HTTP_ACCEPT_LANGUAGE") or ""
for lpat in aclang:gmatch("[%w-]+") do
lpat = lpat and lpat:gsub("-", "_")
if conf.languages[lpat] then
lang = lpat
break
end
end
end
require "luci.i18n".setlanguage(lang)
装载资源文件使用i18n.lua中的函数loadc。资源文件位于/usr/lib/lua/luci/i18n,后缀名为lmo,这些文件是有.po文件转换的紧凑格式。
可以查看这些lmo文件:
root@hbg:/usr/lib/lua/luci/i18n# ls
base.en.lmo firewall.es.lmo firewall.pl.lmo firewall.vi.lmo
base.zh-cn.lmo firewall.fr.lmo firewall.pt-br.lmo firewall.zh-cn.lmo
firewall.ca.lmo firewall.hu.lmo firewall.pt.lmo firewall.zh-tw.lmo
firewall.cs.lmo firewall.it.lmo firewall.ro.lmo
firewall.de.lmo firewall.ja.lmo firewall.ru.lmo
firewall.el.lmo firewall.no.lmo firewall.uk.lmo
如果对应的页面有自己的资源文件,可以在entry定义菜单时添加,如
entry({"admin", "network", "ahcpd"}, cbi("ahcp"), _("AHCP Server"), 90).i18n = "ahcp"
在disptach中会自动加载
if track.i18n then
i18n.loadc(track.i18n)
end
也可以在htm文件中主动调用loadc加载资源文件。
对于需要多语言显示的字符串,调用i18n.lua中的translate获取对应语言的字符串显示,参数是po文件对应的英文资源名称。

浙公网安备 33010602011771号