NUKnightLab / TimelineJS3

TimelineJS v3: A Storytelling Timeline built in JavaScript. http://timeline.knightlab.com
Mozilla Public License 2.0
2.99k stars 621 forks source link

Allow to use dynamically created translations #836

Open otacke opened 1 year ago

otacke commented 1 year ago

Currently, TimelineJS expects the language property to either

It is not possible, however, to use dynamically created language overrides, which would commonly be passed as a JSON object following the same structure as the translation JSON files.

A simple change to https://github.com/NUKnightLab/TimelineJS3/blob/master/src/js/language/Language.js#L242-L251 would allow this. If, for instance, the initial check at https://github.com/NUKnightLab/TimelineJS3/blob/master/src/js/language/Language.js#L243 was turned into

if (
  /\.json$/.test(code) ||
  /^blob:/.test(code) // dynamically created file (Data URL)
) {

then in particular 3rd party solutions that use TimelineJS as module could simply construct their own translation JSON object dynamically (let us call it foo) and pass it to TimelineJS as a Data URL, e.g. using

URL.createObjectURL(new Blob([JSON.stringify(foo)])) // in proper `try/catch`-clause, sure

If that's something that feels worth adding to TimelineJS (implementation can vary, of course), please let me know.