loc
Version:
lightweight simple translation module with dynamic json storage
34 lines (30 loc) • 1.04 kB
JavaScript
/**
* Stragety = cookie
* gets and stores the locale to the response cookies.
*/
exports.name = 'cookie';
var fieldname = exports.fieldname = 'lang';
/**
* gets the locale from the given strategy
*
* @param {Object} req connect / express request object
* @return {String|false} Locale if the locale was found with the given strategy, otherwise false.
*/
exports.getLocaleFrom = function(req) {
if (!req || !req.cookies || !req.cookies[fieldname]) return false;
var locale = req.cookies[fieldname];
return locale;
}
/**
* Stores the locale to the given strategy.
* Note: not all strategies have to implement this. Most likely this is suitable for cookie or session strategy.
*
* @param {Object} req connect / express request object
* @param {String} locale the locale like `en` or `de-CH`
* @return {Boolean} true if stored sucessfully, otherwise false
*/
exports.storeLocaleTo = function(req, res, locale) {
if (!req || !res || !locale) return false;
res.cookie(fieldname, locale);
return locale;
}