反应什么是访问语言环境的最简单方法
react what is the esiest way to acces the locale
在我的 React 应用程序中,我这样设置语言环境:
const store = createStore(
combineReducers({
i18n: i18nReducer
}),
applyMiddleware(thunk)
);
syncTranslationWithStore(store)
store.dispatch(loadTranslations(translationsObject));
store.dispatch(setLocale('de'));
检查设置了哪种语言的简便方法是什么?
我试过这个:
store.dispatch(getLocale());
但这不起作用。
感谢您的帮助
你应该使用 connect 将你的 React 组件连接到 Redux state,并映射状态 属性 指示 prop 的语言环境,像这样:
const mapStateToProps = (state) => {
return {
locale: state.i18n.locale
}
};
const connectedSomeComponent= connect(
mapStateToProps
)(someComponent);
那么你可以简单地使用 locale
prop in someComponent
。
您还可以使用 getState 直接访问存储状态,因此如果您想在 React 组件之外访问状态 属性,您可以这样做:
const myState = store.getState();
const locale = myState.i18n.locale;
在我的 React 应用程序中,我这样设置语言环境:
const store = createStore(
combineReducers({
i18n: i18nReducer
}),
applyMiddleware(thunk)
);
syncTranslationWithStore(store)
store.dispatch(loadTranslations(translationsObject));
store.dispatch(setLocale('de'));
检查设置了哪种语言的简便方法是什么?
我试过这个:
store.dispatch(getLocale());
但这不起作用。
感谢您的帮助
你应该使用 connect 将你的 React 组件连接到 Redux state,并映射状态 属性 指示 prop 的语言环境,像这样:
const mapStateToProps = (state) => {
return {
locale: state.i18n.locale
}
};
const connectedSomeComponent= connect(
mapStateToProps
)(someComponent);
那么你可以简单地使用 locale
prop in someComponent
。
您还可以使用 getState 直接访问存储状态,因此如果您想在 React 组件之外访问状态 属性,您可以这样做:
const myState = store.getState();
const locale = myState.i18n.locale;