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

export function setI18nContexts(lang: string, version: string, i18n: VueI18n, 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);
		i18n.locale = lang;
		i18n.setLocaleMessage(lang, Object.fromEntries(flatLocaleEntries));
	});
}