@koine/next 1.0.15 → 1.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Auth/helpers.d.ts +2 -2
- package/Auth/useLogin.js +1 -1
- package/Auth/useLoginUrl.js +1 -1
- package/Auth/useLogout.js +1 -1
- package/Forms/useForm.d.ts +3 -9
- package/Forms/useForm.js +1 -3
- package/i18n/T/index.d.ts +8 -0
- package/i18n/T/index.js +6 -0
- package/i18n/T/package.json +6 -0
- package/i18n/getT/index.d.ts +3 -0
- package/i18n/getT/index.js +9 -0
- package/i18n/getT/package.json +6 -0
- package/i18n/index.d.ts +6 -0
- package/i18n/index.js +6 -0
- package/{I18n → i18n}/package.json +1 -1
- package/i18n/translationAsOptions/index.d.ts +3 -0
- package/i18n/translationAsOptions/index.js +9 -0
- package/i18n/translationAsOptions/package.json +6 -0
- package/i18n/useDateFormat/index.d.ts +6 -0
- package/i18n/useDateFormat/index.js +37 -0
- package/i18n/useDateFormat/package.json +6 -0
- package/i18n/useLocale/index.d.ts +1 -0
- package/i18n/useLocale/index.js +4 -0
- package/i18n/useLocale/package.json +6 -0
- package/i18n/useT/index.d.ts +50 -0
- package/i18n/useT/index.js +115 -0
- package/i18n/useT/package.json +6 -0
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/node/Auth/useLogin.js +2 -2
- package/node/Auth/useLoginUrl.js +2 -2
- package/node/Auth/useLogout.js +2 -2
- package/node/Forms/useForm.js +1 -3
- package/node/i18n/T/index.js +10 -0
- package/node/i18n/getT/index.js +13 -0
- package/node/i18n/index.js +9 -0
- package/node/i18n/translationAsOptions/index.js +13 -0
- package/node/i18n/useDateFormat/index.js +41 -0
- package/node/i18n/useLocale/index.js +9 -0
- package/node/i18n/useT/index.js +120 -0
- package/node/index.js +1 -1
- package/package.json +3 -3
- package/typings.d.ts +19 -10
- package/I18n/I18n.d.ts +0 -44
- package/I18n/I18n.js +0 -95
- package/I18n/index.d.ts +0 -1
- package/I18n/index.js +0 -1
- package/node/I18n/I18n.js +0 -102
- package/node/I18n/index.js +0 -4
package/Auth/helpers.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { TranslateLoose } from "../i18n";
|
|
2
2
|
/**
|
|
3
3
|
* @see next/auth `pages` mapping: https://next-auth.js.org/configuration/pages`
|
|
4
4
|
*/
|
|
@@ -10,7 +10,7 @@ export declare type AuthRoutesMap = {
|
|
|
10
10
|
secured?: RegExp[];
|
|
11
11
|
};
|
|
12
12
|
export declare const AUTH_ROUTES: AuthRoutesMap;
|
|
13
|
-
export declare function getAuthRoutes(t:
|
|
13
|
+
export declare function getAuthRoutes(t: TranslateLoose): AuthRoutesMap;
|
|
14
14
|
/**
|
|
15
15
|
* @param url e.g. "http://localhost:3000/signin?callbackUrl=http://localhost:3000/profile"
|
|
16
16
|
*/
|
package/Auth/useLogin.js
CHANGED
|
@@ -3,7 +3,7 @@ import { useState, useCallback } from "react";
|
|
|
3
3
|
import { useRouter } from "next/router";
|
|
4
4
|
import { signIn } from "next-auth/react";
|
|
5
5
|
import { parseURL } from "@koine/utils";
|
|
6
|
-
import { useT } from "../
|
|
6
|
+
import { useT } from "../i18n";
|
|
7
7
|
import { getAuthRoutes, getCallbackUrl } from "./helpers";
|
|
8
8
|
export function useLogin() {
|
|
9
9
|
var t = useT();
|
package/Auth/useLoginUrl.js
CHANGED
package/Auth/useLogout.js
CHANGED
|
@@ -2,7 +2,7 @@ import { useState, useCallback } from "react";
|
|
|
2
2
|
import { useRouter } from "next/router";
|
|
3
3
|
import { signOut } from "next-auth/react";
|
|
4
4
|
import { parseURL } from "@koine/utils";
|
|
5
|
-
import { useT } from "../
|
|
5
|
+
import { useT } from "../i18n";
|
|
6
6
|
import { getAuthRoutes, getCallbackUrl } from "./helpers";
|
|
7
7
|
export function useLogout() {
|
|
8
8
|
var t = useT();
|
package/Forms/useForm.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { UseFormProps, FieldValues } from "react-hook-form";
|
|
2
2
|
import type { ObjectSchema } from "yup";
|
|
3
|
-
import {
|
|
4
|
-
export declare function useForm<T extends FieldValues>(schema: ObjectSchema<any>,
|
|
3
|
+
import { type TranslateLoose } from "../i18n";
|
|
4
|
+
export declare function useForm<T extends FieldValues>(schema: ObjectSchema<any>, t: TranslateLoose, formProps?: UseFormProps, debug?: boolean): {
|
|
5
5
|
watch: import("react-hook-form").UseFormWatch<T>;
|
|
6
6
|
getValues: import("react-hook-form").UseFormGetValues<T>;
|
|
7
7
|
getFieldState: import("react-hook-form").UseFormGetFieldState<T>;
|
|
@@ -21,12 +21,6 @@ export declare function useForm<T extends FieldValues>(schema: ObjectSchema<any>
|
|
|
21
21
|
control: import("react-hook-form").Control<T, any>;
|
|
22
22
|
register: import("react-hook-form").UseFormRegister<T>;
|
|
23
23
|
setValue: import("react-hook-form").UseFormSetValue<T>;
|
|
24
|
-
t:
|
|
25
|
-
[key: string]: string | number | boolean;
|
|
26
|
-
} | null | undefined, o?: "obj" | {
|
|
27
|
-
returnObjects?: boolean | undefined;
|
|
28
|
-
fallback?: string | string[] | undefined;
|
|
29
|
-
default?: string | undefined;
|
|
30
|
-
} | undefined) => R;
|
|
24
|
+
t: TranslateLoose;
|
|
31
25
|
};
|
|
32
26
|
};
|
package/Forms/useForm.js
CHANGED
|
@@ -2,12 +2,10 @@ import { __assign } from "tslib";
|
|
|
2
2
|
import { useMemo } from "react";
|
|
3
3
|
import { useForm as _useForm } from "react-hook-form";
|
|
4
4
|
import { yupResolver as resolver } from "@hookform/resolvers/yup";
|
|
5
|
-
import { useT } from "../I18n";
|
|
6
5
|
export function useForm(
|
|
7
6
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
8
|
-
schema,
|
|
7
|
+
schema, t, formProps, debug) {
|
|
9
8
|
if (formProps === void 0) { formProps = {}; }
|
|
10
|
-
var t = useT(i18nNamespace);
|
|
11
9
|
// const form = _useForm<InferType<ObjectSchema<T, object>>>({
|
|
12
10
|
var form = _useForm(__assign({
|
|
13
11
|
// @ts-expect-error FIXME:
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import type { TransProps } from "next-translate";
|
|
3
|
+
import type { TranslationsPaths, TranslationsAllPaths } from "../useT";
|
|
4
|
+
export declare type TProps<TNamespace extends keyof Koine.NextTranslations | undefined = undefined> = Omit<TransProps, "i18nKey" | "ns"> & {
|
|
5
|
+
i18nKey: TNamespace extends keyof Koine.NextTranslations ? TranslationsPaths<Koine.NextTranslations[TNamespace]> : TranslationsAllPaths;
|
|
6
|
+
ns?: TNamespace;
|
|
7
|
+
};
|
|
8
|
+
export declare function T(props: TProps): JSX.Element;
|
package/i18n/T/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unnecessary-type-constraint */
|
|
2
|
+
/**
|
|
3
|
+
* @file
|
|
4
|
+
*
|
|
5
|
+
* About the typescript support for translation strings @see:
|
|
6
|
+
* - https://github.com/vinissimus/next-translate/issues/721
|
|
7
|
+
*/
|
|
8
|
+
import getTranslation from "next-translate/getT";
|
|
9
|
+
export var getT = getTranslation;
|
package/i18n/index.d.ts
ADDED
package/i18n/index.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { __assign } from "tslib";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import { useRouter } from "next/router";
|
|
4
|
+
import format from "date-fns/format";
|
|
5
|
+
import { useDateLocale } from "@koine/react";
|
|
6
|
+
/**
|
|
7
|
+
* Automatically returns the `date-fns/format` function with the right locale
|
|
8
|
+
* passed as option (grabbed from next router value).
|
|
9
|
+
*/
|
|
10
|
+
export var useDateFormat = function () {
|
|
11
|
+
var _a = useState(function () {
|
|
12
|
+
return function () {
|
|
13
|
+
var args = [];
|
|
14
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
15
|
+
args[_i] = arguments[_i];
|
|
16
|
+
}
|
|
17
|
+
return format.apply(void 0, args);
|
|
18
|
+
};
|
|
19
|
+
}), formatter = _a[0], setFormatter = _a[1];
|
|
20
|
+
var router = useRouter();
|
|
21
|
+
var locale = useDateLocale(router.locale);
|
|
22
|
+
useEffect(function () {
|
|
23
|
+
if (locale) {
|
|
24
|
+
var newFormatter_1 = function (date, _format, options) { return format(date, _format, __assign(__assign({}, (options || {})), { locale: locale })); };
|
|
25
|
+
setFormatter(function () {
|
|
26
|
+
return function () {
|
|
27
|
+
var args = [];
|
|
28
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
29
|
+
args[_i] = arguments[_i];
|
|
30
|
+
}
|
|
31
|
+
return newFormatter_1.apply(void 0, args);
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}, [locale]);
|
|
36
|
+
return formatter;
|
|
37
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useLocale(): string;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export declare type TranslateNamespace = Extract<keyof Koine.NextTranslations, string>;
|
|
2
|
+
declare type Join<S1, S2> = S1 extends string ? S2 extends string ? `${S1}.${S2}` : S1 : never;
|
|
3
|
+
export declare type TranslationsPaths<T, TAddRoot extends true | undefined = undefined> = {
|
|
4
|
+
[K in keyof T]: T[K] extends Record<string, unknown> ? K extends string ? TAddRoot extends true ? `${K}` | Join<K, TranslationsPaths<T[K]>> : Join<K, TranslationsPaths<T[K]>> : K : K;
|
|
5
|
+
}[keyof T];
|
|
6
|
+
export declare type TranslationsAllPaths = {
|
|
7
|
+
[N in Extract<keyof Koine.NextTranslations, string>]: {
|
|
8
|
+
[K in Extract<keyof Koine.NextTranslations[N], string>]: Koine.NextTranslations[N][K] extends Array<Record<string, unknown>> ? `${N}:${K}` : Koine.NextTranslations[N][K] extends Record<string, unknown> ? Join<K extends string ? `${N}:${K}` : `${N}:`, TranslationsPaths<Koine.NextTranslations[N][K], true>> : Koine.NextTranslations[N][K] extends Array<string | number | boolean> ? `${N}:${K}` : Koine.NextTranslations[N][K] extends string | number | boolean ? K extends string ? `${N}:${K}` : `${N}:` : ``;
|
|
9
|
+
}[Extract<keyof Koine.NextTranslations[N], string>];
|
|
10
|
+
}[Extract<keyof Koine.NextTranslations, string>];
|
|
11
|
+
/**
|
|
12
|
+
* Passing just `obj` is a shortcut for `{ returnObjects: true }`
|
|
13
|
+
*/
|
|
14
|
+
export declare type TranslationQuery = undefined | null | "obj" | {
|
|
15
|
+
[key: string]: string | number | boolean;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Passing just `obj` is a shortcut for `{ returnObjects: true }`
|
|
19
|
+
*/
|
|
20
|
+
export declare type TranslationOptions = undefined | "obj" | {
|
|
21
|
+
returnObjects?: boolean;
|
|
22
|
+
fallback?: string | string[];
|
|
23
|
+
default?: string;
|
|
24
|
+
};
|
|
25
|
+
export declare type TranslateLoose = (s: any, // s: string,
|
|
26
|
+
q?: any, // q?: TranslationQuery,
|
|
27
|
+
o?: any) => string;
|
|
28
|
+
export declare type Translate<TNamespace extends keyof Koine.NextTranslations | undefined> = TNamespace extends keyof Koine.NextTranslations ? TranslateNamespaced<TNamespace> : TranslateDefault;
|
|
29
|
+
export declare type TranslateDefault = <TReturn extends unknown = string>(s: TranslationsAllPaths, q?: TranslationQuery, o?: TranslationOptions) => TReturn;
|
|
30
|
+
export declare type TranslateNamespaced<TNamespace extends keyof Koine.NextTranslations> = <TReturn extends unknown = string>(s: TranslationsPaths<Koine.NextTranslations[TNamespace]>, q?: TranslationQuery, o?: TranslationOptions) => TReturn;
|
|
31
|
+
/**
|
|
32
|
+
* Wrap `next-translate` useTranslations adding the `obj` second argument shortcut
|
|
33
|
+
* and type safety
|
|
34
|
+
*
|
|
35
|
+
* @see https://github.com/vinissimus/next-translate/issues/513#issuecomment-779826418
|
|
36
|
+
*/
|
|
37
|
+
export declare function useT(): TranslateDefault;
|
|
38
|
+
export declare function useT<TNamespace extends TranslateNamespace>(namespace: TNamespace): TranslateNamespaced<TNamespace>;
|
|
39
|
+
export {};
|
|
40
|
+
/**
|
|
41
|
+
* Using this as generic instead of the interface on the namespace `Koine.NextTranslations`
|
|
42
|
+
* generates the typescript warning `Type instantiation is excessively deep and possibly infinite.`
|
|
43
|
+
* Its usage is left here and commented out. Maybe we can restore it once we find
|
|
44
|
+
* the time to look for a way to avoid the warning
|
|
45
|
+
*/
|
|
46
|
+
/**
|
|
47
|
+
* Instead of wrapping the `useTranslation` hook from [`next-translate`](https://github.com/vinissimus/next-translate/blob/master/src/useTranslation.tsx)
|
|
48
|
+
* we might just rewrite it with our custom implementation but the `_context`
|
|
49
|
+
* to use is not exposed by the library...
|
|
50
|
+
*/
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unnecessary-type-constraint */
|
|
2
|
+
/**
|
|
3
|
+
* @file
|
|
4
|
+
*
|
|
5
|
+
* About the typescript support for translation strings @see:
|
|
6
|
+
* - https://github.com/vinissimus/next-translate/issues/721
|
|
7
|
+
*/
|
|
8
|
+
import { useMemo } from "react";
|
|
9
|
+
import useTranslation from "next-translate/useTranslation";
|
|
10
|
+
export function useT(namespace) {
|
|
11
|
+
var t = useTranslation().t;
|
|
12
|
+
var tMemoized = useMemo(function () {
|
|
13
|
+
return function (s, q, o) {
|
|
14
|
+
return t(
|
|
15
|
+
// @ts-expect-error again...
|
|
16
|
+
(namespace ? "".concat(namespace, ":").concat(s) : s), q === "obj" ? null : q, q === "obj" || o === "obj" ? { returnObjects: true } : o
|
|
17
|
+
// ) as TReturn extends (undefined | never | unknown) ? TranslateReturn<TranslationQuery, TranslationOptions> : TReturn;
|
|
18
|
+
);
|
|
19
|
+
};
|
|
20
|
+
}, [t, namespace]);
|
|
21
|
+
return tMemoized;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Using this as generic instead of the interface on the namespace `Koine.NextTranslations`
|
|
25
|
+
* generates the typescript warning `Type instantiation is excessively deep and possibly infinite.`
|
|
26
|
+
* Its usage is left here and commented out. Maybe we can restore it once we find
|
|
27
|
+
* the time to look for a way to avoid the warning
|
|
28
|
+
*/
|
|
29
|
+
// type Translations = { [key: string]: Record<string, unknown> };
|
|
30
|
+
// export type TranslationsAllPaths<TTranslations extends Translations> = {
|
|
31
|
+
// [N in Extract<keyof TTranslations, string>]: {
|
|
32
|
+
// [K in Extract<
|
|
33
|
+
// keyof TTranslations[N],
|
|
34
|
+
// string
|
|
35
|
+
// >]: TTranslations[N][K] extends Array<
|
|
36
|
+
// // if we have an array of objects
|
|
37
|
+
// Record<string, unknown>
|
|
38
|
+
// >
|
|
39
|
+
// ? Join<
|
|
40
|
+
// K extends string ? `${N}:${K}` : `${N}:`,
|
|
41
|
+
// Paths<TTranslations[N][K], true>
|
|
42
|
+
// >
|
|
43
|
+
// : // if we have an object
|
|
44
|
+
// TTranslations[N][K] extends Record<string, unknown>
|
|
45
|
+
// ? Join<
|
|
46
|
+
// K extends string ? `${N}:${K}` : `${N}:`,
|
|
47
|
+
// Paths<TTranslations[N][K], true>
|
|
48
|
+
// > /* &
|
|
49
|
+
// Record<TTranslations[N][K], object> */
|
|
50
|
+
// : // if we have an array of primitives
|
|
51
|
+
// TTranslations[N][K] extends Array<string | number | boolean>
|
|
52
|
+
// ? Join<
|
|
53
|
+
// K extends string ? `${N}:${K}` : `${N}`,
|
|
54
|
+
// Paths<TTranslations[N][K], true>
|
|
55
|
+
// >
|
|
56
|
+
// : // if we have a primitve string/number/boolean
|
|
57
|
+
// TTranslations[N][K] extends string | number | boolean
|
|
58
|
+
// ? K extends string
|
|
59
|
+
// ? `${N}:${K}`
|
|
60
|
+
// : `${N}:`
|
|
61
|
+
// : ``;
|
|
62
|
+
// }[Extract<keyof TTranslations[N], string>];
|
|
63
|
+
// }[Extract<keyof TTranslations, string>];
|
|
64
|
+
// export type Translate<
|
|
65
|
+
// TTranslations extends Translations = Koine.NextTranslations,
|
|
66
|
+
// TNamespace extends undefined | keyof TTranslations = undefined
|
|
67
|
+
// > = <TReturn extends unknown = string>(
|
|
68
|
+
// s: TNamespace extends keyof TTranslations
|
|
69
|
+
// ? Paths<TTranslations[TNamespace]>
|
|
70
|
+
// : TranslationsAllPaths<TTranslations>,
|
|
71
|
+
// q?: TranslationQuery,
|
|
72
|
+
// o?: TranslationOptions
|
|
73
|
+
// ) => TReturn;
|
|
74
|
+
// export function useT<
|
|
75
|
+
// TTranslations extends Translations = Koine.NextTranslations,
|
|
76
|
+
// TNamespace extends keyof TTranslations | undefined = undefined
|
|
77
|
+
// >(namespace?: TNamespace) {
|
|
78
|
+
// const t = useTranslation().t;
|
|
79
|
+
// const tMemoized = useMemo(
|
|
80
|
+
// () =>
|
|
81
|
+
// <TReturn extends unknown = string>(
|
|
82
|
+
// s: TNamespace extends keyof TTranslations
|
|
83
|
+
// ? Paths<TTranslations[TNamespace], true>
|
|
84
|
+
// : TranslationsAllPaths<TTranslations>,
|
|
85
|
+
// q?: TranslationQuery,
|
|
86
|
+
// o?: TranslationOptions
|
|
87
|
+
// ) =>
|
|
88
|
+
// t(
|
|
89
|
+
// namespace ? `${namespace}:${s}` : s,
|
|
90
|
+
// q === "obj" ? null : q,
|
|
91
|
+
// q === "obj" || o === "obj" ? { returnObjects: true } : o
|
|
92
|
+
// // ) as TReturn extends (undefined | never | unknown) ? TranslateReturn<TranslationQuery, TranslationOptions> : TReturn;
|
|
93
|
+
// ) as TReturn,
|
|
94
|
+
// [t, namespace]
|
|
95
|
+
// );
|
|
96
|
+
// return tMemoized;
|
|
97
|
+
// }
|
|
98
|
+
/**
|
|
99
|
+
* Instead of wrapping the `useTranslation` hook from [`next-translate`](https://github.com/vinissimus/next-translate/blob/master/src/useTranslation.tsx)
|
|
100
|
+
* we might just rewrite it with our custom implementation but the `_context`
|
|
101
|
+
* to use is not exposed by the library...
|
|
102
|
+
*/
|
|
103
|
+
// import { useContext, useMemo } from "react"
|
|
104
|
+
// import wrapTWithDefaultNs from "next-translate/lib/cjs/wrapTWithDefaultNs";
|
|
105
|
+
// import I18nContext from "next-translate/lib/cjs/_context";
|
|
106
|
+
// export default function useTranslation(defaultNS?: string): I18n {
|
|
107
|
+
// const ctx = useContext(I18nContext)
|
|
108
|
+
// return useMemo(
|
|
109
|
+
// () => ({
|
|
110
|
+
// ...ctx,
|
|
111
|
+
// t: wrapTWithDefaultNs(ctx.t, defaultNS),
|
|
112
|
+
// }),
|
|
113
|
+
// [ctx.lang, defaultNS]
|
|
114
|
+
// )
|
|
115
|
+
// }
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/node/Auth/useLogin.js
CHANGED
|
@@ -6,10 +6,10 @@ var react_1 = require("react");
|
|
|
6
6
|
var router_1 = require("next/router");
|
|
7
7
|
var react_2 = require("next-auth/react");
|
|
8
8
|
var utils_1 = require("@koine/utils");
|
|
9
|
-
var
|
|
9
|
+
var i18n_1 = require("../i18n");
|
|
10
10
|
var helpers_1 = require("./helpers");
|
|
11
11
|
function useLogin() {
|
|
12
|
-
var t = (0,
|
|
12
|
+
var t = (0, i18n_1.useT)();
|
|
13
13
|
var push = (0, router_1.useRouter)().push;
|
|
14
14
|
var _a = (0, react_1.useState)(false), loading = _a[0], setLoading = _a[1];
|
|
15
15
|
var _b = (0, react_1.useState)(false), ok = _b[0], setOk = _b[1];
|
package/node/Auth/useLoginUrl.js
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useLoginUrl = void 0;
|
|
4
4
|
var react_1 = require("react");
|
|
5
|
-
var
|
|
5
|
+
var i18n_1 = require("../i18n");
|
|
6
6
|
var helpers_1 = require("./helpers");
|
|
7
7
|
function useLoginUrl() {
|
|
8
8
|
var _a = (0, react_1.useState)(""), currentUrl = _a[0], setCurrentUrl = _a[1];
|
|
9
|
-
var t = (0,
|
|
9
|
+
var t = (0, i18n_1.useT)();
|
|
10
10
|
(0, react_1.useEffect)(function () {
|
|
11
11
|
setCurrentUrl("?callbackUrl=".concat(window.location.href));
|
|
12
12
|
}, []);
|
package/node/Auth/useLogout.js
CHANGED
|
@@ -5,10 +5,10 @@ var react_1 = require("react");
|
|
|
5
5
|
var router_1 = require("next/router");
|
|
6
6
|
var react_2 = require("next-auth/react");
|
|
7
7
|
var utils_1 = require("@koine/utils");
|
|
8
|
-
var
|
|
8
|
+
var i18n_1 = require("../i18n");
|
|
9
9
|
var helpers_1 = require("./helpers");
|
|
10
10
|
function useLogout() {
|
|
11
|
-
var t = (0,
|
|
11
|
+
var t = (0, i18n_1.useT)();
|
|
12
12
|
var push = (0, router_1.useRouter)().push;
|
|
13
13
|
var _a = (0, react_1.useState)(false), loading = _a[0], setLoading = _a[1];
|
|
14
14
|
var _b = (0, react_1.useState)(false), ok = _b[0], setOk = _b[1];
|
package/node/Forms/useForm.js
CHANGED
|
@@ -5,12 +5,10 @@ var tslib_1 = require("tslib");
|
|
|
5
5
|
var react_1 = require("react");
|
|
6
6
|
var react_hook_form_1 = require("react-hook-form");
|
|
7
7
|
var yup_1 = require("@hookform/resolvers/yup");
|
|
8
|
-
var I18n_1 = require("../I18n");
|
|
9
8
|
function useForm(
|
|
10
9
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
|
-
schema,
|
|
10
|
+
schema, t, formProps, debug) {
|
|
12
11
|
if (formProps === void 0) { formProps = {}; }
|
|
13
|
-
var t = (0, I18n_1.useT)(i18nNamespace);
|
|
14
12
|
// const form = _useForm<InferType<ObjectSchema<T, object>>>({
|
|
15
13
|
var form = (0, react_hook_form_1.useForm)(tslib_1.__assign({
|
|
16
14
|
// @ts-expect-error FIXME:
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.T = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
+
var Trans_1 = tslib_1.__importDefault(require("next-translate/Trans"));
|
|
7
|
+
function T(props) {
|
|
8
|
+
return (0, jsx_runtime_1.jsx)(Trans_1.default, tslib_1.__assign({}, props));
|
|
9
|
+
}
|
|
10
|
+
exports.T = T;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-unnecessary-type-constraint */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.getT = void 0;
|
|
5
|
+
var tslib_1 = require("tslib");
|
|
6
|
+
/**
|
|
7
|
+
* @file
|
|
8
|
+
*
|
|
9
|
+
* About the typescript support for translation strings @see:
|
|
10
|
+
* - https://github.com/vinissimus/next-translate/issues/721
|
|
11
|
+
*/
|
|
12
|
+
var getT_1 = tslib_1.__importDefault(require("next-translate/getT"));
|
|
13
|
+
exports.getT = getT_1.default;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./getT"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./T"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./translationAsOptions"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./useDateFormat"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./useLocale"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./useT"), exports);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.translationAsOptions = void 0;
|
|
4
|
+
function translationAsOptions(t, i18nKey) {
|
|
5
|
+
var dictionary = t(i18nKey, undefined, {
|
|
6
|
+
returnObjects: true,
|
|
7
|
+
});
|
|
8
|
+
return Object.keys(dictionary).map(function (key) { return ({
|
|
9
|
+
value: key,
|
|
10
|
+
label: dictionary[key],
|
|
11
|
+
}); });
|
|
12
|
+
}
|
|
13
|
+
exports.translationAsOptions = translationAsOptions;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useDateFormat = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var react_1 = require("react");
|
|
6
|
+
var router_1 = require("next/router");
|
|
7
|
+
var format_1 = tslib_1.__importDefault(require("date-fns/format"));
|
|
8
|
+
var react_2 = require("@koine/react");
|
|
9
|
+
/**
|
|
10
|
+
* Automatically returns the `date-fns/format` function with the right locale
|
|
11
|
+
* passed as option (grabbed from next router value).
|
|
12
|
+
*/
|
|
13
|
+
var useDateFormat = function () {
|
|
14
|
+
var _a = (0, react_1.useState)(function () {
|
|
15
|
+
return function () {
|
|
16
|
+
var args = [];
|
|
17
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
18
|
+
args[_i] = arguments[_i];
|
|
19
|
+
}
|
|
20
|
+
return format_1.default.apply(void 0, args);
|
|
21
|
+
};
|
|
22
|
+
}), formatter = _a[0], setFormatter = _a[1];
|
|
23
|
+
var router = (0, router_1.useRouter)();
|
|
24
|
+
var locale = (0, react_2.useDateLocale)(router.locale);
|
|
25
|
+
(0, react_1.useEffect)(function () {
|
|
26
|
+
if (locale) {
|
|
27
|
+
var newFormatter_1 = function (date, _format, options) { return (0, format_1.default)(date, _format, tslib_1.__assign(tslib_1.__assign({}, (options || {})), { locale: locale })); };
|
|
28
|
+
setFormatter(function () {
|
|
29
|
+
return function () {
|
|
30
|
+
var args = [];
|
|
31
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
32
|
+
args[_i] = arguments[_i];
|
|
33
|
+
}
|
|
34
|
+
return newFormatter_1.apply(void 0, args);
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}, [locale]);
|
|
39
|
+
return formatter;
|
|
40
|
+
};
|
|
41
|
+
exports.useDateFormat = useDateFormat;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useLocale = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var useTranslation_1 = tslib_1.__importDefault(require("next-translate/useTranslation"));
|
|
6
|
+
function useLocale() {
|
|
7
|
+
return (0, useTranslation_1.default)().lang;
|
|
8
|
+
}
|
|
9
|
+
exports.useLocale = useLocale;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-unnecessary-type-constraint */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.useT = void 0;
|
|
5
|
+
var tslib_1 = require("tslib");
|
|
6
|
+
/**
|
|
7
|
+
* @file
|
|
8
|
+
*
|
|
9
|
+
* About the typescript support for translation strings @see:
|
|
10
|
+
* - https://github.com/vinissimus/next-translate/issues/721
|
|
11
|
+
*/
|
|
12
|
+
var react_1 = require("react");
|
|
13
|
+
var useTranslation_1 = tslib_1.__importDefault(require("next-translate/useTranslation"));
|
|
14
|
+
function useT(namespace) {
|
|
15
|
+
var t = (0, useTranslation_1.default)().t;
|
|
16
|
+
var tMemoized = (0, react_1.useMemo)(function () {
|
|
17
|
+
return function (s, q, o) {
|
|
18
|
+
return t(
|
|
19
|
+
// @ts-expect-error again...
|
|
20
|
+
(namespace ? "".concat(namespace, ":").concat(s) : s), q === "obj" ? null : q, q === "obj" || o === "obj" ? { returnObjects: true } : o
|
|
21
|
+
// ) as TReturn extends (undefined | never | unknown) ? TranslateReturn<TranslationQuery, TranslationOptions> : TReturn;
|
|
22
|
+
);
|
|
23
|
+
};
|
|
24
|
+
}, [t, namespace]);
|
|
25
|
+
return tMemoized;
|
|
26
|
+
}
|
|
27
|
+
exports.useT = useT;
|
|
28
|
+
/**
|
|
29
|
+
* Using this as generic instead of the interface on the namespace `Koine.NextTranslations`
|
|
30
|
+
* generates the typescript warning `Type instantiation is excessively deep and possibly infinite.`
|
|
31
|
+
* Its usage is left here and commented out. Maybe we can restore it once we find
|
|
32
|
+
* the time to look for a way to avoid the warning
|
|
33
|
+
*/
|
|
34
|
+
// type Translations = { [key: string]: Record<string, unknown> };
|
|
35
|
+
// export type TranslationsAllPaths<TTranslations extends Translations> = {
|
|
36
|
+
// [N in Extract<keyof TTranslations, string>]: {
|
|
37
|
+
// [K in Extract<
|
|
38
|
+
// keyof TTranslations[N],
|
|
39
|
+
// string
|
|
40
|
+
// >]: TTranslations[N][K] extends Array<
|
|
41
|
+
// // if we have an array of objects
|
|
42
|
+
// Record<string, unknown>
|
|
43
|
+
// >
|
|
44
|
+
// ? Join<
|
|
45
|
+
// K extends string ? `${N}:${K}` : `${N}:`,
|
|
46
|
+
// Paths<TTranslations[N][K], true>
|
|
47
|
+
// >
|
|
48
|
+
// : // if we have an object
|
|
49
|
+
// TTranslations[N][K] extends Record<string, unknown>
|
|
50
|
+
// ? Join<
|
|
51
|
+
// K extends string ? `${N}:${K}` : `${N}:`,
|
|
52
|
+
// Paths<TTranslations[N][K], true>
|
|
53
|
+
// > /* &
|
|
54
|
+
// Record<TTranslations[N][K], object> */
|
|
55
|
+
// : // if we have an array of primitives
|
|
56
|
+
// TTranslations[N][K] extends Array<string | number | boolean>
|
|
57
|
+
// ? Join<
|
|
58
|
+
// K extends string ? `${N}:${K}` : `${N}`,
|
|
59
|
+
// Paths<TTranslations[N][K], true>
|
|
60
|
+
// >
|
|
61
|
+
// : // if we have a primitve string/number/boolean
|
|
62
|
+
// TTranslations[N][K] extends string | number | boolean
|
|
63
|
+
// ? K extends string
|
|
64
|
+
// ? `${N}:${K}`
|
|
65
|
+
// : `${N}:`
|
|
66
|
+
// : ``;
|
|
67
|
+
// }[Extract<keyof TTranslations[N], string>];
|
|
68
|
+
// }[Extract<keyof TTranslations, string>];
|
|
69
|
+
// export type Translate<
|
|
70
|
+
// TTranslations extends Translations = Koine.NextTranslations,
|
|
71
|
+
// TNamespace extends undefined | keyof TTranslations = undefined
|
|
72
|
+
// > = <TReturn extends unknown = string>(
|
|
73
|
+
// s: TNamespace extends keyof TTranslations
|
|
74
|
+
// ? Paths<TTranslations[TNamespace]>
|
|
75
|
+
// : TranslationsAllPaths<TTranslations>,
|
|
76
|
+
// q?: TranslationQuery,
|
|
77
|
+
// o?: TranslationOptions
|
|
78
|
+
// ) => TReturn;
|
|
79
|
+
// export function useT<
|
|
80
|
+
// TTranslations extends Translations = Koine.NextTranslations,
|
|
81
|
+
// TNamespace extends keyof TTranslations | undefined = undefined
|
|
82
|
+
// >(namespace?: TNamespace) {
|
|
83
|
+
// const t = useTranslation().t;
|
|
84
|
+
// const tMemoized = useMemo(
|
|
85
|
+
// () =>
|
|
86
|
+
// <TReturn extends unknown = string>(
|
|
87
|
+
// s: TNamespace extends keyof TTranslations
|
|
88
|
+
// ? Paths<TTranslations[TNamespace], true>
|
|
89
|
+
// : TranslationsAllPaths<TTranslations>,
|
|
90
|
+
// q?: TranslationQuery,
|
|
91
|
+
// o?: TranslationOptions
|
|
92
|
+
// ) =>
|
|
93
|
+
// t(
|
|
94
|
+
// namespace ? `${namespace}:${s}` : s,
|
|
95
|
+
// q === "obj" ? null : q,
|
|
96
|
+
// q === "obj" || o === "obj" ? { returnObjects: true } : o
|
|
97
|
+
// // ) as TReturn extends (undefined | never | unknown) ? TranslateReturn<TranslationQuery, TranslationOptions> : TReturn;
|
|
98
|
+
// ) as TReturn,
|
|
99
|
+
// [t, namespace]
|
|
100
|
+
// );
|
|
101
|
+
// return tMemoized;
|
|
102
|
+
// }
|
|
103
|
+
/**
|
|
104
|
+
* Instead of wrapping the `useTranslation` hook from [`next-translate`](https://github.com/vinissimus/next-translate/blob/master/src/useTranslation.tsx)
|
|
105
|
+
* we might just rewrite it with our custom implementation but the `_context`
|
|
106
|
+
* to use is not exposed by the library...
|
|
107
|
+
*/
|
|
108
|
+
// import { useContext, useMemo } from "react"
|
|
109
|
+
// import wrapTWithDefaultNs from "next-translate/lib/cjs/wrapTWithDefaultNs";
|
|
110
|
+
// import I18nContext from "next-translate/lib/cjs/_context";
|
|
111
|
+
// export default function useTranslation(defaultNS?: string): I18n {
|
|
112
|
+
// const ctx = useContext(I18nContext)
|
|
113
|
+
// return useMemo(
|
|
114
|
+
// () => ({
|
|
115
|
+
// ...ctx,
|
|
116
|
+
// t: wrapTWithDefaultNs(ctx.t, defaultNS),
|
|
117
|
+
// }),
|
|
118
|
+
// [ctx.lang, defaultNS]
|
|
119
|
+
// )
|
|
120
|
+
// }
|
package/node/index.js
CHANGED
|
@@ -6,7 +6,7 @@ tslib_1.__exportStar(require("./Auth"), exports);
|
|
|
6
6
|
tslib_1.__exportStar(require("./Favicon"), exports);
|
|
7
7
|
tslib_1.__exportStar(require("./Forms"), exports);
|
|
8
8
|
tslib_1.__exportStar(require("./Head"), exports);
|
|
9
|
-
tslib_1.__exportStar(require("./
|
|
9
|
+
tslib_1.__exportStar(require("./i18n"), exports);
|
|
10
10
|
tslib_1.__exportStar(require("./Img"), exports);
|
|
11
11
|
tslib_1.__exportStar(require("./Link"), exports);
|
|
12
12
|
tslib_1.__exportStar(require("./NextProgress"), exports);
|
package/package.json
CHANGED
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"react": "^16.8 || ^17 || ^18",
|
|
8
8
|
"next": "^12.1.6",
|
|
9
|
-
"@koine/utils": "1.0.
|
|
9
|
+
"@koine/utils": "1.0.18",
|
|
10
10
|
"framer-motion": "^6.3.4",
|
|
11
|
-
"@koine/react": "1.0.
|
|
11
|
+
"@koine/react": "1.0.18",
|
|
12
12
|
"styled-components": "^5.3.5",
|
|
13
13
|
"@mui/base": "^5.0.0-alpha.82",
|
|
14
14
|
"react-icons": "^4.3.1",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"next-seo": "^5.4.0"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {},
|
|
32
|
-
"version": "1.0.
|
|
32
|
+
"version": "1.0.18",
|
|
33
33
|
"module": "./index.js",
|
|
34
34
|
"types": "./index.d.ts"
|
|
35
35
|
}
|
package/typings.d.ts
CHANGED
|
@@ -59,28 +59,37 @@ declare namespace Koine {
|
|
|
59
59
|
};
|
|
60
60
|
|
|
61
61
|
/**
|
|
62
|
-
* Translations dictionary extracted from JSON files.
|
|
63
|
-
* type with something like:
|
|
62
|
+
* Translations dictionary extracted from JSON files.
|
|
63
|
+
* You need to augment this type with something like:
|
|
64
64
|
*
|
|
65
65
|
* ```ts
|
|
66
66
|
* declare namespace Koine {
|
|
67
67
|
* interface NextTranslations {
|
|
68
68
|
* "~": typeof import("./locales/en/~.json");
|
|
69
|
-
* _: typeof import("./locales/en/_.json");
|
|
70
|
-
* $team: typeof import("./locales/en/$team.json");
|
|
71
|
-
* home: typeof import("./locales/en/home.json");
|
|
72
|
-
*
|
|
73
|
-
* Header: typeof import("./locales/en/Header.json");
|
|
69
|
+
* "_": typeof import("./locales/en/_.json");
|
|
70
|
+
* "$team": typeof import("./locales/en/$team.json");
|
|
71
|
+
* "home": typeof import("./locales/en/home.json");
|
|
72
|
+
* "Header": typeof import("./locales/en/Header.json");
|
|
74
73
|
* }
|
|
75
74
|
* }
|
|
76
75
|
* ```
|
|
76
|
+
*
|
|
77
|
+
* Best to follow a convention to name the files which become the namespaces:
|
|
78
|
+
*
|
|
79
|
+
* - `~`: for app wide **urls** translated definitions
|
|
80
|
+
* - `_`: for app wide **common** translations
|
|
81
|
+
* - `${data}`: dollar prefix for static **data** like arrays, objects, .etc
|
|
82
|
+
* - `{route-name}`: lower cased for **route** specific data
|
|
83
|
+
* - `{ComponentName}`: pascal cased for **components** specific data
|
|
77
84
|
*/
|
|
78
85
|
interface NextTranslations {
|
|
79
|
-
/** Convention for app wide `common` translations */
|
|
80
|
-
_: Record<string, string>;
|
|
81
|
-
/** Convention for app wide `routes` translated definitions */
|
|
82
86
|
"~": Record<string, string>;
|
|
87
|
+
_: Record<string, string>;
|
|
83
88
|
}
|
|
89
|
+
// interface NextTranslations {
|
|
90
|
+
// "~": Record<string, string>;
|
|
91
|
+
// _: Record<string, string>;
|
|
92
|
+
// }
|
|
84
93
|
}
|
|
85
94
|
|
|
86
95
|
/**
|
package/I18n/I18n.d.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/// <reference types="@koine/next/typings" />
|
|
2
|
-
import format from "date-fns/format";
|
|
3
|
-
import type { Option } from "@koine/react";
|
|
4
|
-
export { default as T } from "next-translate/Trans";
|
|
5
|
-
declare type Join<S1, S2> = S1 extends string ? S2 extends string ? `${S1}.${S2}` : never : never;
|
|
6
|
-
declare type Paths<T> = {
|
|
7
|
-
[K in keyof T]: T[K] extends Record<string, unknown> ? Join<K, Paths<T[K]>> : K;
|
|
8
|
-
}[keyof T];
|
|
9
|
-
export declare type AllPaths = {
|
|
10
|
-
[N in keyof Koine.NextTranslations]: {
|
|
11
|
-
[K in keyof Koine.NextTranslations[N]]: Koine.NextTranslations[N][K] extends Array<Record<string, unknown>> ? Join<K extends string ? `${N}:${K}` : `${N}:`, Paths<Koine.NextTranslations[N][K]>> : Koine.NextTranslations[N][K] extends Record<string, unknown> ? Join<K extends string ? `${N}:${K}` : `${N}:`, Paths<Koine.NextTranslations[N][K]>> & Record<Koine.NextTranslations[N][K], object> : Koine.NextTranslations[N][K] extends Array<string | number | boolean> ? Join<K extends string ? `${N}:${K}` : `${N}`, Paths<Koine.NextTranslations[N][K]>> : Koine.NextTranslations[N][K] extends string | number | boolean ? K extends string ? `${N}:${K}` : `${N}:` : ``;
|
|
12
|
-
}[keyof Koine.NextTranslations[N]];
|
|
13
|
-
}[keyof Koine.NextTranslations];
|
|
14
|
-
/**
|
|
15
|
-
* Passing just `obj` is a shortcut for `{ returnObjects: true }`
|
|
16
|
-
*/
|
|
17
|
-
declare type TranslationQuery = undefined | null | "obj" | {
|
|
18
|
-
[key: string]: string | number | boolean;
|
|
19
|
-
};
|
|
20
|
-
/**
|
|
21
|
-
* Passing just `obj` is a shortcut for `{ returnObjects: true }`
|
|
22
|
-
*/
|
|
23
|
-
declare type TranslationOptions = undefined | "obj" | {
|
|
24
|
-
returnObjects?: boolean;
|
|
25
|
-
fallback?: string | string[];
|
|
26
|
-
default?: string;
|
|
27
|
-
};
|
|
28
|
-
export declare type TranslateNamespace = keyof Koine.NextTranslations;
|
|
29
|
-
export declare type TranslateKey = AllPaths;
|
|
30
|
-
export declare type Translate<N extends undefined | keyof Koine.NextTranslations = undefined> = <R extends unknown = string>(s: N extends TranslateNamespace ? Paths<Koine.NextTranslations[N]> | AllPaths : AllPaths, q?: TranslationQuery, o?: TranslationOptions) => R;
|
|
31
|
-
export declare type TranslateLoose = (s: any, q?: any, o?: any) => string;
|
|
32
|
-
/**
|
|
33
|
-
* FIXME: the use of `useMemo` is a hopefully-temporary solution to fix the FOUC
|
|
34
|
-
* problem of untranslated text during page transitions
|
|
35
|
-
*
|
|
36
|
-
* @see https://github.com/vinissimus/next-translate/issues/513#issuecomment-779826418
|
|
37
|
-
*/
|
|
38
|
-
export declare function useT<N extends TranslateNamespace | undefined>(namespace?: N): <R extends unknown = string>(s: N extends TranslateNamespace ? Paths<Koine.NextTranslations[N]> | AllPaths : AllPaths, q?: TranslationQuery, o?: TranslationOptions) => R;
|
|
39
|
-
export declare function translationAsOptions(t: TranslateLoose, i18nKey: string): Option[];
|
|
40
|
-
/**
|
|
41
|
-
* Automatically returns the `date-fns/format` function with the right locale
|
|
42
|
-
* passed as option (grabbed from next router value).
|
|
43
|
-
*/
|
|
44
|
-
export declare const useDateFormat: () => typeof format;
|
package/I18n/I18n.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
/// <reference types="@koine/next/typings" />
|
|
2
|
-
import { __assign } from "tslib";
|
|
3
|
-
/* eslint-disable @typescript-eslint/no-unnecessary-type-constraint */
|
|
4
|
-
/**
|
|
5
|
-
* @file
|
|
6
|
-
*
|
|
7
|
-
* About the typescript support for translation strings @see:
|
|
8
|
-
* - https://github.com/vinissimus/next-translate/issues/721
|
|
9
|
-
*/
|
|
10
|
-
import { useEffect, useState, useMemo } from "react";
|
|
11
|
-
import { useRouter } from "next/router";
|
|
12
|
-
import format from "date-fns/format";
|
|
13
|
-
import useTranslation from "next-translate/useTranslation";
|
|
14
|
-
import { useDateLocale } from "@koine/react";
|
|
15
|
-
export { default as T } from "next-translate/Trans";
|
|
16
|
-
/**
|
|
17
|
-
* FIXME: the use of `useMemo` is a hopefully-temporary solution to fix the FOUC
|
|
18
|
-
* problem of untranslated text during page transitions
|
|
19
|
-
*
|
|
20
|
-
* @see https://github.com/vinissimus/next-translate/issues/513#issuecomment-779826418
|
|
21
|
-
*/
|
|
22
|
-
export function useT(namespace) {
|
|
23
|
-
var t = useTranslation().t;
|
|
24
|
-
// const typedT = <
|
|
25
|
-
// // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-constraint
|
|
26
|
-
// R extends unknown = string,
|
|
27
|
-
// // Q extends TranslationQuery = TranslationQuery
|
|
28
|
-
// // O extends TranslationOptions = TranslationOptions
|
|
29
|
-
// >(
|
|
30
|
-
// s: N extends TranslateNamespace
|
|
31
|
-
// ? Paths<Koine.NextTranslations[N]> | AllPaths
|
|
32
|
-
// : AllPaths,
|
|
33
|
-
// q?: TranslationQuery,
|
|
34
|
-
// o?: TranslationOptions
|
|
35
|
-
// ) =>
|
|
36
|
-
// // eslint-disable-next-line
|
|
37
|
-
// t.call(
|
|
38
|
-
// null,
|
|
39
|
-
// namespace ? `${namespace}:${s}` : s,
|
|
40
|
-
// q === "obj" ? null : q,
|
|
41
|
-
// q === "obj" || o === "obj" ? { returnObjects: true } : o
|
|
42
|
-
// // ) as R extends (undefined | never | unknown) ? TranslateReturn<TranslationQuery, TranslationOptions> : R;
|
|
43
|
-
// ) as R;
|
|
44
|
-
// return typedT;
|
|
45
|
-
var tMemoized = useMemo(function () {
|
|
46
|
-
return function (s, q, o) {
|
|
47
|
-
return t(namespace ? "".concat(namespace, ":").concat(s) : s, q === "obj" ? null : q, q === "obj" || o === "obj" ? { returnObjects: true } : o
|
|
48
|
-
// ) as R extends (undefined | never | unknown) ? TranslateReturn<TranslationQuery, TranslationOptions> : R;
|
|
49
|
-
);
|
|
50
|
-
};
|
|
51
|
-
}, [t, namespace]);
|
|
52
|
-
// return tMemoized as Translate<N>;
|
|
53
|
-
return tMemoized;
|
|
54
|
-
}
|
|
55
|
-
export function translationAsOptions(t, i18nKey) {
|
|
56
|
-
var dictionary = t(i18nKey, undefined, {
|
|
57
|
-
returnObjects: true,
|
|
58
|
-
});
|
|
59
|
-
return Object.keys(dictionary).map(function (key) { return ({
|
|
60
|
-
value: key,
|
|
61
|
-
label: dictionary[key],
|
|
62
|
-
}); });
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Automatically returns the `date-fns/format` function with the right locale
|
|
66
|
-
* passed as option (grabbed from next router value).
|
|
67
|
-
*/
|
|
68
|
-
export var useDateFormat = function () {
|
|
69
|
-
var _a = useState(function () {
|
|
70
|
-
return function () {
|
|
71
|
-
var args = [];
|
|
72
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
73
|
-
args[_i] = arguments[_i];
|
|
74
|
-
}
|
|
75
|
-
return format.apply(void 0, args);
|
|
76
|
-
};
|
|
77
|
-
}), formatter = _a[0], setFormatter = _a[1];
|
|
78
|
-
var router = useRouter();
|
|
79
|
-
var locale = useDateLocale(router.locale);
|
|
80
|
-
useEffect(function () {
|
|
81
|
-
if (locale) {
|
|
82
|
-
var newFormatter_1 = function (date, _format, options) { return format(date, _format, __assign(__assign({}, (options || {})), { locale: locale })); };
|
|
83
|
-
setFormatter(function () {
|
|
84
|
-
return function () {
|
|
85
|
-
var args = [];
|
|
86
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
87
|
-
args[_i] = arguments[_i];
|
|
88
|
-
}
|
|
89
|
-
return newFormatter_1.apply(void 0, args);
|
|
90
|
-
};
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
}, [locale]);
|
|
94
|
-
return formatter;
|
|
95
|
-
};
|
package/I18n/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./I18n";
|
package/I18n/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./I18n";
|
package/node/I18n/I18n.js
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/// <reference types="@koine/next/typings" />
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.useDateFormat = exports.translationAsOptions = exports.useT = exports.T = void 0;
|
|
5
|
-
var tslib_1 = require("tslib");
|
|
6
|
-
/* eslint-disable @typescript-eslint/no-unnecessary-type-constraint */
|
|
7
|
-
/**
|
|
8
|
-
* @file
|
|
9
|
-
*
|
|
10
|
-
* About the typescript support for translation strings @see:
|
|
11
|
-
* - https://github.com/vinissimus/next-translate/issues/721
|
|
12
|
-
*/
|
|
13
|
-
var react_1 = require("react");
|
|
14
|
-
var router_1 = require("next/router");
|
|
15
|
-
var format_1 = tslib_1.__importDefault(require("date-fns/format"));
|
|
16
|
-
var useTranslation_1 = tslib_1.__importDefault(require("next-translate/useTranslation"));
|
|
17
|
-
var react_2 = require("@koine/react");
|
|
18
|
-
var Trans_1 = require("next-translate/Trans");
|
|
19
|
-
Object.defineProperty(exports, "T", { enumerable: true, get: function () { return tslib_1.__importDefault(Trans_1).default; } });
|
|
20
|
-
/**
|
|
21
|
-
* FIXME: the use of `useMemo` is a hopefully-temporary solution to fix the FOUC
|
|
22
|
-
* problem of untranslated text during page transitions
|
|
23
|
-
*
|
|
24
|
-
* @see https://github.com/vinissimus/next-translate/issues/513#issuecomment-779826418
|
|
25
|
-
*/
|
|
26
|
-
function useT(namespace) {
|
|
27
|
-
var t = (0, useTranslation_1.default)().t;
|
|
28
|
-
// const typedT = <
|
|
29
|
-
// // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-constraint
|
|
30
|
-
// R extends unknown = string,
|
|
31
|
-
// // Q extends TranslationQuery = TranslationQuery
|
|
32
|
-
// // O extends TranslationOptions = TranslationOptions
|
|
33
|
-
// >(
|
|
34
|
-
// s: N extends TranslateNamespace
|
|
35
|
-
// ? Paths<Koine.NextTranslations[N]> | AllPaths
|
|
36
|
-
// : AllPaths,
|
|
37
|
-
// q?: TranslationQuery,
|
|
38
|
-
// o?: TranslationOptions
|
|
39
|
-
// ) =>
|
|
40
|
-
// // eslint-disable-next-line
|
|
41
|
-
// t.call(
|
|
42
|
-
// null,
|
|
43
|
-
// namespace ? `${namespace}:${s}` : s,
|
|
44
|
-
// q === "obj" ? null : q,
|
|
45
|
-
// q === "obj" || o === "obj" ? { returnObjects: true } : o
|
|
46
|
-
// // ) as R extends (undefined | never | unknown) ? TranslateReturn<TranslationQuery, TranslationOptions> : R;
|
|
47
|
-
// ) as R;
|
|
48
|
-
// return typedT;
|
|
49
|
-
var tMemoized = (0, react_1.useMemo)(function () {
|
|
50
|
-
return function (s, q, o) {
|
|
51
|
-
return t(namespace ? "".concat(namespace, ":").concat(s) : s, q === "obj" ? null : q, q === "obj" || o === "obj" ? { returnObjects: true } : o
|
|
52
|
-
// ) as R extends (undefined | never | unknown) ? TranslateReturn<TranslationQuery, TranslationOptions> : R;
|
|
53
|
-
);
|
|
54
|
-
};
|
|
55
|
-
}, [t, namespace]);
|
|
56
|
-
// return tMemoized as Translate<N>;
|
|
57
|
-
return tMemoized;
|
|
58
|
-
}
|
|
59
|
-
exports.useT = useT;
|
|
60
|
-
function translationAsOptions(t, i18nKey) {
|
|
61
|
-
var dictionary = t(i18nKey, undefined, {
|
|
62
|
-
returnObjects: true,
|
|
63
|
-
});
|
|
64
|
-
return Object.keys(dictionary).map(function (key) { return ({
|
|
65
|
-
value: key,
|
|
66
|
-
label: dictionary[key],
|
|
67
|
-
}); });
|
|
68
|
-
}
|
|
69
|
-
exports.translationAsOptions = translationAsOptions;
|
|
70
|
-
/**
|
|
71
|
-
* Automatically returns the `date-fns/format` function with the right locale
|
|
72
|
-
* passed as option (grabbed from next router value).
|
|
73
|
-
*/
|
|
74
|
-
var useDateFormat = function () {
|
|
75
|
-
var _a = (0, react_1.useState)(function () {
|
|
76
|
-
return function () {
|
|
77
|
-
var args = [];
|
|
78
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
79
|
-
args[_i] = arguments[_i];
|
|
80
|
-
}
|
|
81
|
-
return format_1.default.apply(void 0, args);
|
|
82
|
-
};
|
|
83
|
-
}), formatter = _a[0], setFormatter = _a[1];
|
|
84
|
-
var router = (0, router_1.useRouter)();
|
|
85
|
-
var locale = (0, react_2.useDateLocale)(router.locale);
|
|
86
|
-
(0, react_1.useEffect)(function () {
|
|
87
|
-
if (locale) {
|
|
88
|
-
var newFormatter_1 = function (date, _format, options) { return (0, format_1.default)(date, _format, tslib_1.__assign(tslib_1.__assign({}, (options || {})), { locale: locale })); };
|
|
89
|
-
setFormatter(function () {
|
|
90
|
-
return function () {
|
|
91
|
-
var args = [];
|
|
92
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
93
|
-
args[_i] = arguments[_i];
|
|
94
|
-
}
|
|
95
|
-
return newFormatter_1.apply(void 0, args);
|
|
96
|
-
};
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
}, [locale]);
|
|
100
|
-
return formatter;
|
|
101
|
-
};
|
|
102
|
-
exports.useDateFormat = useDateFormat;
|
package/node/I18n/index.js
DELETED