framework
framework copied to clipboard
Multi-lanuage routes
How to implement multi-lanuage routes?
I want to use the accept-language header to get the browser language, path url or cookie and then redirect automatically to the corresponding language route like: site.com/en, site.com/cz/show/1, site.com/en/controller/action?page=1&sort=_date etc.
Any advice on this? Thanks
Hi @veslo1, good choice is translate "views" only.
For example:
exports.install = function(framework) {
framework.route('/{language}/', view_homepage);
framework.route('/{language}/products/', view_products);
};
function view_homepage(language) {
language = language || 'en';
this.view('homepage-' + language);
};
// ....
// ....
OR
exports.install = function(framework) {
framework.route('/', view_homepage);
};
exports.onRequest = function(controller) {
// this code affect only this controller
var language = controller.req.cookie('language') || '';
if (language === '')
language = controller.req.headers['accept-language'];
controller.language = language.indexOf('cz') !== -1 ? 'cz' : language.indexOf('sk') !== -1 ? 'sk' : 'en';
};
function view_homepage() {
var self = this;
// self.language === {String}
self.view('homepage-' + self.language);
};
OR
yourwebsite/modules/#.js
exports.onLoaded = function(framework) {
// this code affect all controllers
framework.on('controller', function(controller, name) {
var language = controller.req.cookie('language') || '';
if (language === '')
language = controller.req.headers['accept-language'];
controller.language = language.indexOf('cz') !== -1 ? 'cz' : language.indexOf('sk') !== -1 ? 'sk' : 'en';
});
};
yourwebsite/modules/controller.js
exports.install = function(framework) {
framework.route('/', view_homepage);
};
function view_homepage() {
var self = this;
// self.language === {String}
self.view('homepage-' + self.language);
};
https://github.com/petersirka/partial.js/tree/master/examples/localization-resources