@helpwave/hightide
Version:
helpwave's component and theming library
59 lines (56 loc) • 2.18 kB
text/typescript
import { Language } from './util.mjs';
/**
* A type describing the pluralization of a word
*/
type TranslationPlural = {
zero?: string;
one?: string;
two?: string;
few?: string;
many?: string;
other: string;
};
/**
* The type describing all values of a translation
*/
type TranslationType = Record<string, string | TranslationPlural>;
/**
* The type of translations
*/
type Translation<T extends TranslationType> = Record<Language, T>;
type OverwriteTranslationType<T extends TranslationType> = {
language?: Language;
translation?: Translation<Partial<T>>;
};
/**
* Adds the `language` prop to the component props.
*
* @param Translation the type of the translation object
*
* @param Props the type of the component props, defaults to `Record<string, never>`,
* if you don't expect any other props other than `language` and get an
* error when using your component (because it uses `forwardRef` etc.)
* you can try out `Record<string, unknown>`, this might resolve your
* problem as `SomeType & never` is still `never` but `SomeType & unknown`
* is `SomeType` which means that adding back props (like `ref` etc.)
* works properly
*/
type PropsForTranslation<Translation extends TranslationType, Props = unknown> = Props & {
overwriteTranslation?: OverwriteTranslationType<Translation>;
};
type StringKeys<T> = Extract<keyof T, string>;
type TranslationFunctionOptions = {
replacements?: Record<string, string>;
count?: number;
};
type TranslationFunction<T extends TranslationType> = (key: StringKeys<T>, options?: TranslationFunctionOptions) => string;
declare const TranslationPluralCount: {
zero: number;
one: number;
two: number;
few: number;
many: number;
other: number;
};
declare const useTranslation: <T extends TranslationType>(translations: Translation<Partial<TranslationType>>[], overwriteTranslation?: OverwriteTranslationType<T>) => TranslationFunction<T>;
export { type PropsForTranslation, type Translation, type TranslationPlural, TranslationPluralCount, type TranslationType, useTranslation };