Mon Aug 06 2018
Copied to clipboard! Copy reply
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
const translate = require('@google-cloud/translate')();


exports.translate = (req, res) => {

    const from = req.query.from
    const to = req.query.to
    const text = req.query.text

    if (!from || !to || !text)
    {
        return res.status(200).send('')
    }

    const options = {from: from, to: to }

    

    translate.translate(text, options, (e, translation) => {
        if (!e)
        {
            const translated = translation
            res.status(200).send(translated)            
        }
        else
        {   
            if (req.method == 'OPTIONS')
            {
                res.status(204).send('');
            }
        
            res.status(200)
                .header('Access-Control-Allow-Origin', '*')
                .header('Access-Control-Allow-Headers', 'Content-Type')
                .send(translated);
        }
        
    })

    

    /*
    if (!req.body.from || !req.body.to || !req.body.text)
    {
        return
    }

    // TODO if 'to' or 'from' are not in a valid list of arrays, return
    // ie, user cant translated to 'es33'

    const options = {
        from: req.body.from,
        to: req.body.to
    }

    const text = req.body.text
    
    
    translate.translate(text, options, (e, translation) => {
        if (!e)
        {
            const translated = translation
            res.status(200).send(translated)            
        }
        else
        {   
            if (req.method == 'OPTIONS')
            {
                res.status(204).send('');
            }
        
            res.status(200)
                .header('Access-Control-Allow-Origin', '*')
                .header('Access-Control-Allow-Headers', 'Content-Type')
                .send(translated);
        }
        
    })
    */

}