2020-05-23 13:19:31 +09:00
|
|
|
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;
|
2020-05-23 23:21:09 +09:00
|
|
|
i18n.setLocaleMessage(lang, Object.fromEntries(flatLocaleEntries));
|
2020-05-23 13:19:31 +09:00
|
|
|
});
|
|
|
|
}
|