Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 219x 7x 7x 7x 77x 77x 1x 1x 77x 77x 8x | import { loadLocales, SupportedLocale, SUPPORTED_LOCALES } from '@fundraising/white-labeling'
export class I18nUtil {
/**
* Helper to resolve a nested key in a locale object.
*/
private static resolveKey(obj: any, path: string): string | undefined {
return path.split('.').reduce((prev, curr) => prev && prev[curr], obj)
}
/**
* Loads the effective locale data by merging defaults with overrides using the shared library logic.
*/
static getEffectiveLocaleData(locale: string, overrides?: Record<string, any>): any {
// We simulate a config object to leverage loadLocales
// loadLocales merges nested overrides and handles flat keys
const effectiveLocales = loadLocales(null, { locales: { overrides } })
const targetLocale = (
SUPPORTED_LOCALES.includes(locale as SupportedLocale) ? locale : 'en'
) as SupportedLocale
return effectiveLocales[targetLocale]
}
/**
* Translate a key using the provided locale data.
*/
static t(data: any, key: string, params: Record<string, string | number> = {}): string {
let value = I18nUtil.resolveKey(data, key)
if (value === undefined) {
// Fallback to English if not found in target locale
const enValue = I18nUtil.resolveKey(loadLocales().en, key)
value = enValue !== undefined ? enValue : key
}
Iif (typeof value !== 'string') {
return key
}
// Replace params: {{param}}
return value.replace(/{{(\w+)}}/g, (_, paramKey) => {
return params[paramKey] !== undefined ? String(params[paramKey]) : `{{${paramKey}}}`
})
}
}
|