next-rosetta
Version:
Next.js + Rosetta with native i18n support
65 lines (64 loc) • 2.95 kB
TypeScript
import React from "react";
import { Rosetta as RosettaBase } from "rosetta";
declare type Key = string | number | bigint | symbol;
/**
* @see https://github.com/microsoft/TypeScript/pull/40336
*/
declare type PropType<T, Path extends Key> = string extends Path ? unknown : Path extends keyof T ? T[Path] : Path extends `${infer K}.${infer R}` ? K extends keyof T ? PropType<T[K], R> : unknown : unknown;
declare type Join<T extends unknown[], D extends string> = T extends [] ? "" : T extends [string | number | boolean | bigint] ? `${T[0]}` : T extends [string | number | boolean | bigint, ...infer U] ? `${T[0]}${D}${Join<U, D>}` : string;
export interface RosettaExtended<T> extends Omit<RosettaBase<T>, "t"> {
/**
* Inter type from property path (note: using array as path won't infer type)
* @example <caption>Infer type</caption>
* const title = t("title");
* const text = t("landing.title");
* @example <caption>Force type.</caption>
* const text = t<string>(["landing", "title"]);
* const text = t<string>(["landing.feature", index, "description"]);
*/
t<P extends Key | Key[], X extends Record<string, any> | any[]>(key: P, params?: X, lang?: string): P extends Key[] ? PropType<T, Join<P, ".">> : P extends Key ? PropType<T, P> : unknown;
/**
* Force or overwrite type
* @example <caption>Infer type</caption>
* const title = t("title");
* const text = t("landing.title");
* @example <caption>Force type.</caption>
* const text = t<string>(["landing", "title"]);
* const text = t<string>(["landing.feature", index, "description"]);
*/
t<F extends any, X extends Record<string, any> | any[] = Record<string, any> | any[]>(key: Key | Key[], params?: X, lang?: string): F;
}
/**
* Use <I18nProvider /> instead of this internal context.
*/
export declare const I18nContext: React.Context<RosettaExtended<any> | null>;
/**
* @example <caption>Simple</caption>
* const { t } = useI18n()
* const text = t("title")
* @example <caption>With types</caption>
* interface LocaleTable { title: string; }
* const { t } = useI18n<LocaleTable>()
* const text = t("title")
*/
export declare function useI18n<T = any>(): RosettaExtended<T>;
/**
* @example
* import type { GetStaticProps } from "next";
* import type { I18nProps } from "next-rosetta";
* interface MyLocale { title: string }
* export const getStaticProps: GetStaticProps<I18nProps<MyLocale>> = async (context) => {
* // ...
* }
*/
export declare type I18nProps<T = any> = {
table: T;
};
export declare type I18nProviderProps<T = any> = I18nProps<T> & {
children?: any;
};
/**
* You probably want to add this at the root of your project. If you are using Next.js add it to `_app.tsx`.
*/
export declare function I18nProvider<T = any>({ table, children }: I18nProviderProps<T>): JSX.Element;
export {};