I want to load some javascript data dependent on the user’s language (not translation files). In PHP I would just go if ($lang == 'de') { include 'data-de.php'; } else { include 'data-en.php'; }
, or something to that effect.
I understand it doesn’t work like that in JS, and I’ve looked into dynamic imports. This is what I have after determining the language to use (the value of lang):
// in data-{lang}.js
export const points = [{"id":"Q230416", …}, {…}, …];
// in map.js
import('./data-'+lang+'.js')
.then((data) => {
const points = data.points;
console.log(data.points);
})
.catch((error) => {
console.error(error);
});
This gives me all my points in the log alright. However, how can I use them elsewhere? (The way I went about this is I wrote all the functionality with a fixed data set, and now I am retrofitting different data sets dependent on language – maybe that is not the best way to do it.)
I am pretty sure I’m missing something very basic here, like I haven’t understood all the relevant concepts yet – but I just can’t figure out what exactly I need to dive deeper into. Any pointers are much appreciated!