Last active
February 8, 2018 04:22
-
-
Save albertosouza/6b590a2f544a2a5077ed to your computer and use it in GitHub Desktop.
Sails.js simple example of localization with database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
adapter:'exampleDB', | |
//tableName:'locale', | |
attributes: { | |
text: { | |
type: 'string' | |
}, | |
// locale config | |
locale: { | |
type: 'string', | |
// dafault locale | |
defaultsTo: 'us-en' | |
} | |
// others atribs here ... | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
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