0

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?

  • I don't understand where you need i18n in your example but may be you can create a document in firestore with different language and get your string but you have to read this document at each request. – ande Nov 25 '20 at 20:39
  • in the example there is no need, it was just an example of dynamic content with firebase. In my case imagine that html being returned could have been in other languages depending where the user that requested it was requesting from – user3107720 Nov 25 '20 at 22:18
  • "*I believe I would need a way to get the locale information inside the function itself*" - what locale information? The end user's locale, as defined by the browser they're using? – Doug Stevenson Nov 26 '20 at 07:30
  • @user3107720. I think firestore document is the only way to have different sentences in different language. But may be it's better to not use functions and create your own API with firebase hosting. – ande Nov 26 '20 at 12:02
  • yes @DougStevenson, that would be perfect if possible inside the cloud function – user3107720 Nov 26 '20 at 21:57
  • @ande I'm not sure how to do the firebase hosting i18n example with dynamic content, content generate by the server an not in the html, any idea? – user3107720 Nov 26 '20 at 21:59

1 Answers1

2

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:

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • So if you do SSR via a Cloud Function, you don't have to configure anything in 118n hosting (like https://firebase.google.com/docs/hosting/i18n-rewrites), right ? If true, when catching the locale you have to use a redirect in the response ? – Johan Chouquet Mar 23 '21 at 15:36