implement /api/translate route
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const { restart } = require('nodemon');
|
||||||
const Translator = require('../components/translator.js');
|
const Translator = require('../components/translator.js');
|
||||||
|
|
||||||
module.exports = function (app) {
|
module.exports = function (app) {
|
||||||
@@ -8,6 +9,49 @@ module.exports = function (app) {
|
|||||||
|
|
||||||
app.route('/api/translate').post((req, res) => {
|
app.route('/api/translate').post((req, res) => {
|
||||||
|
|
||||||
|
const text = req.body.text;
|
||||||
|
const locale = req.body.locale;
|
||||||
|
|
||||||
|
if (text == undefined || locale == undefined) return res.status(200).json({error: 'Required field(s) missing'});
|
||||||
|
|
||||||
|
if (!text) return res.status(200).json({error: 'No text to translate'});
|
||||||
|
|
||||||
|
const validLocales = ['american-to-british', 'british-to-american'];
|
||||||
|
|
||||||
|
if (!validLocales.includes(locale)) return res.status(200).json({error: 'Invalid value for locale field'});
|
||||||
|
|
||||||
|
if (locale == 'american-to-british') {
|
||||||
|
|
||||||
|
const translation = translator.translateAmerican(text);
|
||||||
|
|
||||||
|
if (text == translation) return res.status(200).json({
|
||||||
|
text: text,
|
||||||
|
translation: 'Everything looks good to me!'
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
text: text,
|
||||||
|
translation: translation
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (locale == 'british-to-american') {
|
||||||
|
|
||||||
|
const translation = translator.translateBritish(text);
|
||||||
|
|
||||||
|
if (text == translation) return res.status(200).json({
|
||||||
|
text: text,
|
||||||
|
translation: 'Everything looks good to me!'
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
text: text,
|
||||||
|
translation: translation
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -206,16 +206,6 @@ suite('Unit Tests', () => {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Translate { Tea time is usually around 4 or 4.30. } to American English', done => {
|
|
||||||
|
|
||||||
const string = 'Tea time is usually around 4 or 4.30.';
|
|
||||||
const result = translator.translateBritish(string);
|
|
||||||
|
|
||||||
assert.equal(result, 'Tea time is usually around 4 or <span class="highlight">4:30</span>.');
|
|
||||||
done();
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Highlight differences { Mangoes are my favorite fruit. } when translated to British English', done => {
|
test('Highlight differences { Mangoes are my favorite fruit. } when translated to British English', done => {
|
||||||
|
|
||||||
const string = 'Mangoes are my favorite fruit.';
|
const string = 'Mangoes are my favorite fruit.';
|
||||||
|
|||||||
Reference in New Issue
Block a user