Search code examples
jsonwebpackinternationalizationrollupsvelte

Can I generate i18n JSON files for Svelte app using Rollup?


I have a Svelte Rollup JS app. I have implemented my own i18n. I have en-us/[function].json and de-de/[function].json. I fetch the JSON at runtime and display the text.

en-us/login.json

{
  "username": {
    "label": "Username"
  }
}

de-de/login.json

{
  "username": {
    "label": "Nutzername"
  }
}

Functions like login and sign up have common texts. In the above example, the username field is used in both the screens/functions. If I have to make a change to the text, I will have to change all the [function].json files in all language folders. In this case, if I change the username.label, I will have to change in 4 files en-us/login.json, en-us/signup.json, de-de/login.json, de-de/signup.json.

Is there a better way to handle it? Can I generate these JSON files using Rollup?


Solution

  • You could use a javascript gettext port like guillaumepotier/gettext.js, or Jed. The former suggests to use xgettext-php together with Poedit to extract/edit the PO files. Probably you could use also jspot for the extraction.

    See also po2json.

    Here it is an article explaining the process with Jed and jspot.