jackocnr / intl-tel-input

A JavaScript plugin for entering and validating international telephone numbers. React and Vue components also included.
https://intl-tel-input.com
MIT License
7.66k stars 1.95k forks source link

JSON import for localization #1544

Closed julianwitzel closed 8 months ago

julianwitzel commented 8 months ago

I was wondering if it is somehow possible to load the translations from an external source. That way the code stays more clean and the translation files can be used for multiple projects.

The code I tried did not work. Is there any way this is possible already?

i18n: fetch(langFilePath)
    .then(response => response.json())
    .then(data => {
    return data;
  })
  .catch(error => {
    console.error('Error loading JSON file:', error);
}),
jackocnr commented 8 months ago

Fetch returns a promise, so you are effectively initialising the plugin with the i18n option set to a promise which won't work. Instead you need to finish loading your JSON before you initialise the plugin, and then pass the result as the value to i18n.