Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quantum-kittens
GitHub Repository: quantum-kittens/platypus
Path: blob/main/frontend/ts/translations.ts
3855 views
1
const fetchTranslations = function (locale: string): Promise<{[x:string]: string}> {
2
return fetch(`/locales/${locale}`)
3
.then(res => res?.json ? res.json() : {})
4
}
5
6
const loadTranslations = function () {
7
const elt = document.getElementById('translations')
8
return elt?.textContent ? JSON.parse(elt.textContent) : {}
9
}
10
11
const translate = function (str: string, args: string[] = [], translations?: any): string {
12
const translator = translations || window.textbook.translations
13
let translated = translator?.[str] || str
14
15
args.forEach((newValue, index) => {
16
translated = translated.replace('$' + index, newValue)
17
})
18
19
return translated
20
}
21
22
export {
23
fetchTranslations,
24
loadTranslations,
25
translate
26
}
27
28