Dictionaries
Create a Definition of terms used for translations.
Instantiate
TS
function useDictionary<T>(terms: [string, T | Translator<T>][]): UseDictionary<T>;
function useDictionary<T>(terms: Record<string, T | Translator<T>>): UseDictionary<T>;
TS
type Translator<T, K = string> = (value: K) => T;
Returns
TS
type UseDictionary<T, K = string> = UseDefinitionMap<T | Translator<T>> & {
/**
* Translate a key. If no key is found, use the fallback.
*/
translate(key: K): T;
translate(key: K, fallback: T): T;
translate(key: K, fallback?: T): T;
};
Usage
ts
import { useDictionary } from 'flipclock';
const { define, translate } = useDictionary({
foo: 'bar'
});
console.log(translate('foo')); // 'bar'
console.log(translate('bar')); // 'bar
console.log(translate('bar', 'foo')); // 'foo'
define('hello', 'hola');
console.log(translate('hello')); // 'hola'
TIP
These are just a few examples and far from complete. If you want to see a feature-complete example, check tests/helpers/dictionary.test.ts
in the repo.