summaryrefslogtreecommitdiff
path: root/src/client/scripts/set-i18n-contexts.ts
blob: 60149573615c49a5f27e5a66b2ce1161c114765d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { clientDb, clear, bulkSet } from '../db';
import { deepEntries, delimitEntry } from 'deep-entries';

export function setI18nContexts(lang: string, version: string, cleardb = false) {
	return Promise.all([
		cleardb ? clear(clientDb.i18n) : Promise.resolve(),
		fetch(`/assets/locales/${lang}.${version}.json`)
	])
	.then(([, response]) => response.json())
	.then(locale => {
		const flatLocaleEntries = deepEntries(locale, delimitEntry) as [string, string][];
		bulkSet(flatLocaleEntries, clientDb.i18n);
		return Object.fromEntries(flatLocaleEntries);
	});
}