All files / src/locales index.ts

90.32% Statements 28/31
77.77% Branches 14/18
100% Functions 6/6
96.55% Lines 28/29

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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86                    1x                   1x 1x 1x   1x 1x 2x 2x     1x           1x   2x     2x 2x   1x 1x   1x 1x 1x             2x             1x       3x 3x     3x     3x 1x       3x 1x     3x    
import enDefault from './en.default.json'
import frDefault from './fr.default.json'
import deDefault from './de.default.json'
import itDefault from './it.default.json'
import { getGlobalConfig, getEventConfig } from '../store'
import { deepMerge } from '../utils/merge'
import { EventConfig, DeepPartial } from '../types'
 
import { SupportedLocale } from '../types/locales'
 
export const defaultLocales: Record<SupportedLocale, any> = {
    en: enDefault,
    fr: frDefault,
    de: deDefault,
    it: itDefault,
}
 
/**
 * Sets a value in a nested object based on a dot-notated path string.
 */
const applyFlatOverride = (target: any, path: string, value: string) => {
    const parts = path.split('.')
    Iif (parts.length < 2) return
 
    const key = parts.pop()!
    const leaf = parts.reduce((acc, part) => {
        Iif (!acc[part]) acc[part] = {}
        return acc[part]
    }, target)
 
    leaf[key] = value
}
 
/**
 * Merges overrides and applies flat dot-notation keys.
 */
const processOverrides = (base: any, overrides: any) => {
    // 1. Deep merge to handle nested objects
    const result = deepMerge(base, overrides)
 
    // 2. Scan for dot-notation keys
    Object.keys(overrides).forEach((key) => {
        if (key.includes('.')) {
            // Top-level flat override (e.g. "en.auth.title")
            applyFlatOverride(result, key, String(overrides[key]))
        E} else if (typeof overrides[key] === 'object') {
            // Nested flat override (e.g. en: { "auth.title": "..." })
            const nested = overrides[key] as Record<string, string>
            Object.keys(nested).forEach((nestedKey) => {
                Iif (nestedKey.includes('.')) {
                    applyFlatOverride(result, `${key}.${nestedKey}`, String(nested[nestedKey]))
                }
            })
        }
    })
 
    return result
}
 
/**
 * Loads the application locales by merging static defaults with dynamic database overrides.
 * Supports both nested structures and flat-map overrides (e.g. { "en.donation.title": "..." }).
 */
export const loadLocales = (
    providedGlobalConfig?: DeepPartial<EventConfig> | null,
    providedEventConfig?: DeepPartial<EventConfig> | null,
): typeof defaultLocales => {
    const globalConfig = providedGlobalConfig ?? getGlobalConfig()
    const eventConfig = providedEventConfig ?? getEventConfig()
 
    // 1. Defaults
    let result = defaultLocales
 
    // 2. Global Overrides (Nested Structure + Flat Map)
    if (globalConfig?.locales?.overrides) {
        result = processOverrides(result, globalConfig.locales.overrides)
    }
 
    // 3. Event Overrides (Nested Structure + Flat Map)
    if (eventConfig?.locales?.overrides) {
        result = processOverrides(result, eventConfig.locales.overrides)
    }
 
    return result
}