Skip to content

Commit

Permalink
citeproc-rs conversion: locale
Browse files Browse the repository at this point in the history
  • Loading branch information
tnajdek committed Jun 8, 2021
1 parent 032bbf5 commit eda139d
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/js/utils.jsx
Expand Up @@ -20,16 +20,24 @@ const stylesCache = {};
// debugger;

class Fetcher {
async fetchLocale(lang) {
// return await fetch("https://some-cdn-with-locales.com/locales-${lang}.xml")
// .then(res => res.text());

// or just
// return "<locale> ... </locale>";
// return LOCALES_PRELOADED[lang];
async fetchLocale(lang) {
const cacheId = `zotero-style-locales-${lang}`;
var locales = localStorage.getItem(cacheId);

// fix in place for scenarios where potentially bad locales have been cached
// see issue #236
if(typeof locales === 'string' && !locales.startsWith('<?xml')) {
locales = false;
}

// or if you don't support locales other than the bundled en-US!
return null;
if(locales) {
return locales;
} else {
const response = await fetch(`/static/locales/locales-${lang}.xml`);
const locales = response.text();
localStorage.setItem(cacheId, locales);
return locales;
}
}
}

Expand Down Expand Up @@ -65,8 +73,7 @@ const getCiteproc = async (citationStyle) => {
try {
const { default: init, Driver } = await import("/static/js/citeproc-rs/wasm.js");
await init();
// const driverResult = Driver.new({ localeOverride: lang, style, fetcher });
const driverResult = Driver.new({ style, fetcher });
const driverResult = Driver.new({ localeOverride: lang, style, fetcher });
const driver = driverResult.unwrap();
await driver.fetchLocales();
console.log({ driver, style });
Expand Down

0 comments on commit eda139d

Please sign in to comment.