Import Modules Dynamically(import())

During web development bootcamp after Completion of useContext hook , students were asked to implement different languages in the web app so that is were we got to know that dev's will maintian different languge script files in accordance to English languge, so that means for every language we will keep one separate large translated js file

Say our app is giving 10 languages flexbility so we will keep 10 separate files which consist of each and every word in its native language with respect to the english

// spanish-translation.js

export translations={
helloWorld:" word in spanish"
// and like this it will consist entire application words in its language

}
// Hindi-translation.js

export translations={
helloWorld:" word in hindi"
}

// and like this it will consist entire application words in its language

now user may use specific language on their own, say user is on home route he/she wants to toggle to hindi languge so to show in hindi usually we should import that hindi langugage file into the home route and we should use that hindi langugae file to show hindi words and if user wants to toggle to spanish agian we have import spanish file and we have that file to show words in spanish

what usually we do is static Imports of translated langugaes

// home route.js
import {hindiTranslation} from 'hindiTranslation.js'
import {spanishTranslation} from 'hindiTranslation.js'
import {teluguTranslation} from 'hindiTranslation.js'
import {tamilTranslation} from 'hindiTranslation.js'
import {kanndaTranslation} from 'hindiTranslation.js'

whenever homeroute.js file executed if we do static imports browser will immediately downloads all the langugaes files which will take more time which leads to the slower loading of page

say user only used hindiLangugae then remaining languages files is useless so if we download user specific file only instead of downloading all it will improves loading time hence user experience is also affected accordingly

so we have to import files dynamically for that we have import()

import()

import() will return us the promise so we can chain on promise and we can execute the functionality accordingly ex

const userRequireLanguage=hindi

import(`./${userRequireLanguage}Translation`)
.catch(()=>{
   import("./englishTranslation")
})
.then((module)=>{
const {translations}=module
console.log(translations.helloWorld)
})

in the above code if userRequireLanguage rejected the we import default english language we use that if not we downloads the required language so this how we improve the performance of web app to some extent.