fix: language settings

This commit is contained in:
chenos 2022-03-21 21:37:35 +08:00
parent 7285dd04ee
commit f7ce3f64cf
5 changed files with 35 additions and 6 deletions

View File

@ -12,6 +12,7 @@ process.env.MFSU_AD = 'none';
const umiConfig = getUmiConfig(); const umiConfig = getUmiConfig();
export default defineConfig({ export default defineConfig({
hash: true,
define: { define: {
'process.env.NOCOBASE_ENV': process.env.NOCOBASE_ENV, 'process.env.NOCOBASE_ENV': process.env.NOCOBASE_ENV,
...umiConfig.define, ...umiConfig.define,

View File

@ -19,7 +19,7 @@ export function AntdConfigProvider(props) {
i18n.changeLanguage(data?.data?.lang); i18n.changeLanguage(data?.data?.lang);
} }
}, },
manual: true, // !remoteLocale, manual: !remoteLocale,
}, },
); );
if (loading) { if (loading) {

View File

@ -3,7 +3,15 @@ import { uid } from '@formily/shared';
import { Menu } from 'antd'; import { Menu } from 'antd';
import React, { useContext, useState } from 'react'; import React, { useContext, useState } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { ActionContext, DropdownVisibleContext, SchemaComponent, useActionContext, useCurrentUserContext, useRequest } from '../'; import {
ActionContext,
DropdownVisibleContext,
SchemaComponent,
useActionContext,
useCurrentUserContext,
useRequest
} from '../';
import { useAPIClient } from '../api-client';
const useCloseAction = () => { const useCloseAction = () => {
const { setVisible } = useActionContext(); const { setVisible } = useActionContext();
@ -24,13 +32,22 @@ const useCurrentUserValues = (options) => {
}; };
const useSaveCurrentUserValues = () => { const useSaveCurrentUserValues = () => {
const ctx = useCurrentUserContext();
const { setVisible } = useActionContext(); const { setVisible } = useActionContext();
const form = useForm(); const form = useForm();
const api = useAPIClient();
return { return {
async run() { async run() {
form.submit((values) => { const values = await form.submit<any>();
setVisible(false); setVisible(false);
console.log(values); await api.resource('users').updateProfile({
values,
});
ctx.mutate({
data: {
...ctx?.data?.data,
...values,
},
}); });
}, },
}; };

View File

@ -1,10 +1,13 @@
import { Menu, Select } from 'antd'; import { Menu, Select } from 'antd';
import React, { useState } from 'react'; import React, { useState } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useAPIClient, useCurrentUserContext } from '..';
export const LanguageSettings = () => { export const LanguageSettings = () => {
const { t, i18n } = useTranslation(); const { t, i18n } = useTranslation();
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const api = useAPIClient();
const ctx = useCurrentUserContext();
return ( return (
<Menu.Item <Menu.Item
onClick={() => { onClick={() => {
@ -25,6 +28,11 @@ export const LanguageSettings = () => {
]} ]}
value={i18n.language} value={i18n.language}
onChange={async (lang) => { onChange={async (lang) => {
await api.resource('users').updateProfile({
values: {
appLang: lang,
},
});
await i18n.changeLanguage(lang); await i18n.changeLanguage(lang);
window.location.reload(); window.location.reload();
}} }}

View File

@ -32,8 +32,11 @@ export class ClientPlugin extends Plugin {
name: 'app', name: 'app',
actions: { actions: {
async getLang(ctx, next) { async getLang(ctx, next) {
const SystemSetting = ctx.db.getRepository('systemSettings');
const systemSetting = await SystemSetting.findOne();
const currentUser = ctx.state.currentUser;
ctx.body = { ctx.body = {
lang: 'zh-CN', lang: currentUser?.appLang || systemSetting?.appLang || process.env.APP_LANG || 'en-US',
}; };
await next(); await next();
}, },