mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-03-27 02:51:18 -04:00
19 lines
661 B
TypeScript
19 lines
661 B
TypeScript
import {defaultLocale} from 'common/constants'
|
|
|
|
export function getTranslationMethod(locale: string | undefined, messages: Record<string, string>) {
|
|
return (key: string, fallback: string, formatter?: any): string => {
|
|
const result = locale === defaultLocale ? fallback : (messages[key] ?? fallback)
|
|
if (!formatter) return result
|
|
if (typeof formatter === 'function') return formatter(result)
|
|
if (typeof formatter === 'object') {
|
|
let text = String(result)
|
|
for (const [k, v] of Object.entries(formatter)) {
|
|
text = text.replace(new RegExp(`\\{${k}\\}`, 'g'), String(v))
|
|
}
|
|
return text
|
|
}
|
|
|
|
return result
|
|
}
|
|
}
|