You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
999 B
TypeScript
37 lines
999 B
TypeScript
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
|
import { Dictionary, en } from '../../localization/locales';
|
|
import { loadDictionary, Locale } from '../../util/i18n';
|
|
|
|
export type DictionaryState = {
|
|
dictionary: Dictionary;
|
|
locale: string;
|
|
};
|
|
|
|
export const initialDictionaryState = {
|
|
dictionary: en,
|
|
locale: 'en',
|
|
};
|
|
|
|
const dictionarySlice = createSlice({
|
|
name: 'dictionary',
|
|
initialState: initialDictionaryState,
|
|
reducers: {
|
|
updateLocale(state: DictionaryState, action: PayloadAction<Locale>) {
|
|
// eslint-disable-next-line more/no-then
|
|
loadDictionary(action.payload)
|
|
.then(dictionary => {
|
|
state.dictionary = dictionary;
|
|
state.locale = action.payload;
|
|
})
|
|
.catch(e => {
|
|
window.log.error('Failed to load dictionary', e);
|
|
});
|
|
},
|
|
},
|
|
});
|
|
|
|
// destructures
|
|
const { actions, reducer } = dictionarySlice;
|
|
export const { updateLocale } = actions;
|
|
export const defaultDictionaryReducer = reducer;
|