Loading language when datatables is in a module
Loading language when datatables is in a module
tacman1123
Posts: 198Questions: 46Answers: 1
I'm using datatables inside an ES6 module (I think that's the right terminology. It's actually a stimulus-js controller).
This code seems like it can be improved (and it's much longer, as it has all the datatables language packs).
import enLanguage from 'datatables.net-plugins/i18n/en-GB.mjs'
import esLanguage from 'datatables.net-plugins/i18n/es-ES.mjs';
import deLanguage from 'datatables.net-plugins/i18n/de-DE.mjs';
let language = enLanguage;
if(this.locale === 'es') {
language = esLanguage;
}else if(this.locale === 'de') {
language = deLanguage;
I guess what I want is some sort of way to select the locale, maybe an all-inclusive language plugin that does all this so I don't have to import them individually?
This question has an accepted answers - jump to answer
Answers
At the moment, no there isn't such an option, but I like that as an idea! Thanks for the suggestion. I've added it to my todo list. It would basically be what you have there, but wrapped in an NPM package.
Dynamic imports is something that is possible as of ECMAScript 2020 and modern browsers do support it, but I haven't seen it used much. It also depends on your bundling approach - you might need all the languages loaded and make the decision on the client-side (which could result in a large download to the end user, depending on how manage languages you have!).
Allan
Thanks! Gracias! Danke!
Dynamic imports look cool, but I haven't been able to figure them out at all. We're completely dependent on importmap now, and we heavily cache these files, so loading all of them seems tolerable.