Permalink
Browse files

#501 - Wired up ability to add alternate locales for views

  • Loading branch information...
brianhyder committed Oct 26, 2015
1 parent 4d3222e commit e35421075cbc5178c3f8c327268a9394718fbfe2
View
@@ -0,0 +1,12 @@
root = true
[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false
@@ -15,7 +15,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//dependencies
//dependencies
var url = require('url');
var util = require('../include/util.js');
@@ -70,9 +70,9 @@ module.exports = function BaseControllerModule(pb) {
* @type {String}
*/
var ALERT_PATTERN = '<div class="alert %s error_success">%s<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button></div>';
/**
* A mapping that converts the HTTP standard for content-type encoding and
* A mapping that converts the HTTP standard for content-type encoding and
* what the Buffer prototype expects
* @static
* @private
@@ -85,7 +85,7 @@ module.exports = function BaseControllerModule(pb) {
'US-ASCII': 'ascii',
'UTF-16LE': 'utf16le'
});
/**
* Responsible for initializing a controller. Properties from the
* RequestHandler are passed down so that the controller has complete access to
@@ -160,6 +160,34 @@ module.exports = function BaseControllerModule(pb) {
this.ts.registerLocal('site_name', function(flag, cb) {
cb(null, self.siteName);
});
this.ts.registerLocal('localized_alternate', function(flag, cb) {
if (!props.routeLocalized) {
return cb(null, '');
}
var val = '';
pb.Localization.getSupported().forEach(function(locale) {
if (self.ls.language === locale) {
//skip current language. We don't need to list it as an alternate
return;
}
var path = self.req.url;
var urlOpts = {
hostname: self.hostname,
locale: undefined
};
if (self.pathVars.locale && path.indexOf(self.pathVars.locale)) {
path = path.replace(self.pathVars.locale, locale);
}
else {
urlOpts.locale = locale;
}
var url = pb.UrlService.createSystemUrl(path, urlOpts);
val += '<link rel="alternate" hreflang="' + locale + '" href="' + url + '" />\n';
});
cb(null, new pb.TemplateValue(val, false));
});
/**
*
@@ -184,9 +212,9 @@ module.exports = function BaseControllerModule(pb) {
cb();
};
/**
* Retrieves a context object that contains the necessary information for
* Retrieves a context object that contains the necessary information for
* service prototypes
* @method getServiceContext
* @return {Object}
@@ -228,7 +256,7 @@ module.exports = function BaseControllerModule(pb) {
BaseController.prototype.formError = function(message, redirectLocation, cb) {
this.session.error = message;
var uri = pb.UrlService.createSystemUrl(redirectLocation, this.hostname);
var uri = pb.UrlService.createSystemUrl(redirectLocation, { hostname: this.hostname });
cb(pb.RequestHandler.generateRedirect(uri));
};
@@ -280,7 +308,7 @@ module.exports = function BaseControllerModule(pb) {
*/
BaseController.prototype.getPostParams = function(cb) {
var self = this;
this.getPostData(function(err, raw){
//Handle error
if (util.isError(err)) {
@@ -291,7 +319,7 @@ module.exports = function BaseControllerModule(pb) {
//lookup encoding
var encoding = pb.BaseBodyParser.getContentEncoding(self.req);
encoding = ENCODING_MAPPING[encoding] ? ENCODING_MAPPING[encoding] : 'utf8';
//convert to string
var postParams = url.parse('?' + raw.toString(encoding), true).query;
cb(null, postParams);
@@ -305,14 +333,14 @@ module.exports = function BaseControllerModule(pb) {
*/
BaseController.prototype.getJSONPostParams = function(cb) {
var self = this;
this.getPostData(function(err, raw){
//Handle error
if (util.isError(err)) {
pb.log.error("BaseController.getJSONPostParams encountered an error. ERROR[%s]", err.stack);
return cb(err, null);
}
//lookup encoding
var encoding = pb.BaseBodyParser.getContentEncoding(self.req);
encoding = ENCODING_MAPPING[encoding] ? ENCODING_MAPPING[encoding] : 'utf8';
@@ -338,11 +366,11 @@ module.exports = function BaseControllerModule(pb) {
BaseController.prototype.getPostData = function(cb) {
var buffers = [];
var totalLength = 0;
this.req.on('data', function (data) {
buffers.push(data);
totalLength += data.length;
// 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
if (totalLength > 1e6) {
// FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST
@@ -352,7 +380,7 @@ module.exports = function BaseControllerModule(pb) {
}
});
this.req.on('end', function () {
//create one big buffer.
var body = Buffer.concat (buffers, totalLength);
cb(null, body);
@@ -134,7 +134,7 @@ module.exports = function(pb) {
/**
*
* @method
* @method
* @param {Error} [err]
* @param {String} message
* @param {Function} cb
@@ -151,7 +151,7 @@ module.exports = function(pb) {
* @method getFormErrorRedirect
* @param {Error} err
* @param {String} message
* @return
* @return
*/
DeleteController.prototype.getFormErrorRedirect = function(/*err, message*/) {
return '/';
@@ -172,7 +172,7 @@ module.exports = function(pb) {
* @return {String}
*/
DeleteController.prototype.getSuccessRedirect = function() {
return pb.UrlService.createSystemUrl('/', this.hostname);
return pb.UrlService.createSystemUrl('/', { hostname: this.hostname });
};
/**
@@ -193,6 +193,6 @@ module.exports = function(pb) {
DeleteController.prototype.getDefaultErrorMessage = function() {
return this.ls.get('ERROR_SAVING');
};
return DeleteController;
};
Oops, something went wrong.

0 comments on commit e354210

Please sign in to comment.