Line data Source code
1 : import { t, te } from '@/i18n'
2 :
3 : // Shared i18n helpers for error catalog resolvers. These preserve the raw API
4 : // message/details as fallbacks when a catalog key is not available. Keep this
5 : // module folder-internal so UI code only consumes resolved display fields.
6 : export interface ErrorResolveContext {
7 : isCloud?: boolean
8 : nodeDisplayName?: string
9 : }
10 :
11 : export type CatalogParams = Record<string, string | number>
12 :
13 1962 : export function translateCatalogMessage(
14 : key: string,
15 : fallback: string,
16 : params?: CatalogParams
17 : ): string {
18 1962 : if (te(key))
19 1962 : return params ? t(key, params, { escapeParameter: false }) : t(key)
20 0 : if (!params) return fallback
21 :
22 0 : return fallback.replace(/\{(\w+)\}/g, (match, paramName) => {
23 0 : const value: unknown = params[paramName]
24 0 : return value === undefined ? match : String(value)
25 : })
26 : }
27 :
28 45 : export function translateOptionalCatalogMessage(
29 : key: string,
30 : fallback?: string,
31 : params?: CatalogParams
32 : ): string | undefined {
33 45 : if (te(key))
34 25 : return params ? t(key, params, { escapeParameter: false }) : t(key)
35 45 : return fallback?.trim() ? fallback : undefined
36 : }
37 :
38 62 : export function normalizeNodeName(nodeDisplayName: string | undefined): string {
39 62 : return (
40 : nodeDisplayName?.trim() ||
41 : translateCatalogMessage('errorCatalog.fallbacks.nodeName', 'This node')
42 : )
43 : }
|