Files
ipal-kit/src/modules/integrations/texts.ts
T

43 lines
2.0 KiB
TypeScript

import type { AdminText } from '../admin-i18n/define.js'
import type { AdminTextKey } from '../admin-i18n/texts/index.js'
import { adminTexts } from '../admin-i18n/texts/index.js'
/** A label as Payload takes it: one string, or one string per language. */
export type IntegrationLabel = AdminText | Record<string, string> | string
/** The label in `language`, falling back to English, then to any language. */
export function resolveLabel(label: IntegrationLabel | undefined, language: string | undefined): string {
if (!label) {return ''}
if (typeof label === 'string') {return label}
const map = label as Record<string, string>
return map[language ?? 'en'] ?? map.en ?? Object.values(map)[0] ?? ''
}
/** Status codes whose message has its own text — shown in the editor's language. */
export const STATUS_CODE_TEXTS: Record<string, AdminTextKey> = {
'bad-response': 'statusTurnstileBadResponse',
'graph-large-files': 'statusEmailGraphLargeFiles',
'graph-unavailable': 'statusEmailGraphUnavailable',
'internal-error': 'statusTurnstileInternalError',
'invalid-input-secret': 'statusTurnstileInvalidSecret',
'missing-input-secret': 'statusTurnstileMissingSecret',
'missing-secret': 'statusTurnstileMissingSecret',
'network-error': 'statusTurnstileNetworkError',
}
type I18nLike = { language?: string; t?: (key: never, vars?: Record<string, unknown>) => string } | undefined
/**
* One plugin text on the server: through `i18n.t` when Payload's i18n is there
* (so a project's own translation wins), else the plugin's English text.
*/
export function serverText(i18n: I18nLike, key: AdminTextKey, vars: Record<string, unknown> = {}): string {
if (i18n?.t) {
const out = i18n.t(`ipal:${key}` as never, vars)
if (out && out !== `ipal:${key}` && out !== key) {return out}
}
const lang = i18n?.language === 'pl' ? 'pl' : 'en'
return adminTexts[lang][key].replace(/\{\{(\w+)\}\}/g, (_, name: string) => String((vars[name] as number | string | undefined) ?? ''))
}