简体中文、英文、多语言框架、时区、夏令时、公制/英制和区域配置
This commit is contained in:
@@ -4,11 +4,13 @@ import { useDispatch } from 'react-redux';
|
||||
// 👇 保留 import,但是把类型定义放到 store 后面
|
||||
import userReducer from './userSlice';
|
||||
import stationReducer from './stationSlice'; // 👈 加这个
|
||||
import preferencesReducer from './preferencesSlice';
|
||||
|
||||
export const store = configureStore({
|
||||
reducer: {
|
||||
user: userReducer,
|
||||
station: stationReducer, // 👈 加这个
|
||||
preferences: preferencesReducer,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
111
src/store/preferencesSlice.ts
Normal file
111
src/store/preferencesSlice.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
||||
|
||||
export type Locale = 'zh' | 'en';
|
||||
export type UnitSystem = 'metric' | 'imperial';
|
||||
export type DateFormat = 'YYYY-MM-DD HH:mm:ss' | 'YYYY-MM-DD HH:mm' | 'MM/DD/YYYY HH:mm:ss';
|
||||
|
||||
export interface PreferencesState {
|
||||
locale: Locale;
|
||||
timezone: string;
|
||||
unitSystem: UnitSystem;
|
||||
dateFormat: DateFormat;
|
||||
}
|
||||
|
||||
const STORAGE_KEY = 'preferences';
|
||||
|
||||
const detectBrowserTimezone = (): string => {
|
||||
try {
|
||||
return Intl.DateTimeFormat().resolvedOptions().timeZone || 'Asia/Shanghai';
|
||||
} catch {
|
||||
return 'Asia/Shanghai';
|
||||
}
|
||||
};
|
||||
|
||||
const detectLocale = (): Locale => {
|
||||
const stored = localStorage.getItem('i18nextLng');
|
||||
if (stored?.startsWith('en')) return 'en';
|
||||
return 'zh';
|
||||
};
|
||||
|
||||
const loadFromStorage = (): PreferencesState => {
|
||||
try {
|
||||
const raw = localStorage.getItem(STORAGE_KEY);
|
||||
if (raw) {
|
||||
const parsed = JSON.parse(raw) as Partial<PreferencesState>;
|
||||
return {
|
||||
locale: parsed.locale === 'en' ? 'en' : 'zh',
|
||||
timezone: parsed.timezone || detectBrowserTimezone(),
|
||||
unitSystem: parsed.unitSystem === 'imperial' ? 'imperial' : 'metric',
|
||||
dateFormat: (parsed.dateFormat as DateFormat) || 'YYYY-MM-DD HH:mm:ss',
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
return {
|
||||
locale: detectLocale(),
|
||||
timezone: detectBrowserTimezone(),
|
||||
unitSystem: 'metric',
|
||||
dateFormat: 'YYYY-MM-DD HH:mm:ss',
|
||||
};
|
||||
};
|
||||
|
||||
const persist = (state: PreferencesState) => {
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
};
|
||||
|
||||
const initialState: PreferencesState = loadFromStorage();
|
||||
persist(initialState);
|
||||
|
||||
const preferencesSlice = createSlice({
|
||||
name: 'preferences',
|
||||
initialState,
|
||||
reducers: {
|
||||
setLocale: (state, action: PayloadAction<Locale>) => {
|
||||
state.locale = action.payload;
|
||||
persist(state);
|
||||
},
|
||||
setTimezone: (state, action: PayloadAction<string>) => {
|
||||
state.timezone = action.payload;
|
||||
persist(state);
|
||||
},
|
||||
setUnitSystem: (state, action: PayloadAction<UnitSystem>) => {
|
||||
state.unitSystem = action.payload;
|
||||
persist(state);
|
||||
},
|
||||
setDateFormat: (state, action: PayloadAction<DateFormat>) => {
|
||||
state.dateFormat = action.payload;
|
||||
persist(state);
|
||||
},
|
||||
setPreferences: (state, action: PayloadAction<Partial<PreferencesState>>) => {
|
||||
const { locale, timezone, unitSystem, dateFormat } = action.payload;
|
||||
if (locale) state.locale = locale;
|
||||
if (timezone) state.timezone = timezone;
|
||||
if (unitSystem) state.unitSystem = unitSystem;
|
||||
if (dateFormat) state.dateFormat = dateFormat;
|
||||
persist(state);
|
||||
},
|
||||
resetPreferences: (state) => {
|
||||
state.locale = detectLocale();
|
||||
state.timezone = detectBrowserTimezone();
|
||||
state.unitSystem = 'metric';
|
||||
state.dateFormat = 'YYYY-MM-DD HH:mm:ss';
|
||||
persist(state);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const {
|
||||
setLocale,
|
||||
setTimezone,
|
||||
setUnitSystem,
|
||||
setDateFormat,
|
||||
setPreferences,
|
||||
resetPreferences,
|
||||
} = preferencesSlice.actions;
|
||||
|
||||
export default preferencesSlice.reducer;
|
||||
Reference in New Issue
Block a user