UNPKG

alepha

Version:

Alepha is a convention-driven TypeScript framework for building robust, end-to-end type-safe applications, from serverless APIs to full-stack React apps.

115 lines (114 loc) 3.73 kB
import * as _alepha_core1 from "alepha"; import { Alepha, Descriptor, KIND } from "alepha"; import * as _alepha_logger0 from "alepha/logger"; import * as _alepha_server_cookies0 from "alepha/server/cookies"; import * as typebox0 from "typebox"; //#region src/hooks/useI18n.d.ts /** * Hook to access the i18n service. */ declare const useI18n: <S extends object, K extends keyof ServiceDictionary<S>>() => I18nProvider<S, K> & { tr(key: keyof ServiceDictionary<S>[K] | string, options?: { args?: string[]; default?: string; }): string; }; type ServiceDictionary<T extends object> = { [K in keyof T]: T[K] extends DictionaryDescriptor<infer U> ? U : never }; //#endregion //#region src/providers/I18nProvider.d.ts declare class I18nProvider<S extends object, K extends keyof ServiceDictionary<S>> { protected logger: _alepha_logger0.Logger; protected alepha: Alepha; protected cookie: _alepha_server_cookies0.AbstractCookieDescriptor<typebox0.TString>; readonly registry: Array<{ name: string; lang: string; loader: () => Promise<Record<string, string>>; translations: Record<string, string>; }>; options: { fallbackLang: string; }; numberFormat: { format: (value: number) => string; }; get languages(): string[]; protected readonly onRender: _alepha_core1.HookDescriptor<"server:onRequest">; protected readonly onStart: _alepha_core1.HookDescriptor<"start">; protected createFormatters(): void; setLang(lang: string): Promise<void>; protected readonly mutate: _alepha_core1.HookDescriptor<"state:mutate">; get lang(): string; translate: (key: string, args?: string[]) => string; readonly tr: (key: keyof ServiceDictionary<S>[K] | string, options?: { args?: string[]; default?: string; }) => string; protected render(item: string, args: string[]): string; } //#endregion //#region src/descriptors/$dictionary.d.ts /** * Register a dictionary entry for translations. * * It allows you to define a set of translations for a specific language. * Entry can be lazy-loaded, which is useful for large dictionaries or when translations are not needed immediately. * * @example * ```ts * import { $dictionary } from "alepha/react-i18n"; * * const Example = () => { * const { tr } = useI18n<App, "en">(); * return <div>{tr("hello")}</div>; // * } * * class App { * * en = $dictionary({ * // { default: { hello: "Hey" } } * lazy: () => import("./translations/en.ts"), * }); * * home = $page({ * path: "/", * component: Example, * }) * } * * run(App); * ``` */ declare const $dictionary: { <T extends Record<string, string>>(options: DictionaryDescriptorOptions<T>): DictionaryDescriptor<T>; [KIND]: typeof DictionaryDescriptor; }; interface DictionaryDescriptorOptions<T extends Record<string, string>> { lang?: string; name?: string; lazy: () => Promise<{ default: T; }>; } declare class DictionaryDescriptor<T extends Record<string, string>> extends Descriptor<DictionaryDescriptorOptions<T>> { protected provider: I18nProvider<object, never>; protected onInit(): void; } //#endregion //#region src/index.d.ts declare module "alepha" { interface State { "react.i18n.lang"?: string; } } /** * Add i18n support to your Alepha React application. SSR and CSR compatible. * * It supports lazy loading of translations and provides a context to access the current language. * * @module alepha.react.i18n */ declare const AlephaReactI18n: _alepha_core1.Service<_alepha_core1.Module>; //#endregion export { $dictionary, AlephaReactI18n, DictionaryDescriptor, DictionaryDescriptorOptions, I18nProvider, ServiceDictionary, useI18n }; //# sourceMappingURL=index.d.ts.map