fix(client): language settings

This commit is contained in:
chenos 2022-06-10 08:38:24 +08:00
parent b466b8f6fb
commit d2837740bf
3 changed files with 30 additions and 18 deletions

View File

@ -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',

View File

@ -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);

View File

@ -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();
},