Dynamic Localization with NextJS

Viewed 41

I have a language api written in .net core. Should I call the data from the api as a variable in the code? How can I manage a localization where the data comes from the database? With the i18n package, you can directly access the language options given in the nextjs project. My data comes through an api. What is the most accurate way to manage localization?

As follow:

enter image description here

1 Answers

What you can do:

  1. You return the translation code to your page. (from your API). Example: {"statusCode": 111}

  2. You fill all translations for all status codes with i18n. Example: {"translationStatusCode_111": "bla bla bla", "translationStatusCode_112": "tra la la"}

  3. In your page, you are showing variable as {t(`translationStatusCode_${returned_status_code}`)}

    where returned_status_code variable is your statusCode of translation returned from your API.

I wrote this example for you from this library, as you are using nextjs.

Related