Unverified Commit 3c9c6661 authored by zhu-mingye's avatar zhu-mingye Committed by GitHub

[Feature][admin,web] support switching language and backend synchronization (#2009)

* Realize switching language and backend synchronization * Realize switching language and backend synchronization * Realize switching language and backend synchronization * modify i18n
parent 8e5022db
...@@ -42,7 +42,7 @@ public class LocaleChangeInterceptor implements AsyncHandlerInterceptor { ...@@ -42,7 +42,7 @@ public class LocaleChangeInterceptor implements AsyncHandlerInterceptor {
Cookie cookie = WebUtils.getCookie(request, BaseConstant.LOCALE_LANGUAGE_COOKIE); Cookie cookie = WebUtils.getCookie(request, BaseConstant.LOCALE_LANGUAGE_COOKIE);
if (Asserts.isNotNull(cookie)) { if (Asserts.isNotNull(cookie)) {
// Proceed in cookie // Proceed in cookie
return true; LocaleContextHolder.setLocale(parseLocaleValue(cookie.getValue()));
} }
// Proceed in header // Proceed in header
String newLocale = request.getHeader(BaseConstant.LOCALE_LANGUAGE_COOKIE); String newLocale = request.getHeader(BaseConstant.LOCALE_LANGUAGE_COOKIE);
......
...@@ -15,8 +15,14 @@ ...@@ -15,8 +15,14 @@
* limitations under the License. * limitations under the License.
*/ */
import {VERSION} from "@/services/constants"; import {STORY_LANGUAGE, VERSION} from '@/services/constants';
import {parseJsonStr, setTenantStorageAndCookie} from "@/utils/function"; import {
getLocalStorageLanguage, getValueFromLocalStorage,
parseJsonStr,
setCookieByKey,
setKeyToLocalStorage,
setTenantStorageAndCookie
} from '@/utils/function';
import {l} from "@/utils/intl"; import {l} from "@/utils/intl";
import { import {
FullscreenExitOutlined, FullscreenExitOutlined,
...@@ -35,6 +41,7 @@ import {ThemeCloud, ThemeStar} from "@/components/ThemeSvg/ThemeSvg"; ...@@ -35,6 +41,7 @@ import {ThemeCloud, ThemeStar} from "@/components/ThemeSvg/ThemeSvg";
import {chooseTenantSubmit} from "@/services/BusinessCrud"; import {chooseTenantSubmit} from "@/services/BusinessCrud";
import {ErrorNotification, SuccessNotification} from "@/utils/messages"; import {ErrorNotification, SuccessNotification} from "@/utils/messages";
import {THEME} from "@/types/Public/data"; import {THEME} from "@/types/Public/data";
import cookies from 'js-cookie';
const GlobalHeaderRight: React.FC = () => { const GlobalHeaderRight: React.FC = () => {
...@@ -51,7 +58,7 @@ const GlobalHeaderRight: React.FC = () => { ...@@ -51,7 +58,7 @@ const GlobalHeaderRight: React.FC = () => {
* init render theme status * init render theme status
*/ */
useEffect(() => { useEffect(() => {
const theme :any = localStorage.getItem(THEME.NAV_THEME) !== undefined ? localStorage.getItem(THEME.NAV_THEME) : settings?.navTheme const theme :any = getValueFromLocalStorage(THEME.NAV_THEME) !== undefined ? getValueFromLocalStorage(THEME.NAV_THEME) : settings?.navTheme
setThemeChecked(theme === THEME.dark ? true : false); setThemeChecked(theme === THEME.dark ? true : false);
setInitialState((preInitialState) => { setInitialState((preInitialState) => {
return { return {
...@@ -64,6 +71,18 @@ const GlobalHeaderRight: React.FC = () => { ...@@ -64,6 +71,18 @@ const GlobalHeaderRight: React.FC = () => {
}, []); }, []);
useEffect(() => {
const lang = getLocalStorageLanguage();
if (lang) {
setCookieByKey(STORY_LANGUAGE, lang);
setInitialState((s) => ({
...s,
locale: lang,
}));
}
}, [initialState?.settings?.locale]);
if (!initialState || !initialState.settings) { if (!initialState || !initialState.settings) {
return null; return null;
} }
...@@ -203,12 +222,12 @@ const GlobalHeaderRight: React.FC = () => { ...@@ -203,12 +222,12 @@ const GlobalHeaderRight: React.FC = () => {
checkedChildren={<ThemeCloud/>} checkedChildren={<ThemeCloud/>}
unCheckedChildren={<ThemeStar/>} unCheckedChildren={<ThemeStar/>}
onChange={(value) => { onChange={(value) => {
localStorage.setItem(THEME.NAV_THEME, !value ? THEME.light : THEME.dark); setKeyToLocalStorage(THEME.NAV_THEME, !value ? THEME.light : THEME.dark);
setInitialState((preInitialState :any) => { setInitialState((preInitialState :any) => {
return { return {
...preInitialState, ...preInitialState,
settings: { settings: {
...settings, navTheme: !value ? THEME.light : THEME.dark,colorMenuBackground: (localStorage.getItem(THEME.NAV_THEME) === THEME.dark ? "transparent" : "#fff") ...settings, navTheme: !value ? THEME.light : THEME.dark,colorMenuBackground: (getValueFromLocalStorage(THEME.NAV_THEME) === THEME.dark ? "transparent" : "#fff")
} }
}; };
}); });
......
...@@ -495,9 +495,9 @@ export default { ...@@ -495,9 +495,9 @@ export default {
'sys.maven.settings.repositoryUser': 'Maven warehouse user name', 'sys.maven.settings.repositoryUser': 'Maven warehouse user name',
'sys.maven.settings.repositoryUser.note': 'Maven private server authentication user name', 'sys.maven.settings.repositoryUser.note': 'Maven private server authentication user name',
'sys.maven.settings.repositoryPassword': 'Maven warehouse password', 'sys.maven.settings.repositoryPassword': 'Maven warehouse password',
'sys.maven.settings.repositoryPassword.note': 'Maven private server authentication password', 'sys.maven.settings.repositoryPassword.note': 'Maven private server authentication password, please note: For security reasons, the password will not be displayed in plain text, and it has been desensitized when it returns',
'sys.dolphinscheduler.settings.enable': 'Whether to enable', 'sys.dolphinscheduler.settings.enable': 'Whether to enable DolphinScheduler',
'sys.dolphinscheduler.settings.enable.note': 'Whether it is enabled or not, the relevant functions of DolphinScheduler can only be used after it is enabled, please ensure that the relevant configuration of DolphinScheduler is correct', 'sys.dolphinscheduler.settings.enable.note': 'Whether to enable DolphinScheduler, the relevant functions of DolphinScheduler can only be used after it is enabled, please ensure that the relevant configuration of DolphinScheduler is correct',
'sys.dolphinscheduler.settings.url': 'DolphinScheduler address', 'sys.dolphinscheduler.settings.url': 'DolphinScheduler address',
'sys.dolphinscheduler.settings.url.note': 'The address must be consistent with the address configured in the DolphinScheduler background, eg: http://127.0.0.1:12345/dolphinscheduler', 'sys.dolphinscheduler.settings.url.note': 'The address must be consistent with the address configured in the DolphinScheduler background, eg: http://127.0.0.1:12345/dolphinscheduler',
'sys.dolphinscheduler.settings.token': 'DolphinScheduler Token', 'sys.dolphinscheduler.settings.token': 'DolphinScheduler Token',
......
...@@ -497,9 +497,9 @@ export default { ...@@ -497,9 +497,9 @@ export default {
'sys.maven.settings.repositoryUser': 'Maven 仓库用户名', 'sys.maven.settings.repositoryUser': 'Maven 仓库用户名',
'sys.maven.settings.repositoryUser.note': 'Maven 私服认证用户名', 'sys.maven.settings.repositoryUser.note': 'Maven 私服认证用户名',
'sys.maven.settings.repositoryPassword': 'Maven 仓库密码', 'sys.maven.settings.repositoryPassword': 'Maven 仓库密码',
'sys.maven.settings.repositoryPassword.note': 'Maven 私服认证密码', 'sys.maven.settings.repositoryPassword.note': 'Maven 私服认证密码,请注意: 此处为了安全起见,密码不会显示明文,返回时已做脱敏处理',
'sys.dolphinscheduler.settings.enable': '是否启用', 'sys.dolphinscheduler.settings.enable': '是否启用 DolphinScheduler',
'sys.dolphinscheduler.settings.enable.note': '是否启用, 启用后才能使用 DolphinScheduler 的相关功能, 请确保 DolphinScheduler 的相关配置正确', 'sys.dolphinscheduler.settings.enable.note': '是否启用 DolphinScheduler ,启用后才能使用 DolphinScheduler 的相关功能, 请确保 DolphinScheduler 的相关配置正确',
'sys.dolphinscheduler.settings.url': 'DolphinScheduler 地址', 'sys.dolphinscheduler.settings.url': 'DolphinScheduler 地址',
'sys.dolphinscheduler.settings.url.note': '地址必须和DolphinScheduler后台配置的地址一致,eg: http://127.0.0.1:12345/dolphinscheduler', 'sys.dolphinscheduler.settings.url.note': '地址必须和DolphinScheduler后台配置的地址一致,eg: http://127.0.0.1:12345/dolphinscheduler',
'sys.dolphinscheduler.settings.token': 'DolphinScheduler Token', 'sys.dolphinscheduler.settings.token': 'DolphinScheduler Token',
......
...@@ -16,10 +16,27 @@ ...@@ -16,10 +16,27 @@
*/ */
import {useEmotionCss} from "@ant-design/use-emotion-css"; import {useEmotionCss} from "@ant-design/use-emotion-css";
import {SelectLang} from "@@/exports"; import {SelectLang, useModel} from '@@/exports';
import React from "react"; import React, {useEffect} from 'react';
import {getLocalStorageLanguage, setCookieByKey} from '@/utils/function';
import {STORY_LANGUAGE} from '@/services/constants';
const LangSwitch = () => { const LangSwitch = () => {
const {initialState, setInitialState} = useModel("@@initialState");
useEffect(() => {
const lang = getLocalStorageLanguage();
if (lang) {
setCookieByKey(STORY_LANGUAGE, lang);
setInitialState((s) => ({
...s,
locale: lang,
}));
}
}, [initialState]);
const langClassName = useEmotionCss(({token}) => { const langClassName = useEmotionCss(({token}) => {
return { return {
width: 42, width: 42,
......
...@@ -79,24 +79,17 @@ const GeneralConfig: React.FC<GeneralConfigProps> = (props) => { ...@@ -79,24 +79,17 @@ const GeneralConfig: React.FC<GeneralConfigProps> = (props) => {
</>; </>;
}; };
/** const renderValuesOfForm = (entity: BaseConfigProperties) => {
* render value for each entity in the list if (entity.frontType === 'boolean') {
* @param entity return <Switch {...SWITCH_OPTIONS()} style={{width: '4vw'}} checked={entity.value}
*/
const renderValue = (entity: BaseConfigProperties) => {
return <>
{
entity.frontType === 'boolean' ?
<Switch
{...SWITCH_OPTIONS()}
style={{width: '4vw'}}
checked={entity.value}
onChange={(checked) => handleSubmit({...entity, value: checked})} onChange={(checked) => handleSubmit({...entity, value: checked})}
/> : />;
<Input style={{width: '30vw'}} disabled value={entity.value}/> } else if (entity.frontType === 'password') {
return <Input.Password style={{width: '30vw'}} disabled value={entity.value}/>;
} else {
return <Input style={{width: '30vw'}} disabled value={entity.value}/>;
}
} }
</>;
};
const metasRestProps: ProListMetas = { const metasRestProps: ProListMetas = {
...@@ -114,7 +107,7 @@ const GeneralConfig: React.FC<GeneralConfigProps> = (props) => { ...@@ -114,7 +107,7 @@ const GeneralConfig: React.FC<GeneralConfigProps> = (props) => {
}, },
content: { content: {
dataIndex: 'value', dataIndex: 'value',
render: (dom: any, entity: BaseConfigProperties) => renderValue(entity), render: (dom: any, entity: BaseConfigProperties) => renderValuesOfForm(entity),
}, },
actions: { actions: {
render: (text: any, row: BaseConfigProperties, index: number, action: any) => renderActions(action, row), render: (text: any, row: BaseConfigProperties, index: number, action: any) => renderActions(action, row),
......
...@@ -41,8 +41,7 @@ const SettingOverView = () => { ...@@ -41,8 +41,7 @@ const SettingOverView = () => {
}, []); }, []);
const handleSaveSubmit = async (data: BaseConfigProperties) => { const handleSaveSubmit = async (data: BaseConfigProperties) => {
// todo: save data of update data const {code} = await handleOption(API_CONSTANTS.SYSTEM_MODIFY_CONFIG, l('sys.setting.modify', '', {key: l(`sys.${data.key}`)}), data);
const {code} = await handleOption(API_CONSTANTS.SYSTEM_MODIFY_CONFIG, l('sys.setting.modify', '', {key: data.key}), data);
if (code === RESPONSE_CODE.SUCCESS) await fetchData(); if (code === RESPONSE_CODE.SUCCESS) await fetchData();
}; };
......
...@@ -200,6 +200,14 @@ export const TENANT_ID = "tenantId"; ...@@ -200,6 +200,14 @@ export const TENANT_ID = "tenantId";
*/ */
export const VERSION = "0.8.0"; export const VERSION = "0.8.0";
/**
* the platform language
*/
export const STORY_LANGUAGE = "language";
export const LANGUAGE_KEY = 'umi_locale';
export const LANGUAGE_ZH = 'zh-CN';
export const LANGUAGE_EN = 'en-US';
/** /**
* REQUEST METHOD CONSTANTS * REQUEST METHOD CONSTANTS
*/ */
......
...@@ -15,12 +15,12 @@ ...@@ -15,12 +15,12 @@
* limitations under the License. * limitations under the License.
*/ */
import {DIALECT, TENANT_ID} from "@/services/constants"; import {DIALECT, LANGUAGE_KEY, LANGUAGE_ZH, TENANT_ID} from '@/services/constants';
import cookies from "js-cookie"; import cookies from 'js-cookie';
import {THEME, CODE_EDIT_THEME} from "@/types/Public/data"; import {CODE_EDIT_THEME, THEME} from '@/types/Public/data';
import {editor} from "monaco-editor"; import {editor} from 'monaco-editor';
import React, {useEffect, useState} from "react"; import React, {useEffect, useState} from 'react';
import {trim} from "lodash"; import {trim} from 'lodash';
import { import {
FileIcon, FileIcon,
FolderSvgExpand, FolderSvgExpand,
...@@ -32,29 +32,75 @@ import { ...@@ -32,29 +32,75 @@ import {
ShellSvg, ShellSvg,
XMLSvg, XMLSvg,
YAMLSvg YAMLSvg
} from "@/components/Icons/CodeLanguageIcon"; } from '@/components/Icons/CodeLanguageIcon';
import path from "path"; import path from 'path';
import {l} from "@/utils/intl"; import {l} from '@/utils/intl';
/** /**
* PUT tenantId TO localStorage & cookies * get language by localStorage's umi_locale , if not exist , return zh-CN
* @param tenantId * @param tenantId
*/ */
export function setTenantStorageAndCookie(tenantId: number) { export function getLocalStorageLanguage() {
// save as localStorage return localStorage.getItem(LANGUAGE_KEY) || LANGUAGE_ZH;
localStorage.setItem(TENANT_ID, tenantId.toString()); }
// save as cookies
cookies.set(TENANT_ID, tenantId.toString(), {path: "/"}); /**
* set key value to localStorage
* @param tenantId
*/
export function setKeyToLocalStorage(key: string, value: string) {
localStorage.setItem(key, value);
} }
/**
* get value by localStorage's key
* @param tenantId
*/
export function getValueFromLocalStorage(key: string) {
return localStorage.getItem(key) || '';
}
/** /**
* get tenant id * get tenant id
* @param tenantId * @param tenantId
*/ */
export function getTenantByLocalStorage() { export function getTenantByLocalStorage() {
return localStorage.getItem(TENANT_ID); return getValueFromLocalStorage(TENANT_ID);
}
/**
* get cookie by key
* @param tenantId
*/
export function getCookieByKey(key: string) {
return cookies.get(key) || '';
} }
/**
* set cookie by key
* @param tenantId
*/
export function setCookieByKey(key: string, value: string, options?: {}) {
cookies.set(key, value, options);
}
/**
* PUT tenantId TO localStorage & cookies
* @param tenantId
*/
export function setTenantStorageAndCookie(tenantId: number) {
// save as localStorage
setKeyToLocalStorage(TENANT_ID, tenantId.toString());
// save as cookies
setCookieByKey(TENANT_ID, tenantId.toString(), {path: '/'});
}
/** /**
* parseJsonStr * parseJsonStr
* @param jsonStr * @param jsonStr
...@@ -80,30 +126,30 @@ export function convertCodeEditTheme() { ...@@ -80,30 +126,30 @@ export function convertCodeEditTheme() {
* user can define a new theme by calling the defineTheme method on the editor. * user can define a new theme by calling the defineTheme method on the editor.
*/ */
editor.defineTheme(CODE_EDIT_THEME.VS_CUSTOME, { editor.defineTheme(CODE_EDIT_THEME.VS_CUSTOME, {
base: "vs", // 指定基础主题 , 可选值: 'vs', 'vs-dark', 'hc-black' , base theme base: 'vs', // 指定基础主题 , 可选值: 'vs', 'vs-dark', 'hc-black' , base theme
inherit: true, // 是否继承基础主题配置 , 默认为 true, is to inherit the base theme inherit: true, // 是否继承基础主题配置 , 默认为 true, is to inherit the base theme
// rules is an array of rules. The array must not be sparse (i.e. do not use holes). // rules is an array of rules. The array must not be sparse (i.e. do not use holes).
rules: [ rules: [
{token: "comment", foreground: "#008800", fontStyle: "italic"}, {token: 'comment', foreground: '#008800', fontStyle: 'italic'},
{token: "keyword", foreground: "#064cff", fontStyle: "bold"}, {token: 'keyword', foreground: '#064cff', fontStyle: 'bold'},
{token: "string", foreground: "#507dee"}, {token: 'string', foreground: '#507dee'},
{token: "delimiter", foreground: "#041d81"}, {token: 'delimiter', foreground: '#041d81'},
{token: "readonly", foreground: "#e73a6e", background: "#141414", fontStyle: "italic"}, {token: 'readonly', foreground: '#e73a6e', background: '#141414', fontStyle: 'italic'},
{token: "number", foreground: "#ffffff"}, {token: 'number', foreground: '#ffffff'},
], ],
// colors is an object of color identifiers and their color values. // colors is an object of color identifiers and their color values.
colors: { colors: {
"editor.background": "#141414", // editor background color 'editor.background': '#141414', // editor background color
"editor.lineHighlightBackground": "#141414", // editor line highlight background color 'editor.lineHighlightBackground': '#141414', // editor line highlight background color
"editorLineNumber.foreground": "#ffffff", // editor line number color 'editorLineNumber.foreground': '#ffffff', // editor line number color
"editorCursor.foreground": "#ffffff", // editor cursor color 'editorCursor.foreground': '#ffffff', // editor cursor color
"editorIndentGuide.background": "#ffffff", // editor indent guide color 'editorIndentGuide.background': '#ffffff', // editor indent guide color
"editor.foreground": "#ffffff", // editor selection highlight border color 'editor.foreground': '#ffffff', // editor selection highlight border color
"editor.selectionBackground": "#4ba1ef", // editor selection highlight color 'editor.selectionBackground': '#4ba1ef', // editor selection highlight color
"editor.selectionHighlightBorder": "#4ba1ef", // editor selection highlight border color 'editor.selectionHighlightBorder': '#4ba1ef', // editor selection highlight border color
"editor.findMatchBackground": "#4ba1ef", // editor find match highlight color 'editor.findMatchBackground': '#4ba1ef', // editor find match highlight color
"editor.wordHighlightBackground": "#8bb2d2", // editor word highlight color 'editor.wordHighlightBackground': '#8bb2d2', // editor word highlight color
} }
}); });
...@@ -243,7 +289,7 @@ export const renderIcon = (type: string, splitChar: string, isLeft: boolean) => ...@@ -243,7 +289,7 @@ export const renderIcon = (type: string, splitChar: string, isLeft: boolean) =>
} else { } else {
if (trim(splitChar).length === 0) { if (trim(splitChar).length === 0) {
return getIcon(type); return getIcon(type);
}else { } else {
let suffixOfType = type.toString().split(splitChar).reverse()[0]; let suffixOfType = type.toString().split(splitChar).reverse()[0];
return getIcon(suffixOfType); return getIcon(suffixOfType);
} }
...@@ -271,7 +317,7 @@ export const renderLanguage = (type: string, splitChar: string) => { ...@@ -271,7 +317,7 @@ export const renderLanguage = (type: string, splitChar: string) => {
*/ */
export const folderSeparator = () => { export const folderSeparator = () => {
return path.sep; return path.sep;
} };
/** /**
...@@ -280,26 +326,24 @@ export const folderSeparator = () => { ...@@ -280,26 +326,24 @@ export const folderSeparator = () => {
*/ */
export const parseSecondStr = (s_time: number) => { export const parseSecondStr = (s_time: number) => {
let second_time = Math.floor(s_time); let second_time = Math.floor(s_time);
let time = second_time + l( 'global.time.second'); let time = second_time + l('global.time.second');
if (second_time > 60) { if (second_time > 60) {
let second = second_time % 60; let second = second_time % 60;
let min = Math.floor(second_time / 60); let min = Math.floor(second_time / 60);
time = min + l( 'global.time.minute') + second + l( 'global.time.second'); time = min + l('global.time.minute') + second + l('global.time.second');
if (min > 60) { if (min > 60) {
min = Math.floor(second_time / 60) % 60; min = Math.floor(second_time / 60) % 60;
let hour = Math.floor(Math.floor(second_time / 60) / 60); let hour = Math.floor(Math.floor(second_time / 60) / 60);
time = hour + l( 'global.time.hour') + min + l( 'global.time.minute') + second + l( 'global.time.second'); time = hour + l('global.time.hour') + min + l('global.time.minute') + second + l('global.time.second');
if (hour > 24) { if (hour > 24) {
hour = Math.floor(Math.floor(second_time / 60) / 60) % 24; hour = Math.floor(Math.floor(second_time / 60) / 60) % 24;
let day = Math.floor(Math.floor(Math.floor(second_time / 60) / 60) / 24); let day = Math.floor(Math.floor(Math.floor(second_time / 60) / 60) / 24);
time = day + l( 'global.time.day') + hour + l( 'global.time.hour') + min + l( 'global.time.minute') + second + l( 'global.time.second'); time = day + l('global.time.day') + hour + l('global.time.hour') + min + l('global.time.minute') + second + l('global.time.second');
} }
} }
} }
return time; return time;
} };
/** /**
...@@ -318,7 +362,7 @@ export const buildTreeData = (data: any): any => data?.map((item: any) => { ...@@ -318,7 +362,7 @@ export const buildTreeData = (data: any): any => data?.map((item: any) => {
isLeaf: !item.leaf, isLeaf: !item.leaf,
name: item.name, name: item.name,
parentId: item.path, parentId: item.path,
icon: renderIcon(item.name, ".", item.leaf), icon: renderIcon(item.name, '.', item.leaf),
content: item.content, content: item.content,
path: item.path, path: item.path,
title: item.name, title: item.name,
...@@ -330,7 +374,7 @@ export const buildTreeData = (data: any): any => data?.map((item: any) => { ...@@ -330,7 +374,7 @@ export const buildTreeData = (data: any): any => data?.map((item: any) => {
isLeaf: !item.leaf, isLeaf: !item.leaf,
name: item.name, name: item.name,
parentId: item.path, parentId: item.path,
icon: renderIcon(item.name, ".", item.leaf), icon: renderIcon(item.name, '.', item.leaf),
content: item.content, content: item.content,
path: item.path, path: item.path,
title: item.name, title: item.name,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment