Skip to content

Commit

Permalink
refactor: Clean up locale fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
arildm committed May 28, 2024
1 parent 7f263d3 commit 5515f80
Showing 1 changed file with 13 additions and 27 deletions.
40 changes: 13 additions & 27 deletions app/scripts/data_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,27 @@ import { httpConfAddMethodFetch } from "@/util"

// TODO it would be better only to load additional languages when there is a language change
async function initLocales() {
const packages = ["locale", "corpora"]
const prefix = "translations"
const locData = {}

const defs = []
for (let langObj of settings["languages"]) {
for (const langObj of settings["languages"]) {
const lang = langObj.value
locData[lang] = {}
for (let pkg of packages) {
let file = pkg + "-" + lang + ".json"
file = prefix + "/" + file
const def = new Promise((resolve) => {
fetch(file)
.then((response) => {
if (response.status >= 300) {
throw new Error()
}
response.json().then((data) => {
_.extend(locData[lang], data)
})
resolve()
})
.catch(() => {
resolve()
console.log("No language file: ", file)
})
})
for (const pkg of ["locale", "corpora"]) {
const file = `translations/${pkg}-${lang}.json`
const def = fetch(file)
.then(async (response) => {
if (response.status >= 300) throw new Error()
const data = await response.json()
Object.assign(locData[lang], data)
})
.catch(() => {
console.log("No language file: ", file)
})
defs.push(def)
}
}

for (const def of defs) {
await def
}

await Promise.all(defs)
window.loc_data = locData
return locData
}
Expand Down

0 comments on commit 5515f80

Please sign in to comment.