UNPKG

scrabble-solver

Version:

Scrabble Solver 2 - Free, open-source, cross-platform, multi-language analysis tool for Scrabble, Scrabble Duel, Super Scrabble, Letter League, Literaki, and Kelimelik. Quickly find the top-scoring words using the given board and tiles.

33 lines (25 loc) 882 B
import { useCallback } from 'react'; import { Translate } from 'types'; import { selectLocale, selectTranslations } from './selectors'; 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; };