mirror of
https://github.com/dbeaver/cloudbeaver.git
synced 2026-09-24 16:04:36 +08:00
feat(core-localization): CB-937 localization fallback
This commit is contained in:
@@ -57,7 +57,7 @@ export class LocalizationService extends Bootstrap {
|
||||
this.localeProviders.push(provider);
|
||||
}
|
||||
|
||||
readonly translate = <T extends TLocalizationToken | undefined>(token: T): T => {
|
||||
readonly translate = <T extends TLocalizationToken | undefined>(token: T, fallback?: T): T => {
|
||||
if (token === undefined) {
|
||||
return undefined as T;
|
||||
}
|
||||
@@ -75,6 +75,11 @@ export class LocalizationService extends Bootstrap {
|
||||
if (typeof translation === 'string') {
|
||||
return translation as T;
|
||||
}
|
||||
|
||||
if (fallback !== undefined) {
|
||||
return this.translate(fallback);
|
||||
}
|
||||
|
||||
return token;
|
||||
};
|
||||
|
||||
|
||||
@@ -13,10 +13,11 @@ import { useTranslate } from './useTranslate';
|
||||
|
||||
interface Props {
|
||||
token: TLocalizationToken;
|
||||
fallback?: TLocalizationToken;
|
||||
}
|
||||
|
||||
export const Translate: React.FC<Props> = observer(function Translate({ token }) {
|
||||
export const Translate: React.FC<Props> = observer(function Translate({ token, fallback }) {
|
||||
const translate = useTranslate();
|
||||
|
||||
return <>{translate(token)}</>;
|
||||
return <>{translate(token, fallback)}</>;
|
||||
});
|
||||
|
||||
@@ -11,7 +11,7 @@ import { useService } from '@cloudbeaver/core-di';
|
||||
import { LocalizationService } from './LocalizationService';
|
||||
import type { TLocalizationToken } from './TLocalizationToken';
|
||||
|
||||
export function useTranslate(): (<T extends TLocalizationToken | undefined>(token: T) => T) {
|
||||
export function useTranslate(): (<T extends TLocalizationToken | undefined>(token: T, fallback?: T) => T) {
|
||||
const localizationService = useService(LocalizationService);
|
||||
|
||||
return localizationService.translate;
|
||||
|
||||
Reference in New Issue
Block a user