import React, { ComponentType, ReactNode, ReactElement, ComponentPropsWithoutRef, ComponentPropsWithRef, ElementType, PropsWithChildren } from "react"; import { LangContext, LangContextProps, TextPropType } from "./context"; declare type TPrefix = { [P in keyof T as `t-${string & P}`]?: T[P] | [string]; }; declare type PolyRef = ComponentPropsWithRef["ref"]; declare type AsProperty = { as?: C; }; declare type PropsToOmit = keyof (AsProperty & P); declare type WithName = T & { displayName?: string | undefined; }; declare type PolyProp = PropsWithChildren> & Omit, PropsToOmit>; declare type PolyPropRef = PolyProp & { ref?: PolyRef; }; /** * Hook that gets the currently active translation context. Here's an example * of a component that wraps the `Intl.DateTimeFormat` API using the translation * context. * * ```typescript * const TDateFormat: ComponentType<{ date: Date }> = ({ date }) => { * // Get the context * const ctx = useTranslation(); * // Use context's languages stack to find a format for our locale * const dtf = new Intl.DateTimeFormat(ctx.languages); * // Find out which language was matched... * const { locale } = dtf.resolvedOptions(); * const ts = TString.literal(dtf.format(date), locale); * return ; * }; * ``` * * @returns the active translation context * @category Hooks */ export declare const useTranslation: () => LangContext; export declare type TranslateLocalProps = LangContextProps & { children: ReactNode; }; /** * Wrap components in a nested [[`LangContext`]]. Used to override settings in * the context. For example we can add an additional dictionary. * * ```typescript * const Miscount = ({ children }: { children: ReactNode }) => { * // pretend one is three * const dict = { $$dict: { one: { en: "three" } } }; * return {children}; * }; * ``` * @category Components */ export declare const TranslateLocal: ComponentType; declare type TranslateProps = PolyProp; declare type TranslateComponent = WithName<((props: TranslateProps) => ReactElement | null)>; /** * Wrap components in a nested [[`LangContext`]] that establishes a new * language stack. By default any children will be wrapped in a `div` with * a `lang=` property that indicates the language of the wrapped content. * * Within this context any content which can't be translated into the requested * languages will have it's own `lang=` property to reflect the fact that it * is in a different language than expected. * * ```typescript * // Renders as
....
* const Welsh: ComponentType<{ children: ReactNode }> = ({ children }) => ( * {children} * ); * * // Renders as
....
* const WelshSection: ComponentType<{ children: ReactNode }> = ({ children }) => ( * * {children} * * ); * ``` * * Unlike [[`TranslateLocal`]] `Translate` always wraps the translated * content in an element with a `lang=` property. * * @category Components */ export declare const Translate: TranslateComponent; declare type AsProps = PolyPropRef; declare type AsComponent = WithName<((props: AsProps) => ReactElement | null)>; export declare const As: AsComponent; declare type TTextProps = PolyPropRef; declare type TTextComponent = WithName<((props: TTextProps) => ReactElement | null)>; export declare const TText: TTextComponent; /** * Properties for the `` component. */ export declare type TProps = PolyPropRef & TPrefix>; declare type TComponent = WithName<((props: TProps) => ReactElement | null)>; /** * A wrapper for content that should be translated. It attempts to translate * the content you give it according to the active [[`LangContext`]]. It can * translate content looked up in the translation dictionary and fat strings * (or [[`TString`]]s). * * It can optionally perform template substitution on the translated text, * allowing child components to render portions of the translated text * with arbitrary wrappers. * * By default translated text is wrapped in a `span`. Render a different * element using the `as` property. * * If the wrapped content can't be translated into the context's preferred * language it will have a `lang=` property specifying its actual language. * * The simplest usage is to render translatable content without template * substition: * * ```typescript * // Render multilingual content * const hi = { en: "Hello", de: "Hallo", fr: "Bonjour" }; * return ; * // fr: Bonjour * ``` * * Template substitution allows you to build whole component trees from a * translated string: * * ```typescript * const info = { * en: "Here's a %1[useful link] and here's some %2[italic text]", * fr: "Voici %2[du texte en italique] et un %1[lien utile]", * de: "Hier ist ein %1[nützlicher Link] und hier ein %2[kursiver Text]" * }; * return ( * * * * * ); * // fr: * //
* // Voici du texte en italique et un lien utile * //
* ``` * * You can also look up and translate dictionary tags: * * ```typescript * // Same as the previous example if `info` is in the dictionary * return ( * * * * * ); * ``` * * See [Using T](/Interminimal/index.html#using-t) for more examples. * * @category Components */ export declare const T: TComponent; /** * Create a new component that behaves like `` but with a different default * `as` element. * * ```typescript * const Toption = tBind("option"); * // later * return * ``` * * It's also possible to wrap React components. * * ```typescript * const TImage = tBind(Image as FunctionComponent); * ``` * * The need for the cast is ugly. Not sure how to fix that. PRs welcome... * * The generated components are cached - so whenever you call `tBind("p")` you * will get the same component. * * @category Utilities */ export declare const tBind: >(as: C) => React.ComponentType>; export {};