admin/lang: added "force en"

Massimo Melina committed Apr 12, 2023 at 14:43 UTC 4f871c399c693d9f9f909952e19043add8f898f9
2 files changed +10 -9
admin/src/LangPage.ts
+4 -5
@@ -1,6 +1,6 @@
1 // This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
2
3 -import { createElement as h, Fragment, useEffect, useState } from 'react';
3 +import { createElement as h, Fragment, useEffect, useMemo, useState } from 'react';
4 import { apiCall, useApiEx, useApiList } from './api'
5 import { DataGrid } from '@mui/x-data-grid'
6 import { Alert, Box, Button } from '@mui/material'
@@ -12,16 +12,15 @@ import { Field, SelectField } from '@hfs/mui-grid-form';
12
13 export default function LangPage() {
14 const { list, error, connecting, reload } = useApiList('list_langs', undefined, { addId: true })
15 - if (error)
16 - return error
15 + const langs = useMemo(() => ['en', ..._.uniq(list.map(x => x.code))], [list])
16 const large = useBreakpoint('md')
18 - return h(Fragment, {},
17 + return error || h(Fragment, {},
18 large && h(Alert, { severity: 'info' }, "Translation is limited to Front-end, it doesn't apply to Admin-panel"),
19 h(Box, { mt: 1, maxWidth: '40em', flex: 1, display: 'flex', flexDirection: 'column' },
20 h(Box, { mb: 1, display: 'flex' },
21 h(Button, { variant: 'contained', startIcon: h(Upload), onClick: add }, "Add"),
22 h(Box, { flex: 1 }),
24 - h(ForceLang, { langs: _.uniq(list.map(x => x.code)) }),
23 + h(ForceLang, { langs }),
24 ),
25 h(DataGrid, {
26 loading: connecting,
src/lang.ts
+6 -4
@@ -17,10 +17,11 @@ export function file2code(fn: string) {
17 }
18
19 export async function getLangData(ctx: Koa.Context) {
20 - if (forceLangData)
20 + const param = String(ctx.query.lang || '')
21 + if (!param && forceLangData)
22 return forceLangData
23 const ret: any = {}
23 - const csv = String(ctx.query.lang||'') || ctx.get('Accept-Language') || ''
24 + const csv = param || ctx.get('Accept-Language') || ''
25 const langs = wantArray(csv.split(',').map(x => x.toLowerCase()))
26 let i = 0
27 while (i < langs.length) {
@@ -48,8 +49,9 @@ let forceLangData: any
49 let undo: any
50 defineConfig('force_lang', '', v => {
51 undo?.()
51 - forceLangData = undefined
52 - if (!v) return
52 + if (!v)
53 + return forceLangData = undefined
54 + forceLangData = {} // necessary to make the embedded language work
55 const res = watchLoad(code2file(v), data => {
56 forceLangData = { [v]: JSON.parse(data) }
57 })