I would like to programmatically simplify the following named imports and assignments in order to avoid repetition and was wondering if there is a way to do this with the help of a loop.
What I have at the moment:
import { globalLocale } from './i18n'
let locale, dateLocale
if (globalLocale === 'de-DE') {
const { dateDeDE, deDE } = await import('node_module')
locale = deDE
dateLocale = dateDeDE
} else if (globalLocale === 'fr-FR') {
const { dateFrFR, frFR } = await import('node_module')
locale = frFR
dateLocale = dateFrFR
} else {
const { dateEnUS, enUS } = await import('node_module')
locale = enUS
dateLocale = dateEnUS
}
Is it possible to summarize the above with the help of a loop like this:
import { globalLocale } from './i18n'
let locale, dateLocale
const locales = {
'de-DE': ['deDE', 'dateDeDE']
'fr-FR': ['frFR', 'dateFrFR']
'en-US': ['enUS', 'dateEnUS']
}
for (const lang in locales) {
if (Object.prototype.hasOwnProperty.call(locales, lang)) {
if (globalLocale === lang) {
const { [locales[lang][0]]: currentLocale, [locales[lang][1]]: currentDateLocale } = await import('node_module')
locale = currentLocale
dateLocale = currentDateLocale
}
}
}