Skip to content

Instantly share code, notes, and snippets.

@albertosouza
Last active February 8, 2018 04:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save albertosouza/6b590a2f544a2a5077ed to your computer and use it in GitHub Desktop.
Save albertosouza/6b590a2f544a2a5077ed to your computer and use it in GitHub Desktop.
Sails.js simple example of localization with database
module.exports = {
adapter:'exampleDB',
//tableName:'locale',
attributes: {
text: {
type: 'string'
},
// locale config
locale: {
type: 'string',
// dafault locale
defaultsTo: 'us-en'
}
// others atribs here ...
}
};
exports.i18n = function LSi18n(text, locale, callback){
if(!locale){
locale = 'en-us';
}
// get string from db
Locale.findOne({
text: text,
locale: locale
}).exec(function(err, locale){
if(err){
sails.log.error(err);
return callback(err, text);
}
if(locale){
return callback(err, locale);
}else{
// if dont are translated return the default text
return callback(err, text);
}
});
};
// ...
exampleAction: function (req,res){
var content = LocaleService.i18n('Hello Word', req.lang, function(err, translatedText){
res.view({
title:translatedText
});
});
}
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment