fix(blocking): локализовать дефолтное сообщение на экране подписки на канал

This commit is contained in:
kewldan
2026-07-23 03:36:17 +03:00
parent f04675b249
commit 5e2e3214c9
3 changed files with 55 additions and 1 deletions

View File

@@ -0,0 +1,33 @@
import { describe, expect, it } from 'vitest';
import { customChannelMessage } from './channelBlockingMessage';
describe('customChannelMessage', () => {
it('returns null for empty values', () => {
expect(customChannelMessage(undefined)).toBeNull();
expect(customChannelMessage(null)).toBeNull();
expect(customChannelMessage('')).toBeNull();
expect(customChannelMessage(' ')).toBeNull();
});
it('returns null for the hardcoded backend default message', () => {
expect(
customChannelMessage('Please subscribe to the required channels to continue'),
).toBeNull();
});
it('ignores surrounding whitespace when matching the backend default', () => {
expect(
customChannelMessage(' Please subscribe to the required channels to continue '),
).toBeNull();
});
it('passes through a custom message', () => {
expect(customChannelMessage('Подпишитесь на наш канал @example')).toBe(
'Подпишитесь на наш канал @example',
);
});
it('trims a custom message', () => {
expect(customChannelMessage(' custom ')).toBe('custom');
});
});

View File

@@ -0,0 +1,18 @@
/**
* The backend hardcodes this English message in the 403
* `channel_subscription_required` payload (app/cabinet/dependencies.py). It is
* not admin-configurable and not localized, so showing it verbatim leaks
* English into every locale. Treat it as "no custom message" and let the UI
* fall back to its own i18n string.
*/
const BACKEND_DEFAULT_MESSAGES = new Set(['Please subscribe to the required channels to continue']);
/**
* Returns the backend-provided channel-subscription message only when it is a
* real custom message; `null` for empty values and known backend defaults.
*/
export function customChannelMessage(message: string | null | undefined): string | null {
const trimmed = message?.trim();
if (!trimmed || BACKEND_DEFAULT_MESSAGES.has(trimmed)) return null;
return trimmed;
}