All files / blong-browser/src/hooks useText.ts

0% Statements 0/33
0% Branches 0/1
0% Functions 0/1
0% Lines 0/33

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                                                                   
/**
 * useText — i18n text lookup.
 */
import {useCallback} from 'react';
import {useAppStore} from '../state/appStore.js';

/**
 * Returns translated text for the given key.
 * Falls back to the key itself if no translation is found.
 *
 * @param id - Translation key (e.g. 'buttons.save') or raw English text
 * @param params - Interpolation parameters (e.g. {name: 'Alice'})
 */
export function useText(id: string, params?: Record<string, string | number>): string {
    const translations = useAppStore(s => s.translations);
    let text = translations[id] ?? id;
    if (params) {
        for (const [key, value] of Object.entries(params)) {
            text = text.replace(new RegExp(`\\{${key}\\}`, 'g'), String(value));
        }
    }
    return text;
}

/**
 * useTranslate — returns a stable `t(text)` function for use outside JSX.
 * The returned function looks up the text in the translations store,
 * falling back to the original text.
 */
export function useTranslate(): (text: string) => string {
    const translations = useAppStore(s => s.translations);
    return useCallback((text: string) => translations[text] ?? text, [translations]);
}