在 JHipster 7.1.0 + React 中,如何在用户登录时将 i18n 更新为当前用户的 lanKey?
In JHipster 7.1.0 + React, How to update the i18n with the current user's lanKey when the user logs in?
我使用 JHipster 7.1.0 和 React 创建了一个新应用程序,使用两种语言:英语和法语。默认语言为英语。当用户登录时,我想用当前用户的 langKey 更新系统语言(例如 'fr')。在 app.tsx
中,我尝试了以下方法:
import { setLocale } from 'app/shared/reducers/locale';
.
.
.
const currentLocale = useAppSelector(state => state.locale.currentLocale);
const isAuthenticated = useAppSelector(state => state.authentication.isAuthenticated);
const currentUser = useAppSelector(state => state.authentication.account);
useEffect(() => {
if (isAuthenticated){
setLocale(currentUser.langKey);
// or
currentLocale = currentUser.langKey;
}
}, [isAuthenticated]);
不幸的是,它不起作用。你能帮帮我吗?
您可能需要发送:
const dispatch = useDispatch()
useEffect(() => {
if (isAuthenticated){
dispatch(setLocale(currentUser.langKey));
}
}, [isAuthenticated]);
我找到了更好的方法:在 loign.tsx 我添加了
useEffect(() => {
if (isAuthenticated){
if (Storage.session.get(`locale`)) {
Storage.session.remove(`locale`);
}
dispatch(setLocale(account.langKey));
}
}, [isAuthenticated]);
我使用 JHipster 7.1.0 和 React 创建了一个新应用程序,使用两种语言:英语和法语。默认语言为英语。当用户登录时,我想用当前用户的 langKey 更新系统语言(例如 'fr')。在 app.tsx
中,我尝试了以下方法:
import { setLocale } from 'app/shared/reducers/locale';
.
.
.
const currentLocale = useAppSelector(state => state.locale.currentLocale);
const isAuthenticated = useAppSelector(state => state.authentication.isAuthenticated);
const currentUser = useAppSelector(state => state.authentication.account);
useEffect(() => {
if (isAuthenticated){
setLocale(currentUser.langKey);
// or
currentLocale = currentUser.langKey;
}
}, [isAuthenticated]);
不幸的是,它不起作用。你能帮帮我吗?
您可能需要发送:
const dispatch = useDispatch()
useEffect(() => {
if (isAuthenticated){
dispatch(setLocale(currentUser.langKey));
}
}, [isAuthenticated]);
我找到了更好的方法:在 loign.tsx 我添加了
useEffect(() => {
if (isAuthenticated){
if (Storage.session.get(`locale`)) {
Storage.session.remove(`locale`);
}
dispatch(setLocale(account.langKey));
}
}, [isAuthenticated]);