Reputation: 17
I've created a simple dynamic content app with firebase following this tutorial: https://firebase.google.com/docs/hosting/functions e.g.:
const functions = require('firebase-functions');
exports.bigben = functions.https.onRequest((req, res) => {
const hours = (new Date().getHours() % 12) + 1 // London is UTC + 1hr;
res.status(200).send(`<!doctype html>
<head>
<title>Time</title>
</head>
<body>
${'BONG '.repeat(hours)}
</body>
</html>`);
});
My question is how do I apply i18n to this dynamic content, so I can return a different html response depending on the locale of the request. I read the i18n documentation in firebase but it only suggests that it is possible with static content using hosting: https://firebase.google.com/docs/hosting/i18n-rewrites
I need it to be done inside the cloud functions, so I believe I would need a way to get the locale information inside the function itself, does anyone have any idea on how to do it or any different approaches for this?
Upvotes: 0
Views: 1367
Reputation: 317412
Cloud Functions doesn't know anything about the end user, other than what you provide to it. So, you'll either have to pass the locale as input to this function, or read it from some other data source, such as a database.
If you want the user's browser configuration for locale in this function, you can use the information in this question to pull the locale out of an HTTP header sent by the browser:
Upvotes: 2