Skip to content

Commit

Permalink
feat(language): customize language switcher
Browse files Browse the repository at this point in the history
  • Loading branch information
serhii-zubkov committed Feb 27, 2024
1 parent 37f101d commit e8ae87f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/backend/controllers/api-controller.ts
Expand Up @@ -293,6 +293,34 @@ class ApiController {
].join('\n'),
}
}

/**
* Switches language for the admin panel.
* To call it use {@link ApiClient#switchLanguage} method.
*
* Handler function responsible for a _.../api/resources/{resourceId}/bulk/{action}?recordIds={recordIds}_
*
* @param {ActionRequest} request
* @param {any} response
*
* @return {Promise<{ redirectUrl: string }>} action response
*/
async switchLanguage(request: any): Promise<{ redirectUrl: string }> {
if (typeof this._admin.options.locale === 'function') {
const { lang } = request.params
const locale = await this._admin.options.locale(this.currentAdmin)
if (locale?.availableLanguages?.includes(lang)) {
const resource = this._admin.findResource('User')
if (resource && this?.currentAdmin?.userId) {
await resource.update(this.currentAdmin.userId, { language: lang })
}
request.session.adminUser.language = lang
}
}
return {
redirectUrl: this._admin.options.rootPath,
}
}
}

export default ApiController
5 changes: 5 additions & 0 deletions src/backend/utils/router/router.ts
Expand Up @@ -171,6 +171,11 @@ export const Router: RouterType = {
path: '/api/pages/{pageName}',
Controller: ApiController,
action: 'page',
}, {
method: 'POST',
path: '/api/language/{lang}',
Controller: ApiController,
action: 'switchLanguage',
}],
}

Expand Down
Expand Up @@ -9,17 +9,20 @@ import {
} from '@adminjs/design-system'
import React, { FC, useMemo } from 'react'
import { useTranslation } from '../../../hooks/index.js'
import ApiClient from '../../../utils/api-client.js'

const LanguageSelect: FC = () => {
const {
i18n: {
language,
options: { supportedLngs },
changeLanguage,
// changeLanguage,
},
translateComponent,
} = useTranslation()

const api = new ApiClient()

const availableLanguages: readonly string[] = useMemo(
() => (supportedLngs ? supportedLngs.filter((lang) => lang !== 'cimode') : []),
[supportedLngs],
Expand All @@ -42,8 +45,8 @@ const LanguageSelect: FC = () => {
{availableLanguages.map((lang) => (
<DropDownItem
key={lang}
onClick={() => {
changeLanguage(lang)
onClick={async () => {
window.location.href = await api.switchLanguage(lang)
}}
>
{translateComponent(`LanguageSelector.availableLanguages.${lang}`, { defaultValue: lang })}
Expand Down
6 changes: 6 additions & 0 deletions src/frontend/utils/api-client.ts
Expand Up @@ -251,6 +251,12 @@ class ApiClient {
return response
}

async switchLanguage(lang: string): Promise<string> {
const response = await this.client.post(`/api/language/${lang}`)
checkResponse(response)
return response.data.redirectUrl
}

async refreshToken(data: Record<string, any>) {
const response = await this.client.request({
url: '/refresh-token',
Expand Down

0 comments on commit e8ae87f

Please sign in to comment.