0

I'm totally new to Angular JS and Node JS. I have several JSON files (Language translations) in server side. According to client request I need to retrieve data of JSON file. If user request french language , I need to retrieve data of that particular JSON file. This is what I tried based on search. It's not working. Can someone help me with proper method?

function getTranslations (
  request /* : Request<null, {
    nextToken?: string
  }, {}> */,
  response /* : Object */
)  {
  request.headers = request.headers || {}
  return authentication.authenticate(request.headers.authorization)
    .then((tokenPayload) => authorisation.authorise(tokenPayload.username, permissions.TRANSLATE))
    .then((authorised) => {
      if (!authorised) {
        return Promise.reject(boom.forbidden('Message'))
      }

    const options = {  
        url: 'files/translations/',
        method: 'GET',
        headers: {
            'Accept': 'application/json',
            'Accept-Charset': 'utf-8',
        }
    };

    app.get("/:getValue", function(req, res)  { 
    request(options, function(err, response, body) {  
        var json = JSON.parse(body);
        console.log(json); 
        res.json(request.json) 
    });    
    });

    app.listen(3000, function() {  
        console.log("My API is running...");
    });

    })
}

1 Answer 1

1

Well, there are many ways to go about it. I'll make some assumptions regarding your question it wasn't clear. First, I assume, there are many json files like en.json for english, fr.json for french etc.

Now, if a user requests, "english", he should get the data of en.json. And same way for fr.json.

What you can do is create a file called translation.js with the following:

module.exports = {
    ENGLISH: require('./en.json'),
    FRENCH: require('./fr.json')
};

Then in your router you can send the data based on the parameter passed:

//app.js
var translation = require('./translation.js');

app.get(/:value, (req, res) => {
    if (req.params.value === 'french') {
        return res.status(200).json(translation.FRENCH);
    }

    return res.status(200).json(translation.ENGLISH);
});

Or you can use a package for i18n - https://github.com/mashpie/i18n-node

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for the response. My route has defined in a seperate file. I cannot use app.get here.Then how do I modify this?
You can require that file anywhere and use it as per your case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.