diff --git a/api/routes.js b/api/routes.js index 6d3c6bf8..ce64b945 100644 --- a/api/routes.js +++ b/api/routes.js @@ -20,4 +20,10 @@ router.get('/search/:term', (req, res, next) => { .catch(err => res.status(404).send({ err: true, debug: err })); }); +router.get('/:name/:language', (req, res, next) => { + utils.getTranslation(req.params.name, req.params.language) + .then(results => res.status(200).send(results).end()) + .catch(err => res.status(404).send({ err: true, debug: err })); +}); + module.exports = router; diff --git a/api/utils.js b/api/utils.js index 081bae89..09b2b8b8 100644 --- a/api/utils.js +++ b/api/utils.js @@ -43,6 +43,17 @@ module.exports.searchTerm = (term) => { }); } +module.exports.getTranslation = (name, language) => { + return new Promise((resolve, reject) => { + db.query(`SELECT value FROM translations WHERE name = ${db.escape(name)} AND language = ${db.escape(language)}`, (err, value, fields) => { + if (err) return reject(err); + if (!value || !value.length) return reject(`Could not find '${language}' translation for '${name}' in the database`); + + return resolve(value[0]); + }); + }); +} + module.exports.generatePublicObject = (name) => { return new Promise((resolve, reject) => { db.query(`SELECT * FROM meanings WHERE name = ${db.escape(name)}`, (err, meaning, fields) => {