diff --git a/packages/core/client/src/system-settings/SystemSettingsShortcut.tsx b/packages/core/client/src/system-settings/SystemSettingsShortcut.tsx index 1062331be..f655a6e24 100644 --- a/packages/core/client/src/system-settings/SystemSettingsShortcut.tsx +++ b/packages/core/client/src/system-settings/SystemSettingsShortcut.tsx @@ -5,8 +5,16 @@ import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useSystemSettings } from '.'; import { PluginManager, useAPIClient, useRequest } from '..'; +import locale from '../locale'; import { ActionContext, SchemaComponent, useActionContext } from '../schema-component'; +const langs = Object.keys(locale).map((lang) => { + return { + label: locale[lang].label, + value: lang, + }; +}); + const useCloseAction = () => { const { setVisible } = useActionContext(); return { @@ -79,28 +87,22 @@ const schema: ISchema = { // accept: 'jpg,png' }, }, - appLang: { - type: 'string', - title: '{{t("Language")}}', - 'x-component': 'Select', - 'x-decorator': 'FormItem', - enum: [ - { label: 'English', value: 'en-US' }, - { label: '简体中文', value: 'zh-CN' }, - ], - }, enabledLanguages: { type: 'array', title: '{{t("Enabled languages")}}', 'x-component': 'Select', - "x-component-props": { + 'x-component-props': { mode: 'multiple', }, 'x-decorator': 'FormItem', - enum: [ - { label: 'English', value: 'en-US' }, - { label: '简体中文', value: 'zh-CN' }, - ], + enum: langs, + }, + appLang: { + type: 'string', + title: '{{t("Default language")}}', + 'x-component': 'Select', + 'x-decorator': 'FormItem', + enum: langs, }, allowSignUp: { type: 'boolean', diff --git a/packages/core/client/src/user/LanguageSettings.tsx b/packages/core/client/src/user/LanguageSettings.tsx index 6a553276b..a9be4e678 100644 --- a/packages/core/client/src/user/LanguageSettings.tsx +++ b/packages/core/client/src/user/LanguageSettings.tsx @@ -11,7 +11,7 @@ export const LanguageSettings = () => { const ctx = useCurrentUserContext(); const { data } = useSystemSettings(); const enabledLanguages: string[] = data?.data?.enabledLanguages || []; - if (!enabledLanguages.length) { + if (enabledLanguages.length < 2) { return null; } // console.log('data', data?.data?.enabledLanguages); diff --git a/packages/plugins/client/src/plugin.ts b/packages/plugins/client/src/plugin.ts index e3de81c8f..bdbb17fc1 100644 --- a/packages/plugins/client/src/plugin.ts +++ b/packages/plugins/client/src/plugin.ts @@ -26,19 +26,29 @@ export class ClientPlugin extends Plugin { async getInfo(ctx, next) { const SystemSetting = ctx.db.getRepository('systemSettings'); const systemSetting = await SystemSetting.findOne(); + const enabledLanguages: string[] = systemSetting.get('enabledLanguages') || []; const currentUser = ctx.state.currentUser; + let lang = systemSetting?.appLang || process.env.APP_LANG || 'en-US'; + if (enabledLanguages.includes(currentUser?.appLang)) { + lang = currentUser?.appLang; + } ctx.body = { version: this.app.getVersion(), - lang: currentUser?.appLang || systemSetting?.appLang || process.env.APP_LANG || 'en-US', + lang, }; await next(); }, async getLang(ctx, next) { const SystemSetting = ctx.db.getRepository('systemSettings'); const systemSetting = await SystemSetting.findOne(); + const enabledLanguages: string[] = systemSetting.get('enabledLanguages') || []; const currentUser = ctx.state.currentUser; + let lang = systemSetting?.appLang || process.env.APP_LANG || 'en-US'; + if (enabledLanguages.includes(currentUser?.appLang)) { + lang = currentUser?.appLang; + } ctx.body = { - lang: currentUser?.appLang || systemSetting?.appLang || process.env.APP_LANG || 'en-US', + lang, }; await next(); },