scrabble-solver
Version:
Scrabble Solver 2 - Free, open-source, cross-platform, multi-language analysis tool for Scrabble, Scrabble Duel, Super Scrabble, Letter League, Crossplay, Literaki, and Kelimelik. Quickly find the top-scoring words using the given board and tiles.
33 lines (25 loc) • 888 B
text/typescript
import { useCallback } from 'react';
import type { Translate } from '@/types';
import { selectLocale, selectTranslations } from './settings';
import { useTypedSelector } from './useTypedSelector';
export const useTranslate = (): Translate => {
const translations = useTypedSelector(selectTranslations);
const locale = useTypedSelector(selectLocale);
const translate: Translate = useCallback(
(id, replacements) => {
const translation = translations[id];
if (typeof translation === 'undefined') {
throw new Error(`Untranslated key "${id}" in locale "${locale}"`);
}
if (!replacements) {
return translation;
}
return Object.entries(replacements).reduce(
(result, [key, value]) => result.replaceAll(`{{${key}}}`, value),
translation,
);
},
[translations, locale],
);
return translate;
};