[enh] i18n

This commit is contained in:
kload 2014-05-12 12:32:56 +00:00
parent bf14b2f17f
commit 82457d6437
6 changed files with 74 additions and 41 deletions

View file

@ -9,6 +9,10 @@ if not srvkey then
cache:add("srvkey", srvkey) cache:add("srvkey", srvkey)
end end
cookies = {} cookies = {}
lang = ngx.req.get_headers()["Accept-Language"]
if lang then
lang = string.sub(lang, 1, 2)
end
-- Load conf file -- Load conf file
local conf_file = assert(io.open(conf_path, "r"), "Configuration file is missing") local conf_file = assert(io.open(conf_path, "r"), "Configuration file is missing")
@ -45,7 +49,8 @@ default_conf = {
ldap_group = "ou=users,dc=yunohost,dc=org", ldap_group = "ou=users,dc=yunohost,dc=org",
ldap_identifier = "uid", ldap_identifier = "uid",
ldap_attributes = {"uid", "givenname", "sn", "cn", "homedirectory", "mail", "maildrop"}, ldap_attributes = {"uid", "givenname", "sn", "cn", "homedirectory", "mail", "maildrop"},
allow_mail_authentication = true allow_mail_authentication = true,
default_language = "en"
} }
for param, default_value in pairs(default_conf) do for param, default_value in pairs(default_conf) do
@ -86,6 +91,14 @@ function string.ends (String, End)
return End=='' or string.sub(String, -string.len(End)) == End return End=='' or string.sub(String, -string.len(End)) == End
end end
function t (key)
if lang and i18n[lang] then
return i18n[lang][key] or ""
else
return i18n[conf["default_language"]][key] or ""
end
end
function cook (cookie_str) function cook (cookie_str)
table.insert(cookies, cookie_str) table.insert(cookies, cookie_str)
end end
@ -233,7 +246,7 @@ function set_headers (user)
end end
user = user or ngx.var.cookie_SSOwAuthUser user = user or ngx.var.cookie_SSOwAuthUser
if not cache:get(user.."-password") then if not cache:get(user.."-password") then
flash("info", "Please log in to access to this content") flash("info", t("please_login"))
local back_url = ngx.var.scheme .. "://" .. ngx.var.host .. ngx.var.uri .. uri_args_string() local back_url = ngx.var.scheme .. "://" .. ngx.var.host .. ngx.var.uri .. uri_args_string()
return redirect(portal_url.."?r="..ngx.encode_base64(back_url)) return redirect(portal_url.."?r="..ngx.encode_base64(back_url))
end end
@ -371,7 +384,7 @@ function get_data_for(view)
local data = {} local data = {}
if view == "login.html" then if view == "login.html" then
data["title"] = "YunoHost Login" data["title"] = t("login")
elseif view == "info.html" then elseif view == "info.html" then
set_headers(user) set_headers(user)
@ -395,7 +408,7 @@ function get_data_for(view)
elseif view == "password.html" then elseif view == "password.html" then
data = { data = {
title = "Change password", title = t("change_password"),
connected = true connected = true
} }
@ -404,7 +417,7 @@ function get_data_for(view)
local mails = get_mails(user) local mails = get_mails(user)
data = { data = {
title = "Edit "..user, title = t("edit").." "..user,
connected = true, connected = true,
uid = user, uid = user,
sn = cache:get(user.."-sn"), sn = cache:get(user.."-sn"),
@ -437,6 +450,16 @@ function get_data_for(view)
end end
end end
-- View translation (use "t_key")
if lang and i18n[lang] then
translate_table = i18n[lang]
else
translate_table = i18n[conf["default_language"]]
end
for k, v in pairs(translate_table) do
data["t_"..k] = v
end
data['flash_fail'] = {flashs["fail"]} data['flash_fail'] = {flashs["fail"]}
data['flash_win'] = {flashs["win"] } data['flash_win'] = {flashs["win"] }
data['flash_info'] = {flashs["info"]} data['flash_info'] = {flashs["info"]}
@ -462,17 +485,17 @@ function do_edit ()
local ldap = lualdap.open_simple(conf["ldap_host"], dn, args.currentpassword) local ldap = lualdap.open_simple(conf["ldap_host"], dn, args.currentpassword)
local password = "{SHA}"..ngx.encode_base64(ngx.sha1_bin(args.newpassword)) local password = "{SHA}"..ngx.encode_base64(ngx.sha1_bin(args.newpassword))
if ldap:modify(dn, {'=', userPassword = password }) then if ldap:modify(dn, {'=', userPassword = password }) then
flash("win", "Password successfully changed") flash("win", t("password_changed"))
cache:set(user.."-password", args.newpassword, conf["session_timeout"]) cache:set(user.."-password", args.newpassword, conf["session_timeout"])
return redirect(portal_url.."info.html") return redirect(portal_url.."info.html")
else else
flash("fail", "An error occured on password changing") flash("fail", t("password_changed_error"))
end end
else else
flash("fail", "New passwords don't match") flash("fail", t("password_not_match"))
end end
else else
flash("fail", "Actual password is wrong") flash("fail", t("wrong_current_password"))
end end
return redirect(portal_url.."password.html") return redirect(portal_url.."password.html")
@ -573,7 +596,7 @@ function do_login ()
end end
else else
ngx.status = ngx.HTTP_UNAUTHORIZED ngx.status = ngx.HTTP_UNAUTHORIZED
flash("fail", "Wrong username/password combination") flash("fail", t("wrong_username_password"))
return redirect(portal_url) return redirect(portal_url)
end end
end end
@ -583,7 +606,7 @@ function do_logout()
if is_logged_in() then if is_logged_in() then
cache:delete("session_"..ngx.var.cookie_SSOwAuthUser) cache:delete("session_"..ngx.var.cookie_SSOwAuthUser)
cache:delete(ngx.var.cookie_SSOwAuthUser.."-"..conf["ldap_identifier"]) -- Ugly trick to reload cache cache:delete(ngx.var.cookie_SSOwAuthUser.."-"..conf["ldap_identifier"]) -- Ugly trick to reload cache
flash("info", "Logged out") flash("info", t("logged_out"))
return redirect(portal_url) return redirect(portal_url)
end end
end end
@ -673,7 +696,7 @@ then
else else
-- Redirect to portal -- Redirect to portal
flash("info", "Please log in to access to this content") flash("info", t("please_login"))
return redirect(portal_url) return redirect(portal_url)
end end
@ -690,7 +713,7 @@ then
end end
else else
-- Redirect to portal -- Redirect to portal
flash("fail", "Please log in from the portal") flash("fail", t("please_login_from_portal"))
return redirect(portal_url) return redirect(portal_url)
end end
end end
@ -846,6 +869,6 @@ end
-- Else redirect to portal -- Else redirect to portal
-- --
flash("info", "Please log in to access to this content") flash("info", t("please_login"))
local back_url = ngx.var.scheme .. "://" .. ngx.var.host .. ngx.var.uri .. uri_args_string() local back_url = ngx.var.scheme .. "://" .. ngx.var.host .. ngx.var.uri .. uri_args_string()
return redirect(portal_url.."?r="..ngx.encode_base64(back_url)) return redirect(portal_url.."?r="..ngx.encode_base64(back_url))

View file

@ -9,11 +9,22 @@ json = require "json"
lualdap = require "lualdap" lualdap = require "lualdap"
math = require "math" math = require "math"
hige = require "hige" hige = require "hige"
lfs = require "lfs"
-- Shared table -- Shared table
flashs = {} flashs = {}
login = {} login = {}
logout = {} logout = {}
i18n = {}
local locale_dir = script_path.."portal/locales/"
for file in lfs.dir(locale_dir) do
if string.sub(file, -4) == "json" then
local lang = string.sub(file, 1, 2)
local locale_file = io.open(locale_dir..file, "r")
i18n[lang] = json.decode(locale_file:read("*all"))
end
end
-- Path of the configuration -- Path of the configuration
conf_path = "/etc/ssowat/conf.json" conf_path = "/etc/ssowat/conf.json"

View file

@ -1,12 +1,12 @@
<form class="form-horizontal" role="form" method="POST" action="edit.html"> <form class="form-horizontal" role="form" method="POST" action="edit.html">
<div class="form-group"> <div class="form-group">
<label for="uid" class="col-sm-3 control-label">Username</label> <label for="uid" class="col-sm-3 control-label">{{t_username}}</label>
<div class="col-sm-9"> <div class="col-sm-9">
<input type="text" name="uid" class="form-control" value="{{uid}}" disabled> <input type="text" name="uid" class="form-control" value="{{uid}}" disabled>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="givenName" class="col-sm-3 control-label">Fullname</label> <label for="givenName" class="col-sm-3 control-label">{{t_fullname}}</label>
<div class="clearfix visible-xs"></div> <div class="clearfix visible-xs"></div>
<div class="col-sm-4 col-xs-6"> <div class="col-sm-4 col-xs-6">
<input type="text" name="givenName" class="form-control" value="{{givenName}}" required> <input type="text" name="givenName" class="form-control" value="{{givenName}}" required>
@ -17,22 +17,22 @@
</div> </div>
<hr> <hr>
<div class="form-group"> <div class="form-group">
<label for="mail" class="col-sm-3 control-label">Mail</label> <label for="mail" class="col-sm-3 control-label">{{t_mail}}</label>
<div class="col-sm-9"> <div class="col-sm-9">
<input type="email" name="mail" class="form-control" value="{{mail}}" required> <input type="email" name="mail" class="form-control" value="{{mail}}" required>
</div> </div>
</div> </div>
<hr> <hr>
<div class="row"> <div class="row">
<div class="col-sm-3 text-right hidden-xs"><strong>Aliases</strong></div> <div class="col-sm-3 text-right hidden-xs"><strong>{{t_aliases}}</strong></div>
<div class="col-sm-3 text-left visible-xs"><h4>Aliases</h4></div> <div class="col-sm-3 text-left visible-xs"><h4>{{t_aliases}}</h4></div>
<div class="col-sm-9"> <div class="col-sm-9">
<blockquote> <blockquote>
{{#mailalias}} {{#mailalias}}
<input type="email" name="mailalias[]" class="form-control" value="{{.}}"> <input type="email" name="mailalias[]" class="form-control" value="{{.}}">
<br> <br>
{{/mailalias}} {{/mailalias}}
<input type="email" name="mailalias[]" class="form-control mailalias-input" placeholder="newalias@mydomain.org"> <input type="email" name="mailalias[]" class="form-control mailalias-input" placeholder="{{t_newalias}}">
<div class="text-center" style="display: none;" id="add-mailalias"><a class="btn btn-success"><strong>+</strong></a></div> <div class="text-center" style="display: none;" id="add-mailalias"><a class="btn btn-success"><strong>+</strong></a></div>
<div class="clearfix"></div> <div class="clearfix"></div>
</blockquote> </blockquote>
@ -40,15 +40,15 @@
</div> </div>
<hr> <hr>
<div class="row"> <div class="row">
<div class="col-sm-3 text-right hidden-xs"><strong>Forward</strong></div> <div class="col-sm-3 text-right hidden-xs"><strong>{{t_forward}}</strong></div>
<div class="col-sm-3 text-left visible-xs"><h4>Forward</h4></div> <div class="col-sm-3 text-left visible-xs"><h4>{{t_forward}}</h4></div>
<div class="col-sm-9"> <div class="col-sm-9">
<blockquote> <blockquote>
{{#maildrop}} {{#maildrop}}
<input type="email" name="maildrop[]" class="form-control" value="{{.}}"> <input type="email" name="maildrop[]" class="form-control" value="{{.}}">
<br> <br>
{{/maildrop}} {{/maildrop}}
<input type="email" name="maildrop[]" class="form-control maildrop-input" placeholder="newforward@myforeigndomain.org"> <input type="email" name="maildrop[]" class="form-control maildrop-input" placeholder="{{t_newforward}}">
<div class="text-center" style="display: none;" id="add-maildrop"><a class="btn btn-success"><strong>+</strong></a></div> <div class="text-center" style="display: none;" id="add-maildrop"><a class="btn btn-success"><strong>+</strong></a></div>
<div class="clearfix"></div> <div class="clearfix"></div>
</blockquote> </blockquote>
@ -57,11 +57,11 @@
<hr> <hr>
<div class="row"> <div class="row">
<div class="col-sm-6 text-center"> <div class="col-sm-6 text-center">
<input type="submit" class="btn btn-lg btn-primary" value="OK"> <input type="submit" class="btn btn-lg btn-primary" value="{{t_ok}}">
</div> </div>
<div class="visible-xs" style="height: 20px"></div> <div class="visible-xs" style="height: 20px"></div>
<div class="col-sm-6 text-center"> <div class="col-sm-6 text-center">
<a href="info.html" class="btn btn-lg btn-default">Cancel</a> <a href="info.html" class="btn btn-lg btn-default">{{t_cancel}}</a>
</div> </div>
</div> </div>
</form> </form>

View file

@ -1,10 +1,9 @@
<!DOCTYPE html>
<html> <html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<head> <head>
<meta charset="utf-8">
<meta name="format-detection" content="telephone=no" /> <meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="user-scalable=no, width=device-width, height=device-height" /> <meta name="viewport" content="user-scalable=no, width=device-width, height=device-height" />
<title>YunoHost Portal</title> <title>{{t_portal}}</title>
<link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css"/> <link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css"/>
<script src="assets/js/jquery-1.10.2.min.js"></script> <script src="assets/js/jquery-1.10.2.min.js"></script>
</head> </head>
@ -19,7 +18,7 @@
<h2>{{{title}}}</h2> <h2>{{{title}}}</h2>
{{#connected}} {{#connected}}
<div class="pull-right" style="margin-top: -31px"> <div class="pull-right" style="margin-top: -31px">
<a href="?action=logout">Logout</a> <a href="?action=logout">{{t_logout}}</a>
</div> </div>
{{/connected}} {{/connected}}
<hr> <hr>

View file

@ -26,12 +26,12 @@
<div class="visible-sm" style="height: 20px"></div> <div class="visible-sm" style="height: 20px"></div>
<div class="col-md-8"> <div class="col-md-8">
<div class="row"> <div class="row">
<div class="col-sm-4 text-right hidden-xs"><strong>Mail</strong></div> <div class="col-sm-4 text-right hidden-xs"><strong>{{t_mail}}</strong></div>
<div class="col-sm-4 visible-xs "><h4>Mail</h4></div> <div class="col-sm-4 visible-xs "><h4>{{t_mail}}</h4></div>
<div class="col-sm-8"><blockquote>{{mail}}</blockquote></div> <div class="col-sm-8"><blockquote>{{mail}}</blockquote></div>
<div class="clearfix"></div><br> <div class="clearfix"></div><br>
<div class="col-sm-4 text-right hidden-xs"><strong>Aliases</strong></div> <div class="col-sm-4 text-right hidden-xs"><strong>{{t_aliases}}</strong></div>
<div class="col-sm-4 visible-xs "><h4>Aliases</h4></div> <div class="col-sm-4 visible-xs "><h4>{{t_aliases}}</h4></div>
<div class="col-sm-8"> <div class="col-sm-8">
<blockquote> <blockquote>
{{#mailalias}} {{#mailalias}}
@ -40,8 +40,8 @@
</blockquote> </blockquote>
</div> </div>
<div class="clearfix"></div><br> <div class="clearfix"></div><br>
<div class="col-sm-4 text-right hidden-xs"><strong>Forward</strong></div> <div class="col-sm-4 text-right hidden-xs"><strong>{{t_forward}}</strong></div>
<div class="col-sm-4 visible-xs "><h4>Forward</h4></div> <div class="col-sm-4 visible-xs "><h4>{{t_forward}}</h4></div>
<div class="col-sm-8"> <div class="col-sm-8">
<blockquote> <blockquote>
{{#maildrop}} {{#maildrop}}
@ -55,10 +55,10 @@
<hr> <hr>
<div class="row"> <div class="row">
<div class="col-sm-6 text-center"> <div class="col-sm-6 text-center">
<a href="password.html" class="btn btn-lg btn-danger">Change password</a> <a href="password.html" class="btn btn-lg btn-danger">{{t_change_password}}</a>
</div> </div>
<div class="visible-xs" style="height: 20px"></div> <div class="visible-xs" style="height: 20px"></div>
<div class="col-sm-6 text-center"> <div class="col-sm-6 text-center">
<a href="edit.html" class="btn btn-lg btn-warning">Edit</a> <a href="edit.html" class="btn btn-lg btn-warning">{{t_edit}}</a>
</div> </div>
</div> </div>

View file

@ -1,11 +1,11 @@
<form name="input" action="" method="post"> <form name="input" action="" method="post">
<div class="form-group"> <div class="form-group">
<label for="user">Username</label> <label for="user">{{t_username}}</label>
<input type="text" name="user" placeholder="john" class="form-control"> <input type="text" name="user" placeholder="john" class="form-control">
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="user">Password</label> <label for="user">{{t_password}}</label>
<input type="password" name="password" placeholder="•••••" class="form-control"> <input type="password" name="password" placeholder="•••••" class="form-control">
</div> </div>
<input type="submit" value="Login" class="btn btn-success"> <input type="submit" value="{{t_login}}" class="btn btn-success">
</form> </form>