Skip to content

Commit

Permalink
context less version of Trans component (#1588)
Browse files Browse the repository at this point in the history
* context less version of Trans component

* some adjustements
  • Loading branch information
adrai committed Dec 6, 2022
1 parent 08576bf commit e1980e4
Show file tree
Hide file tree
Showing 16 changed files with 842 additions and 523 deletions.
33 changes: 33 additions & 0 deletions TransWithoutContext.d.ts
@@ -0,0 +1,33 @@
import { i18n, TFuncKey, Namespace, TypeOptions, TFunction, KeyPrefix } from 'i18next';
import * as React from 'react';

type DefaultNamespace = TypeOptions['defaultNS'];

type TransChild = React.ReactNode | Record<string, unknown>;
export type TransProps<
K extends TFuncKey<N, TKPrefix> extends infer A ? A : never,
N extends Namespace = DefaultNamespace,
TKPrefix = undefined,
E = React.HTMLProps<HTMLDivElement>
> = E & {
children?: TransChild | TransChild[];
components?: readonly React.ReactElement[] | { readonly [tagName: string]: React.ReactElement };
count?: number;
context?: string;
defaults?: string;
i18n?: i18n;
i18nKey?: K | K[];
ns?: N;
parent?: string | React.ComponentType<any> | null; // used in React.createElement if not null
tOptions?: {};
values?: {};
shouldUnescape?: boolean;
t?: TFunction<N, TKPrefix>;
};

export function Trans<
K extends TFuncKey<N, TKPrefix> extends infer A ? A : never,
N extends Namespace = DefaultNamespace,
TKPrefix extends KeyPrefix<N> = undefined,
E = React.HTMLProps<HTMLDivElement>
>(props: TransProps<K, N, TKPrefix, E>): React.ReactElement;
34 changes: 2 additions & 32 deletions index.d.ts
@@ -1,23 +1,22 @@
import i18next, {
ReactOptions,
i18n,
ThirdPartyModule,
Resource,
TFuncKey,
Namespace,
TypeOptions,
TFunction,
KeyPrefix,
} from 'i18next';
import * as React from 'react';
export { Trans, TransProps } from './TransWithoutContext';
export { initReactI18next } from './initReactI18next';

type Subtract<T extends K, K> = Omit<T, keyof K>;

export function setDefaults(options: ReactOptions): void;
export function getDefaults(): ReactOptions;
export function setI18n(instance: i18n): void;
export function getI18n(): i18n;
export const initReactI18next: ThirdPartyModule;
export function composeInitialProps(ForComponent: any): (ctx: unknown) => Promise<any>;
export function getInitialProps(): {
initialI18nStore: {
Expand Down Expand Up @@ -50,35 +49,6 @@ declare module 'react' {

type DefaultNamespace = TypeOptions['defaultNS'];

type TransChild = React.ReactNode | Record<string, unknown>;
export type TransProps<
K extends TFuncKey<N, TKPrefix> extends infer A ? A : never,
N extends Namespace = DefaultNamespace,
TKPrefix = undefined,
E = React.HTMLProps<HTMLDivElement>
> = E & {
children?: TransChild | TransChild[];
components?: readonly React.ReactElement[] | { readonly [tagName: string]: React.ReactElement };
count?: number;
context?: string;
defaults?: string;
i18n?: i18n;
i18nKey?: K | K[];
ns?: N;
parent?: string | React.ComponentType<any> | null; // used in React.createElement if not null
tOptions?: {};
values?: {};
shouldUnescape?: boolean;
t?: TFunction<N, TKPrefix>;
};

export function Trans<
K extends TFuncKey<N, TKPrefix> extends infer A ? A : never,
N extends Namespace = DefaultNamespace,
TKPrefix extends KeyPrefix<N> = undefined,
E = React.HTMLProps<HTMLDivElement>
>(props: TransProps<K, N, TKPrefix, E>): React.ReactElement;

export function useSSR(initialI18nStore: Resource, initialLanguage: string): void;

export interface UseTranslationOptions<TKPrefix = undefined> {
Expand Down
3 changes: 3 additions & 0 deletions initReactI18next.d.ts
@@ -0,0 +1,3 @@
import { ThirdPartyModule } from 'i18next';

export const initReactI18next: ThirdPartyModule;
18 changes: 18 additions & 0 deletions package.json
Expand Up @@ -17,6 +17,24 @@
"react",
"reactjs"
],
"exports": {
"./package.json": "./package.json",
".": {
"types": "./index.d.ts",
"import": "./dist/es/index.js",
"require": "./dist/commonjs/index.js"
},
"./TransWithoutContext": {
"types": "./TransWithoutContext.d.ts",
"import": "./dist/es/TransWithoutContext.js",
"require": "./dist/commonjs/TransWithoutContext.js"
},
"./initReactI18next": {
"types": "./initReactI18next.d.ts",
"import": "./dist/es/initReactI18next.js",
"require": "./dist/commonjs/initReactI18next.js"
}
},
"homepage": "https://github.com/i18next/react-i18next",
"bugs": "https://github.com/i18next/react-i18next/issues",
"repository": {
Expand Down

0 comments on commit e1980e4

Please sign in to comment.