fix(client): language settings
This commit is contained in:
parent
b466b8f6fb
commit
d2837740bf
@ -5,8 +5,16 @@ import React, { useState } from 'react';
|
|||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { useSystemSettings } from '.';
|
import { useSystemSettings } from '.';
|
||||||
import { PluginManager, useAPIClient, useRequest } from '..';
|
import { PluginManager, useAPIClient, useRequest } from '..';
|
||||||
|
import locale from '../locale';
|
||||||
import { ActionContext, SchemaComponent, useActionContext } from '../schema-component';
|
import { ActionContext, SchemaComponent, useActionContext } from '../schema-component';
|
||||||
|
|
||||||
|
const langs = Object.keys(locale).map((lang) => {
|
||||||
|
return {
|
||||||
|
label: locale[lang].label,
|
||||||
|
value: lang,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
const useCloseAction = () => {
|
const useCloseAction = () => {
|
||||||
const { setVisible } = useActionContext();
|
const { setVisible } = useActionContext();
|
||||||
return {
|
return {
|
||||||
@ -79,28 +87,22 @@ const schema: ISchema = {
|
|||||||
// accept: 'jpg,png'
|
// 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: {
|
enabledLanguages: {
|
||||||
type: 'array',
|
type: 'array',
|
||||||
title: '{{t("Enabled languages")}}',
|
title: '{{t("Enabled languages")}}',
|
||||||
'x-component': 'Select',
|
'x-component': 'Select',
|
||||||
"x-component-props": {
|
'x-component-props': {
|
||||||
mode: 'multiple',
|
mode: 'multiple',
|
||||||
},
|
},
|
||||||
'x-decorator': 'FormItem',
|
'x-decorator': 'FormItem',
|
||||||
enum: [
|
enum: langs,
|
||||||
{ label: 'English', value: 'en-US' },
|
},
|
||||||
{ label: '简体中文', value: 'zh-CN' },
|
appLang: {
|
||||||
],
|
type: 'string',
|
||||||
|
title: '{{t("Default language")}}',
|
||||||
|
'x-component': 'Select',
|
||||||
|
'x-decorator': 'FormItem',
|
||||||
|
enum: langs,
|
||||||
},
|
},
|
||||||
allowSignUp: {
|
allowSignUp: {
|
||||||
type: 'boolean',
|
type: 'boolean',
|
||||||
|
@ -11,7 +11,7 @@ export const LanguageSettings = () => {
|
|||||||
const ctx = useCurrentUserContext();
|
const ctx = useCurrentUserContext();
|
||||||
const { data } = useSystemSettings();
|
const { data } = useSystemSettings();
|
||||||
const enabledLanguages: string[] = data?.data?.enabledLanguages || [];
|
const enabledLanguages: string[] = data?.data?.enabledLanguages || [];
|
||||||
if (!enabledLanguages.length) {
|
if (enabledLanguages.length < 2) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// console.log('data', data?.data?.enabledLanguages);
|
// console.log('data', data?.data?.enabledLanguages);
|
||||||
|
@ -26,19 +26,29 @@ export class ClientPlugin extends Plugin {
|
|||||||
async getInfo(ctx, next) {
|
async getInfo(ctx, next) {
|
||||||
const SystemSetting = ctx.db.getRepository('systemSettings');
|
const SystemSetting = ctx.db.getRepository('systemSettings');
|
||||||
const systemSetting = await SystemSetting.findOne();
|
const systemSetting = await SystemSetting.findOne();
|
||||||
|
const enabledLanguages: string[] = systemSetting.get('enabledLanguages') || [];
|
||||||
const currentUser = ctx.state.currentUser;
|
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 = {
|
ctx.body = {
|
||||||
version: this.app.getVersion(),
|
version: this.app.getVersion(),
|
||||||
lang: currentUser?.appLang || systemSetting?.appLang || process.env.APP_LANG || 'en-US',
|
lang,
|
||||||
};
|
};
|
||||||
await next();
|
await next();
|
||||||
},
|
},
|
||||||
async getLang(ctx, next) {
|
async getLang(ctx, next) {
|
||||||
const SystemSetting = ctx.db.getRepository('systemSettings');
|
const SystemSetting = ctx.db.getRepository('systemSettings');
|
||||||
const systemSetting = await SystemSetting.findOne();
|
const systemSetting = await SystemSetting.findOne();
|
||||||
|
const enabledLanguages: string[] = systemSetting.get('enabledLanguages') || [];
|
||||||
const currentUser = ctx.state.currentUser;
|
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 = {
|
ctx.body = {
|
||||||
lang: currentUser?.appLang || systemSetting?.appLang || process.env.APP_LANG || 'en-US',
|
lang,
|
||||||
};
|
};
|
||||||
await next();
|
await next();
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user