@nerimity/solid-i18lite 1.5.8 → 1.6.0
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/README.md +1 -1
- package/dist/{index.mjs → browser.mjs} +9 -8
- package/dist/browser.mjs.map +1 -0
- package/dist/ssr.js +2 -0
- package/dist/ssr.js.map +1 -0
- package/dist/ssr.mjs +82 -0
- package/dist/ssr.mjs.map +1 -0
- package/dist/types/Trans.d.ts +1 -1
- package/dist/types/TransProvider.d.ts +1 -1
- package/dist/types/utils/has-interpolation.d.ts +1 -1
- package/dist/types/utils/replace-elements.d.ts +1 -1
- package/dist/types/utils/translate-jsx.d.ts +1 -1
- package/dist/types/utils/translate-with-interpolation.d.ts +1 -1
- package/package.json +13 -9
- package/dist/i18lite.mjs +0 -66
- package/dist/types/i18Lite.d.ts +0 -50
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createComponent as f, memo as d } from "solid-js/web";
|
|
2
2
|
import { useContext as m, createContext as y, createSignal as x, children as C } from "solid-js";
|
|
3
|
-
import T from "
|
|
3
|
+
import T from "@nerimity/i18lite";
|
|
4
4
|
const g = y();
|
|
5
5
|
function w(t, r) {
|
|
6
6
|
const [n, e] = x(r.resources ? t.t : () => null);
|
|
@@ -28,19 +28,19 @@ const I = () => m(g), N = (t) => f(g.Provider, {
|
|
|
28
28
|
get children() {
|
|
29
29
|
return t.children;
|
|
30
30
|
}
|
|
31
|
-
}),
|
|
31
|
+
}), l = (t, { prefix: r, suffix: n }) => t.includes(r) && t.includes(n), S = (t, { interpolation: r }) => (n, e) => typeof n == "string" ? l(n, r) ? t.children[e].children?.[0].content : t.children[e].content : (n.textContent = t.children[e].children?.[0].content, n), k = !globalThis.window, v = (t, r, n) => (e) => {
|
|
32
32
|
const c = typeof e;
|
|
33
|
-
if (c === "string" &&
|
|
33
|
+
if (c === "string" && l(e, r.interpolation)) return t(e, n.options);
|
|
34
34
|
if (c === "object") {
|
|
35
35
|
const o = e.textContent ?? e.t;
|
|
36
|
-
o &&
|
|
36
|
+
o && l(o, r.interpolation) && (e[k ? "t" : "textContent"] = t(o, n.options));
|
|
37
37
|
}
|
|
38
38
|
return e;
|
|
39
39
|
};
|
|
40
|
-
let
|
|
40
|
+
let s;
|
|
41
41
|
(async () => {
|
|
42
42
|
try {
|
|
43
|
-
|
|
43
|
+
s = (await import("html-parse-string")).parse;
|
|
44
44
|
} catch {
|
|
45
45
|
}
|
|
46
46
|
})();
|
|
@@ -54,12 +54,12 @@ const L = () => {
|
|
|
54
54
|
const c = r(n.key, n.options);
|
|
55
55
|
if (!n.children) return c;
|
|
56
56
|
if (c === n.key) return e.map(v(r, t, n));
|
|
57
|
-
if (!
|
|
57
|
+
if (!s) {
|
|
58
58
|
L();
|
|
59
59
|
return;
|
|
60
60
|
}
|
|
61
61
|
try {
|
|
62
|
-
const [o] =
|
|
62
|
+
const [o] = s(`<0>${c}</0>`);
|
|
63
63
|
return e.map(S(o, t));
|
|
64
64
|
} catch (o) {
|
|
65
65
|
console.error(o);
|
|
@@ -79,3 +79,4 @@ export {
|
|
|
79
79
|
N as TransProvider,
|
|
80
80
|
I as useTransContext
|
|
81
81
|
};
|
|
82
|
+
//# sourceMappingURL=browser.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.mjs","sources":["../src/TransProvider.tsx","../src/utils/has-interpolation.ts","../src/utils/replace-elements.ts","../src/utils/translate-with-interpolation.ts","../src/utils/translate-jsx.ts","../src/Trans.tsx"],"sourcesContent":["import i18next, { type InitOptions, type TFunction, type i18n } from '@nerimity/i18lite';\r\nimport { createContext, createSignal, useContext, type ParentComponent } from 'solid-js';\r\n\r\nexport type TransProviderActions = {\r\n addResources(lng: string, ns: string, resources: any): i18n;\r\n changeLanguage(lng: string): Promise<void>;\r\n getI18next(): i18n;\r\n};\r\n\r\nconst TransContext = createContext<[TFunction, TransProviderActions]>();\r\n\r\nfunction createTransContext(instance: i18n, options: InitOptions): [TFunction, TransProviderActions] {\r\n const [translate, setTranslate] = createSignal<TFunction | (() => null)>(\r\n !!options.resources ? instance.t : () => null\r\n );\r\n\r\n instance.on('loaded', () => setTranslate(() => instance.t));\r\n instance.init(options, (_, t) => setTranslate(() => t));\r\n\r\n async function changeLanguage(lng: string) {\r\n const t = await instance.changeLanguage(lng);\r\n setTranslate(() => t);\r\n }\r\n\r\n function addResources(\r\n lng: string,\r\n ns: string,\r\n resources: any,\r\n bundleOptions: { deep?: boolean; overwrite?: boolean } = {}\r\n ): i18n {\r\n return instance.addResourceBundle(lng, ns, resources, bundleOptions.deep, bundleOptions.overwrite);\r\n }\r\n\r\n return [\r\n ((...args: Parameters<TFunction>) => translate().apply(null, args)) as TFunction,\r\n {\r\n addResources,\r\n getI18next: () => instance,\r\n changeLanguage,\r\n },\r\n ];\r\n}\r\n\r\nexport const useTransContext = () => useContext(TransContext);\r\n\r\nexport const TransProvider: ParentComponent<{ instance?: i18n; lng?: string; options?: InitOptions }> = (props) => {\r\n return (\r\n <TransContext.Provider\r\n value={createTransContext(props.instance || i18next, { lng: props.lng, ...props.options })}\r\n children={props.children}\r\n />\r\n );\r\n};\r\n","import type { InterpolationOptions } from '@nerimity/i18lite';\r\n\r\nexport const hasInterpolation = (value: string, { prefix, suffix }: InterpolationOptions) =>\r\n value.includes(prefix) && value.includes(suffix);\r\n","import type { InitOptions } from '@nerimity/i18lite';\r\nimport type { IDom } from 'html-parse-string';\r\nimport { hasInterpolation } from './has-interpolation';\r\n\r\nexport const replaceElements =\r\n (ast: IDom, { interpolation }: InitOptions) =>\r\n (child: Node, index: number) => {\r\n if (typeof child === 'string') {\r\n if (hasInterpolation(child, interpolation)) return ast.children[index].children?.[0].content;\r\n\r\n return ast.children[index].content;\r\n }\r\n\r\n child.textContent = ast.children[index].children?.[0].content;\r\n\r\n return child;\r\n };\r\n","import type { InitOptions, TFunction } from '@nerimity/i18lite';\r\nimport type { TransProps } from '../Trans';\r\nimport { hasInterpolation } from './has-interpolation';\r\n\r\nconst isNode = !globalThis.window;\r\n\r\nexport const translateWithInterpolation = (t: TFunction, options: InitOptions, props: TransProps) => (item) => {\r\n const type = typeof item;\r\n\r\n if (type === 'string' && hasInterpolation(item, options.interpolation)) return t(item, props.options);\r\n\r\n if (type === 'object') {\r\n const textContent = item.textContent ?? item.t;\r\n if (textContent && hasInterpolation(textContent, options.interpolation)) {\r\n item[isNode ? 't' : 'textContent'] = t(textContent, props.options);\r\n }\r\n }\r\n\r\n return item;\r\n};\r\n","import type { TFunction, i18n } from '@nerimity/i18lite';\r\nimport type { parse } from 'html-parse-string';\r\nimport type { ParentProps } from 'solid-js';\r\nimport type { TransProps } from '../Trans';\r\nimport { replaceElements } from './replace-elements';\r\nimport { translateWithInterpolation } from './translate-with-interpolation';\r\n\r\nexport let parseHTML: typeof parse;\r\n\r\n(async () => {\r\n try {\r\n // @ts-ignore\r\n const module = await import('html-parse-string');\r\n parseHTML = module.parse;\r\n } catch {}\r\n})();\r\n\r\nconst htmlParseStringNotFoundError = () => {\r\n console.error(\r\n 'In order to use JSX nesting, install %chtml-parse-string',\r\n 'font-weight: 700',\r\n 'https://github.com/ryansolid/html-parse-string.'\r\n );\r\n};\r\nexport const translateJSX = (\r\n { i18n: { options }, t, props }: { t: TFunction; props: ParentProps<TransProps>; i18n: i18n },\r\n children: Node[]\r\n) => {\r\n const translation = t(props.key, props.options);\r\n\r\n if (!props.children) return translation;\r\n\r\n if (translation === props.key) return children.map(translateWithInterpolation(t, options, props));\r\n\r\n if (!parseHTML) {\r\n htmlParseStringNotFoundError();\r\n return;\r\n }\r\n try {\r\n const [ast] = parseHTML(`<0>${translation}</0>`);\r\n return children.map(replaceElements(ast, options));\r\n } catch (e) {\r\n console.error(e);\r\n }\r\n};\r\n","import type { TOptions } from '@nerimity/i18lite';\r\nimport { children, type ParentComponent } from 'solid-js';\r\nimport { useTransContext } from './TransProvider';\r\nimport { translateJSX } from './utils/translate-jsx';\r\n\r\nexport type TransProps = { key: string; options?: TOptions };\r\n\r\nexport const Trans: ParentComponent<TransProps> = (props) => {\r\n const [t, { getI18next }] = useTransContext();\r\n\r\n return (\r\n <>\r\n {typeof props.children === 'string'\r\n ? t(props.key, props.children, props.options)\r\n : translateJSX({ i18n: getI18next(), t, props }, children(() => props.children)() as Node[])}\r\n </>\r\n );\r\n};\r\n"],"names":["TransContext","createContext","createTransContext","instance","options","translate","setTranslate","createSignal","resources","t","on","init","_","changeLanguage","lng","addResources","ns","bundleOptions","addResourceBundle","deep","overwrite","args","apply","getI18next","useTransContext","useContext","TransProvider","props","_$createComponent","Provider","value","i18next","children","hasInterpolation","prefix","suffix","replaceElements","ast","interpolation","child","index","isNode","translateWithInterpolation","item","type","textContent","parseHTML","htmlParseStringNotFoundError","translateJSX","translation","e","Trans","_$memo","key","i18n"],"mappings":";;;AASA,MAAMA,IAAeC,EAAAA;AAErB,SAASC,EAAmBC,GAAgBC,GAAyD;AACnG,QAAM,CAACC,GAAWC,CAAY,IAAIC,EAC9BH,EAAQI,YAAYL,EAASM,IAAI,MAAM,IAC3C;AAEAN,EAAAA,EAASO,GAAG,UAAU,MAAMJ,EAAa,MAAMH,EAASM,CAAC,CAAC,GAC1DN,EAASQ,KAAKP,GAAS,CAACQ,GAAGH,MAAMH,EAAa,MAAMG,CAAC,CAAC;AAEtD,iBAAeI,EAAeC,GAAa;AACzC,UAAML,IAAI,MAAMN,EAASU,eAAeC,CAAG;AAC3CR,IAAAA,EAAa,MAAMG,CAAC;AAAA,EACtB;AAEA,WAASM,EACPD,GACAE,GACAR,GACAS,IAAyD,CAAA,GACnD;AACN,WAAOd,EAASe,kBAAkBJ,GAAKE,GAAIR,GAAWS,EAAcE,MAAMF,EAAcG,SAAS;AAAA,EACnG;AAEA,SAAO,EACJ,IAAIC,MAAgChB,EAAAA,EAAYiB,MAAM,MAAMD,CAAI,IACjE;AAAA,IACEN,cAAAA;AAAAA,IACAQ,YAAYA,MAAMpB;AAAAA,IAClBU,gBAAAA;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AmB,GAC5B;AAAA,EACrB,QAAQ;AAAA,EAAC;AACX,GAAA;AAEA,MAAMC,IAA+B,MAAM;AACzC,UAAQ;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ,GACaC,IAAe,CAC1B,EAAE,MAAM,EAAE,SAAA5C,KAAW,GAAAK,GAAG,OAAAkB,EAAA,GACxBK,MACG;AACH,QAAMiB,IAAcxC,EAAEkB,EAAM,KAAKA,EAAM,OAAO;AAE9C,MAAI,CAACA,EAAM,SAAU,QAAOsB;AAE5B,MAAIA,MAAgBtB,EAAM,IAAK,QAAOK,EAAS,IAAIU,EAA2BjC,GAAGL,GAASuB,CAAK,CAAC;AAEhG,MAAI,CAACmB,GAAW;AACd,IAAAC,EAAA;AACA;AAAA,EACF;AACA,MAAI;AACF,UAAM,CAACV,CAAG,IAAIS,EAAU,MAAMG,CAAW,MAAM;AAC/C,WAAOjB,EAAS,IAAII,EAAgBC,GAAKjC,CAAO,CAAC;AAAA,EACnD,SAAS8C,GAAG;AACV,YAAQ,MAAMA,CAAC;AAAA,EACjB;AACF,GCrCaC,IAAsCxB,CAAAA,MAAU;AAC3D,QAAM,CAAClB,GAAG;AAAA,IAAEc,YAAAA;AAAAA,EAAAA,CAAY,IAAIC,EAAAA;AAE5B,SAAA4B,EAAA,MAEKA,EAAA,MAAA,OAAOzB,EAAMK,YAAa,QAAQ,EAAA,IAC/BvB,EAAEkB,EAAM0B,KAAK1B,EAAMK,UAAUL,EAAMvB,OAAO,IAC1C4C,EAAa;AAAA,IAAEM,MAAM/B,EAAAA;AAAAA,IAAcd,GAAAA;AAAAA,IAAGkB,OAAAA;AAAAA,EAAAA,GAASK,EAAS,MAAML,EAAMK,QAAQ,EAAA,CAAa,CAAC;AAGpG;"}
|
package/dist/ssr.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var T=Object.create;var g=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var w=Object.getPrototypeOf,m=Object.prototype.hasOwnProperty;var S=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of C(e))!m.call(t,o)&&o!==n&&g(t,o,{get:()=>e[o],enumerable:!(r=x(e,o))||r.enumerable});return t};var b=(t,e,n)=>(n=t!=null?T(w(t)):{},S(e||!t||!t.__esModule?g(n,"default",{value:t,enumerable:!0}):n,t));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("solid-js"),v=require("solid-js/web"),I=require("@nerimity/i18lite"),h=i.createContext();function P(t,e){const[n,r]=i.createSignal(e.resources?t.t:()=>null);t.on("loaded",()=>r(()=>t.t)),t.init(e,(s,a)=>r(()=>a));async function o(s){const a=await t.changeLanguage(s);r(()=>a)}function c(s,a,y,d={}){return t.addResourceBundle(s,a,y,d.deep,d.overwrite)}return[((...s)=>n().apply(null,s)),{addResources:c,getI18next:()=>t,changeLanguage:o}]}const f=()=>i.useContext(h),k=t=>v.createComponent(h.Provider,{get value(){return P(t.instance||I,{lng:t.lng,...t.options})},get children(){return t.children}}),l=(t,{prefix:e,suffix:n})=>t.includes(e)&&t.includes(n),q=(t,{interpolation:e})=>(n,r)=>typeof n=="string"?l(n,e)?t.children[r].children?.[0].content:t.children[r].content:(n.textContent=t.children[r].children?.[0].content,n),J=!globalThis.window,L=(t,e,n)=>r=>{const o=typeof r;if(o==="string"&&l(r,e.interpolation))return t(r,n.options);if(o==="object"){const c=r.textContent??r.t;c&&l(c,e.interpolation)&&(r[J?"t":"textContent"]=t(c,n.options))}return r};let u;(async()=>{try{u=(await import("html-parse-string")).parse}catch{}})();const j=()=>{console.error("In order to use JSX nesting, install %chtml-parse-string","font-weight: 700","https://github.com/ryansolid/html-parse-string.")},E=({i18n:{options:t},t:e,props:n},r)=>{const o=e(n.key,n.options);if(!n.children)return o;if(o===n.key)return r.map(L(e,t,n));if(!u){j();return}try{const[c]=u(`<0>${o}</0>`);return r.map(q(c,t))}catch(c){console.error(c)}},M=t=>{const[e,{getI18next:n}]=f();return typeof t.children=="string"?e(t.key,t.children,t.options):E({i18n:n(),t:e,props:t},i.children(()=>t.children)())};exports.Trans=M;exports.TransProvider=k;exports.useTransContext=f;
|
|
2
|
+
//# sourceMappingURL=ssr.js.map
|
package/dist/ssr.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ssr.js","sources":["../src/TransProvider.tsx","../src/utils/has-interpolation.ts","../src/utils/replace-elements.ts","../src/utils/translate-with-interpolation.ts","../src/utils/translate-jsx.ts","../src/Trans.tsx"],"sourcesContent":["import i18next, { type InitOptions, type TFunction, type i18n } from '@nerimity/i18lite';\r\nimport { createContext, createSignal, useContext, type ParentComponent } from 'solid-js';\r\n\r\nexport type TransProviderActions = {\r\n addResources(lng: string, ns: string, resources: any): i18n;\r\n changeLanguage(lng: string): Promise<void>;\r\n getI18next(): i18n;\r\n};\r\n\r\nconst TransContext = createContext<[TFunction, TransProviderActions]>();\r\n\r\nfunction createTransContext(instance: i18n, options: InitOptions): [TFunction, TransProviderActions] {\r\n const [translate, setTranslate] = createSignal<TFunction | (() => null)>(\r\n !!options.resources ? instance.t : () => null\r\n );\r\n\r\n instance.on('loaded', () => setTranslate(() => instance.t));\r\n instance.init(options, (_, t) => setTranslate(() => t));\r\n\r\n async function changeLanguage(lng: string) {\r\n const t = await instance.changeLanguage(lng);\r\n setTranslate(() => t);\r\n }\r\n\r\n function addResources(\r\n lng: string,\r\n ns: string,\r\n resources: any,\r\n bundleOptions: { deep?: boolean; overwrite?: boolean } = {}\r\n ): i18n {\r\n return instance.addResourceBundle(lng, ns, resources, bundleOptions.deep, bundleOptions.overwrite);\r\n }\r\n\r\n return [\r\n ((...args: Parameters<TFunction>) => translate().apply(null, args)) as TFunction,\r\n {\r\n addResources,\r\n getI18next: () => instance,\r\n changeLanguage,\r\n },\r\n ];\r\n}\r\n\r\nexport const useTransContext = () => useContext(TransContext);\r\n\r\nexport const TransProvider: ParentComponent<{ instance?: i18n; lng?: string; options?: InitOptions }> = (props) => {\r\n return (\r\n <TransContext.Provider\r\n value={createTransContext(props.instance || i18next, { lng: props.lng, ...props.options })}\r\n children={props.children}\r\n />\r\n );\r\n};\r\n","import type { InterpolationOptions } from '@nerimity/i18lite';\r\n\r\nexport const hasInterpolation = (value: string, { prefix, suffix }: InterpolationOptions) =>\r\n value.includes(prefix) && value.includes(suffix);\r\n","import type { InitOptions } from '@nerimity/i18lite';\r\nimport type { IDom } from 'html-parse-string';\r\nimport { hasInterpolation } from './has-interpolation';\r\n\r\nexport const replaceElements =\r\n (ast: IDom, { interpolation }: InitOptions) =>\r\n (child: Node, index: number) => {\r\n if (typeof child === 'string') {\r\n if (hasInterpolation(child, interpolation)) return ast.children[index].children?.[0].content;\r\n\r\n return ast.children[index].content;\r\n }\r\n\r\n child.textContent = ast.children[index].children?.[0].content;\r\n\r\n return child;\r\n };\r\n","import type { InitOptions, TFunction } from '@nerimity/i18lite';\r\nimport type { TransProps } from '../Trans';\r\nimport { hasInterpolation } from './has-interpolation';\r\n\r\nconst isNode = !globalThis.window;\r\n\r\nexport const translateWithInterpolation = (t: TFunction, options: InitOptions, props: TransProps) => (item) => {\r\n const type = typeof item;\r\n\r\n if (type === 'string' && hasInterpolation(item, options.interpolation)) return t(item, props.options);\r\n\r\n if (type === 'object') {\r\n const textContent = item.textContent ?? item.t;\r\n if (textContent && hasInterpolation(textContent, options.interpolation)) {\r\n item[isNode ? 't' : 'textContent'] = t(textContent, props.options);\r\n }\r\n }\r\n\r\n return item;\r\n};\r\n","import type { TFunction, i18n } from '@nerimity/i18lite';\r\nimport type { parse } from 'html-parse-string';\r\nimport type { ParentProps } from 'solid-js';\r\nimport type { TransProps } from '../Trans';\r\nimport { replaceElements } from './replace-elements';\r\nimport { translateWithInterpolation } from './translate-with-interpolation';\r\n\r\nexport let parseHTML: typeof parse;\r\n\r\n(async () => {\r\n try {\r\n // @ts-ignore\r\n const module = await import('html-parse-string');\r\n parseHTML = module.parse;\r\n } catch {}\r\n})();\r\n\r\nconst htmlParseStringNotFoundError = () => {\r\n console.error(\r\n 'In order to use JSX nesting, install %chtml-parse-string',\r\n 'font-weight: 700',\r\n 'https://github.com/ryansolid/html-parse-string.'\r\n );\r\n};\r\nexport const translateJSX = (\r\n { i18n: { options }, t, props }: { t: TFunction; props: ParentProps<TransProps>; i18n: i18n },\r\n children: Node[]\r\n) => {\r\n const translation = t(props.key, props.options);\r\n\r\n if (!props.children) return translation;\r\n\r\n if (translation === props.key) return children.map(translateWithInterpolation(t, options, props));\r\n\r\n if (!parseHTML) {\r\n htmlParseStringNotFoundError();\r\n return;\r\n }\r\n try {\r\n const [ast] = parseHTML(`<0>${translation}</0>`);\r\n return children.map(replaceElements(ast, options));\r\n } catch (e) {\r\n console.error(e);\r\n }\r\n};\r\n","import type { TOptions } from '@nerimity/i18lite';\r\nimport { children, type ParentComponent } from 'solid-js';\r\nimport { useTransContext } from './TransProvider';\r\nimport { translateJSX } from './utils/translate-jsx';\r\n\r\nexport type TransProps = { key: string; options?: TOptions };\r\n\r\nexport const Trans: ParentComponent<TransProps> = (props) => {\r\n const [t, { getI18next }] = useTransContext();\r\n\r\n return (\r\n <>\r\n {typeof props.children === 'string'\r\n ? t(props.key, props.children, props.options)\r\n : translateJSX({ i18n: getI18next(), t, props }, children(() => props.children)() as Node[])}\r\n </>\r\n );\r\n};\r\n"],"names":["TransContext","createContext","createTransContext","instance","options","translate","setTranslate","createSignal","resources","t","on","init","_","changeLanguage","lng","addResources","ns","bundleOptions","addResourceBundle","deep","overwrite","args","apply","getI18next","useTransContext","useContext","TransProvider","props","_$createComponent","Provider","value","i18next","children","hasInterpolation","prefix","suffix","replaceElements","ast","interpolation","child","index","isNode","translateWithInterpolation","item","type","textContent","parseHTML","htmlParseStringNotFoundError","translateJSX","translation","e","Trans","key","i18n"],"mappings":"gnBASMA,EAAeC,EAAAA,cAAAA,EAErB,SAASC,EAAmBC,EAAgBC,EAAyD,CACnG,KAAM,CAACC,EAAWC,CAAY,EAAI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,SACvBvB,EAAEkB,EAAMyB,IAAKzB,EAAMK,SAAUL,EAAMvB,OAAO,EAC1C4C,EAAa,CAAEK,KAAM9B,EAAAA,EAAcd,EAAAA,EAAGkB,MAAAA,CAAAA,EAASK,WAAS,IAAML,EAAMK,QAAQ,GAAa,CAGnG"}
|
package/dist/ssr.mjs
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { useContext as h, createContext as f, createSignal as y, children as m } from "solid-js";
|
|
2
|
+
import { createComponent as x } from "solid-js/web";
|
|
3
|
+
import C from "@nerimity/i18lite";
|
|
4
|
+
const d = f();
|
|
5
|
+
function T(t, r) {
|
|
6
|
+
const [n, e] = y(r.resources ? t.t : () => null);
|
|
7
|
+
t.on("loaded", () => e(() => t.t)), t.init(r, (a, i) => e(() => i));
|
|
8
|
+
async function c(a) {
|
|
9
|
+
const i = await t.changeLanguage(a);
|
|
10
|
+
e(() => i);
|
|
11
|
+
}
|
|
12
|
+
function o(a, i, g, u = {}) {
|
|
13
|
+
return t.addResourceBundle(a, i, g, u.deep, u.overwrite);
|
|
14
|
+
}
|
|
15
|
+
return [((...a) => n().apply(null, a)), {
|
|
16
|
+
addResources: o,
|
|
17
|
+
getI18next: () => t,
|
|
18
|
+
changeLanguage: c
|
|
19
|
+
}];
|
|
20
|
+
}
|
|
21
|
+
const w = () => h(d), J = (t) => x(d.Provider, {
|
|
22
|
+
get value() {
|
|
23
|
+
return T(t.instance || C, {
|
|
24
|
+
lng: t.lng,
|
|
25
|
+
...t.options
|
|
26
|
+
});
|
|
27
|
+
},
|
|
28
|
+
get children() {
|
|
29
|
+
return t.children;
|
|
30
|
+
}
|
|
31
|
+
}), l = (t, { prefix: r, suffix: n }) => t.includes(r) && t.includes(n), I = (t, { interpolation: r }) => (n, e) => typeof n == "string" ? l(n, r) ? t.children[e].children?.[0].content : t.children[e].content : (n.textContent = t.children[e].children?.[0].content, n), S = !globalThis.window, k = (t, r, n) => (e) => {
|
|
32
|
+
const c = typeof e;
|
|
33
|
+
if (c === "string" && l(e, r.interpolation)) return t(e, n.options);
|
|
34
|
+
if (c === "object") {
|
|
35
|
+
const o = e.textContent ?? e.t;
|
|
36
|
+
o && l(o, r.interpolation) && (e[S ? "t" : "textContent"] = t(o, n.options));
|
|
37
|
+
}
|
|
38
|
+
return e;
|
|
39
|
+
};
|
|
40
|
+
let s;
|
|
41
|
+
(async () => {
|
|
42
|
+
try {
|
|
43
|
+
s = (await import("html-parse-string")).parse;
|
|
44
|
+
} catch {
|
|
45
|
+
}
|
|
46
|
+
})();
|
|
47
|
+
const v = () => {
|
|
48
|
+
console.error(
|
|
49
|
+
"In order to use JSX nesting, install %chtml-parse-string",
|
|
50
|
+
"font-weight: 700",
|
|
51
|
+
"https://github.com/ryansolid/html-parse-string."
|
|
52
|
+
);
|
|
53
|
+
}, L = ({ i18n: { options: t }, t: r, props: n }, e) => {
|
|
54
|
+
const c = r(n.key, n.options);
|
|
55
|
+
if (!n.children) return c;
|
|
56
|
+
if (c === n.key) return e.map(k(r, t, n));
|
|
57
|
+
if (!s) {
|
|
58
|
+
v();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
const [o] = s(`<0>${c}</0>`);
|
|
63
|
+
return e.map(I(o, t));
|
|
64
|
+
} catch (o) {
|
|
65
|
+
console.error(o);
|
|
66
|
+
}
|
|
67
|
+
}, N = (t) => {
|
|
68
|
+
const [r, {
|
|
69
|
+
getI18next: n
|
|
70
|
+
}] = w();
|
|
71
|
+
return typeof t.children == "string" ? r(t.key, t.children, t.options) : L({
|
|
72
|
+
i18n: n(),
|
|
73
|
+
t: r,
|
|
74
|
+
props: t
|
|
75
|
+
}, m(() => t.children)());
|
|
76
|
+
};
|
|
77
|
+
export {
|
|
78
|
+
N as Trans,
|
|
79
|
+
J as TransProvider,
|
|
80
|
+
w as useTransContext
|
|
81
|
+
};
|
|
82
|
+
//# sourceMappingURL=ssr.mjs.map
|
package/dist/ssr.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ssr.mjs","sources":["../src/TransProvider.tsx","../src/utils/has-interpolation.ts","../src/utils/replace-elements.ts","../src/utils/translate-with-interpolation.ts","../src/utils/translate-jsx.ts","../src/Trans.tsx"],"sourcesContent":["import i18next, { type InitOptions, type TFunction, type i18n } from '@nerimity/i18lite';\r\nimport { createContext, createSignal, useContext, type ParentComponent } from 'solid-js';\r\n\r\nexport type TransProviderActions = {\r\n addResources(lng: string, ns: string, resources: any): i18n;\r\n changeLanguage(lng: string): Promise<void>;\r\n getI18next(): i18n;\r\n};\r\n\r\nconst TransContext = createContext<[TFunction, TransProviderActions]>();\r\n\r\nfunction createTransContext(instance: i18n, options: InitOptions): [TFunction, TransProviderActions] {\r\n const [translate, setTranslate] = createSignal<TFunction | (() => null)>(\r\n !!options.resources ? instance.t : () => null\r\n );\r\n\r\n instance.on('loaded', () => setTranslate(() => instance.t));\r\n instance.init(options, (_, t) => setTranslate(() => t));\r\n\r\n async function changeLanguage(lng: string) {\r\n const t = await instance.changeLanguage(lng);\r\n setTranslate(() => t);\r\n }\r\n\r\n function addResources(\r\n lng: string,\r\n ns: string,\r\n resources: any,\r\n bundleOptions: { deep?: boolean; overwrite?: boolean } = {}\r\n ): i18n {\r\n return instance.addResourceBundle(lng, ns, resources, bundleOptions.deep, bundleOptions.overwrite);\r\n }\r\n\r\n return [\r\n ((...args: Parameters<TFunction>) => translate().apply(null, args)) as TFunction,\r\n {\r\n addResources,\r\n getI18next: () => instance,\r\n changeLanguage,\r\n },\r\n ];\r\n}\r\n\r\nexport const useTransContext = () => useContext(TransContext);\r\n\r\nexport const TransProvider: ParentComponent<{ instance?: i18n; lng?: string; options?: InitOptions }> = (props) => {\r\n return (\r\n <TransContext.Provider\r\n value={createTransContext(props.instance || i18next, { lng: props.lng, ...props.options })}\r\n children={props.children}\r\n />\r\n );\r\n};\r\n","import type { InterpolationOptions } from '@nerimity/i18lite';\r\n\r\nexport const hasInterpolation = (value: string, { prefix, suffix }: InterpolationOptions) =>\r\n value.includes(prefix) && value.includes(suffix);\r\n","import type { InitOptions } from '@nerimity/i18lite';\r\nimport type { IDom } from 'html-parse-string';\r\nimport { hasInterpolation } from './has-interpolation';\r\n\r\nexport const replaceElements =\r\n (ast: IDom, { interpolation }: InitOptions) =>\r\n (child: Node, index: number) => {\r\n if (typeof child === 'string') {\r\n if (hasInterpolation(child, interpolation)) return ast.children[index].children?.[0].content;\r\n\r\n return ast.children[index].content;\r\n }\r\n\r\n child.textContent = ast.children[index].children?.[0].content;\r\n\r\n return child;\r\n };\r\n","import type { InitOptions, TFunction } from '@nerimity/i18lite';\r\nimport type { TransProps } from '../Trans';\r\nimport { hasInterpolation } from './has-interpolation';\r\n\r\nconst isNode = !globalThis.window;\r\n\r\nexport const translateWithInterpolation = (t: TFunction, options: InitOptions, props: TransProps) => (item) => {\r\n const type = typeof item;\r\n\r\n if (type === 'string' && hasInterpolation(item, options.interpolation)) return t(item, props.options);\r\n\r\n if (type === 'object') {\r\n const textContent = item.textContent ?? item.t;\r\n if (textContent && hasInterpolation(textContent, options.interpolation)) {\r\n item[isNode ? 't' : 'textContent'] = t(textContent, props.options);\r\n }\r\n }\r\n\r\n return item;\r\n};\r\n","import type { TFunction, i18n } from '@nerimity/i18lite';\r\nimport type { parse } from 'html-parse-string';\r\nimport type { ParentProps } from 'solid-js';\r\nimport type { TransProps } from '../Trans';\r\nimport { replaceElements } from './replace-elements';\r\nimport { translateWithInterpolation } from './translate-with-interpolation';\r\n\r\nexport let parseHTML: typeof parse;\r\n\r\n(async () => {\r\n try {\r\n // @ts-ignore\r\n const module = await import('html-parse-string');\r\n parseHTML = module.parse;\r\n } catch {}\r\n})();\r\n\r\nconst htmlParseStringNotFoundError = () => {\r\n console.error(\r\n 'In order to use JSX nesting, install %chtml-parse-string',\r\n 'font-weight: 700',\r\n 'https://github.com/ryansolid/html-parse-string.'\r\n );\r\n};\r\nexport const translateJSX = (\r\n { i18n: { options }, t, props }: { t: TFunction; props: ParentProps<TransProps>; i18n: i18n },\r\n children: Node[]\r\n) => {\r\n const translation = t(props.key, props.options);\r\n\r\n if (!props.children) return translation;\r\n\r\n if (translation === props.key) return children.map(translateWithInterpolation(t, options, props));\r\n\r\n if (!parseHTML) {\r\n htmlParseStringNotFoundError();\r\n return;\r\n }\r\n try {\r\n const [ast] = parseHTML(`<0>${translation}</0>`);\r\n return children.map(replaceElements(ast, options));\r\n } catch (e) {\r\n console.error(e);\r\n }\r\n};\r\n","import type { TOptions } from '@nerimity/i18lite';\r\nimport { children, type ParentComponent } from 'solid-js';\r\nimport { useTransContext } from './TransProvider';\r\nimport { translateJSX } from './utils/translate-jsx';\r\n\r\nexport type TransProps = { key: string; options?: TOptions };\r\n\r\nexport const Trans: ParentComponent<TransProps> = (props) => {\r\n const [t, { getI18next }] = useTransContext();\r\n\r\n return (\r\n <>\r\n {typeof props.children === 'string'\r\n ? t(props.key, props.children, props.options)\r\n : translateJSX({ i18n: getI18next(), t, props }, children(() => props.children)() as Node[])}\r\n </>\r\n );\r\n};\r\n"],"names":["TransContext","createContext","createTransContext","instance","options","translate","setTranslate","createSignal","resources","t","on","init","_","changeLanguage","lng","addResources","ns","bundleOptions","addResourceBundle","deep","overwrite","args","apply","getI18next","useTransContext","useContext","TransProvider","props","_$createComponent","Provider","value","i18next","children","hasInterpolation","prefix","suffix","replaceElements","ast","interpolation","child","index","isNode","translateWithInterpolation","item","type","textContent","parseHTML","htmlParseStringNotFoundError","translateJSX","translation","e","Trans","key","i18n"],"mappings":";;;AASA,MAAMA,IAAeC,EAAAA;AAErB,SAASC,EAAmBC,GAAgBC,GAAyD;AACnG,QAAM,CAACC,GAAWC,CAAY,IAAIC,EAC9BH,EAAQI,YAAYL,EAASM,IAAI,MAAM,IAC3C;AAEAN,EAAAA,EAASO,GAAG,UAAU,MAAMJ,EAAa,MAAMH,EAASM,CAAC,CAAC,GAC1DN,EAASQ,KAAKP,GAAS,CAACQ,GAAGH,MAAMH,EAAa,MAA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AAOG,EAAMK,YAAa,WACvBvB,EAAEkB,EAAMyB,KAAKzB,EAAMK,UAAUL,EAAMvB,OAAO,IAC1C4C,EAAa;AAAA,IAAEK,MAAM9B,EAAAA;AAAAA,IAAcd,GAAAA;AAAAA,IAAGkB,OAAAA;AAAAA,EAAAA,GAASK,EAAS,MAAML,EAAMK,QAAQ,GAAa;AAGnG;"}
|
package/dist/types/Trans.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { type InitOptions, type TFunction, type i18n } from '@nerimity/i18lite';
|
|
1
2
|
import { type ParentComponent } from 'solid-js';
|
|
2
|
-
import { type InitOptions, type TFunction, type i18n } from './i18Lite';
|
|
3
3
|
export type TransProviderActions = {
|
|
4
4
|
addResources(lng: string, ns: string, resources: any): i18n;
|
|
5
5
|
changeLanguage(lng: string): Promise<void>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { InterpolationOptions } from '
|
|
1
|
+
import type { InterpolationOptions } from '@nerimity/i18lite';
|
|
2
2
|
export declare const hasInterpolation: (value: string, { prefix, suffix }: InterpolationOptions) => boolean;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
+
import type { InitOptions } from '@nerimity/i18lite';
|
|
1
2
|
import type { IDom } from 'html-parse-string';
|
|
2
|
-
import type { InitOptions } from '../i18Lite';
|
|
3
3
|
export declare const replaceElements: (ast: IDom, { interpolation }: InitOptions) => (child: Node, index: number) => string | Node;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import type { TFunction, i18n } from '@nerimity/i18lite';
|
|
1
2
|
import type { parse } from 'html-parse-string';
|
|
2
3
|
import type { ParentProps } from 'solid-js';
|
|
3
|
-
import type { TFunction, i18n } from '../i18Lite';
|
|
4
4
|
import type { TransProps } from '../Trans';
|
|
5
5
|
export declare let parseHTML: typeof parse;
|
|
6
6
|
export declare const translateJSX: ({ i18n: { options }, t, props }: {
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type { InitOptions, TFunction } from '
|
|
1
|
+
import type { InitOptions, TFunction } from '@nerimity/i18lite';
|
|
2
2
|
import type { TransProps } from '../Trans';
|
|
3
3
|
export declare const translateWithInterpolation: (t: TFunction, options: InitOptions, props: TransProps) => (item: any) => any;
|
package/package.json
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
"@types/jest": "^30.0.0",
|
|
9
9
|
"babel-preset-solid": "^1.9.9",
|
|
10
10
|
"html-parse-string": "^0.0.9",
|
|
11
|
+
"@nerimity/i18lite": "^1.0.6",
|
|
11
12
|
"i18next-http-backend": "^3.0.2",
|
|
12
13
|
"jest": "^30.2.0",
|
|
13
14
|
"jest-environment-jsdom": "^30.2.0",
|
|
@@ -20,18 +21,17 @@
|
|
|
20
21
|
"exports": {
|
|
21
22
|
".": {
|
|
22
23
|
"types": "./dist/types/index.d.ts",
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
"
|
|
24
|
+
"browser": {
|
|
25
|
+
"default": "./dist/browser.mjs"
|
|
26
|
+
},
|
|
27
|
+
"import": "./dist/ssr.mjs",
|
|
28
|
+
"require": "./dist/ssr.js"
|
|
28
29
|
}
|
|
29
30
|
},
|
|
30
31
|
"files": [
|
|
31
|
-
"dist"
|
|
32
|
+
"dist",
|
|
33
|
+
"CHANGELOG.md"
|
|
32
34
|
],
|
|
33
|
-
"main": "./dist/index.mjs",
|
|
34
|
-
"module": "./dist/index.mjs",
|
|
35
35
|
"keywords": [
|
|
36
36
|
"i18n",
|
|
37
37
|
"i18next",
|
|
@@ -46,9 +46,12 @@
|
|
|
46
46
|
"translations"
|
|
47
47
|
],
|
|
48
48
|
"license": "MIT",
|
|
49
|
+
"main": "./dist/ssr.js",
|
|
50
|
+
"module": "./dist/ssr.mjs",
|
|
49
51
|
"name": "@nerimity/solid-i18lite",
|
|
50
52
|
"peerDependencies": {
|
|
51
53
|
"html-parse-string": "<=1.x",
|
|
54
|
+
"@nerimity/i18lite": "<=1.x",
|
|
52
55
|
"solid-js": ">=1.8.x"
|
|
53
56
|
},
|
|
54
57
|
"peerDependenciesMeta": {
|
|
@@ -60,6 +63,7 @@
|
|
|
60
63
|
"scripts": {
|
|
61
64
|
"build:browser": "vite build --config vite.browser.config.js",
|
|
62
65
|
"build:docs": "vite build --config vite.docs.config.js",
|
|
66
|
+
"build:ssr": "vite build --config vite.ssr.config.js",
|
|
63
67
|
"build:types": "tsc --project tsconfig.types.json",
|
|
64
68
|
"docs": "vite --config vite.docs.config.js",
|
|
65
69
|
"test": "jest --coverage",
|
|
@@ -68,5 +72,5 @@
|
|
|
68
72
|
"sideEffects": false,
|
|
69
73
|
"type": "module",
|
|
70
74
|
"types": "./dist/types/index.d.ts",
|
|
71
|
-
"version": "1.
|
|
75
|
+
"version": "1.6.0"
|
|
72
76
|
}
|
package/dist/i18lite.mjs
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
let h = {}, S = "translation", d = "";
|
|
2
|
-
const A = { resources: {}, interpolation: { prefix: "{{", suffix: "}}" } };
|
|
3
|
-
let c = A;
|
|
4
|
-
const R = (e) => e.replace(/[-|\\{}()[\]^$+*?.]/g, (t) => t === "-" ? "\\x2d" : `\\${t}`), v = /* @__PURE__ */ new Map(), $ = (e, t) => {
|
|
5
|
-
if (!e) return "";
|
|
6
|
-
const o = c?.interpolation, s = o?.prefix ?? "{{", a = o?.suffix ?? "}}", i = s + "|" + a;
|
|
7
|
-
let r = v.get(i);
|
|
8
|
-
if (!r) {
|
|
9
|
-
const f = R(s), u = R(a);
|
|
10
|
-
r = new RegExp(`${f}\\s*(\\w+)\\s*${u}`, "g"), v.set(i, r);
|
|
11
|
-
}
|
|
12
|
-
return e.replace(r, (f, u) => Object.prototype.hasOwnProperty.call(t, u) ? t[u] : f);
|
|
13
|
-
}, g = (e, t, o) => {
|
|
14
|
-
let s = j(n.language, S, e) ?? (typeof t == "string" ? t : e);
|
|
15
|
-
return o || typeof t == "object" && t !== null ? $(s, o || t) : s;
|
|
16
|
-
}, P = (e, t) => {
|
|
17
|
-
h[e] = t;
|
|
18
|
-
}, L = (e, t) => {
|
|
19
|
-
c = A, e && (c = { ...e, interpolation: e?.interpolation || {} }, c.interpolation.prefix = e.interpolation?.prefix || "{{", c.interpolation.suffix = e.interpolation?.suffix || "}}"), d = e?.lng || "", n.store = { data: c.resources || {} }, c.resources = {}, t?.(null, g);
|
|
20
|
-
}, M = (e) => (d = e, Promise.resolve(g)), b = (e, t, o = !0) => {
|
|
21
|
-
const s = e;
|
|
22
|
-
for (const a in t) {
|
|
23
|
-
const i = s[a], r = t[a];
|
|
24
|
-
r && typeof r == "object" && !Array.isArray(r) ? ((typeof i != "object" || Array.isArray(i)) && (s[a] = {}), b(s[a], r, o)) : (o || i === void 0) && (s[a] = r);
|
|
25
|
-
}
|
|
26
|
-
return e;
|
|
27
|
-
}, V = (e, t, o, s = !0, a = !0) => {
|
|
28
|
-
n.store = n.store || { data: {} }, n.store.data = n.store.data || {}, n.store.data[e] = n.store.data[e] || {}, n.store.data[e][t] = n.store.data[e][t] || {};
|
|
29
|
-
const i = n.store.data[e][t], r = o;
|
|
30
|
-
return s ? b(i, r, a) : n.store.data[e][t] = a ? r : { ...r, ...i }, (h.loaded || (() => {
|
|
31
|
-
}))(e, t), n;
|
|
32
|
-
}, j = (e, t, o, s) => {
|
|
33
|
-
const a = n.store;
|
|
34
|
-
if (!a) return;
|
|
35
|
-
const i = a.data?.[e];
|
|
36
|
-
if (!i) return;
|
|
37
|
-
const r = i[t];
|
|
38
|
-
if (!r || typeof r == "string") return;
|
|
39
|
-
const f = s?.keySeparator ?? n.options?.keySeparator ?? ".";
|
|
40
|
-
if (f === !1) return r[o];
|
|
41
|
-
const u = String(f);
|
|
42
|
-
if (o.indexOf(u) === -1) return r[o];
|
|
43
|
-
let p = r, x = 0;
|
|
44
|
-
for (let l = 0; l <= o.length; l++)
|
|
45
|
-
if (l === o.length || o[l] === u) {
|
|
46
|
-
const y = o.slice(x, l);
|
|
47
|
-
if (p == null || !(y in p)) return;
|
|
48
|
-
p = p[y], x = l + 1;
|
|
49
|
-
}
|
|
50
|
-
return p;
|
|
51
|
-
}, B = () => n, n = {
|
|
52
|
-
get language() {
|
|
53
|
-
return d;
|
|
54
|
-
},
|
|
55
|
-
createInstance: B,
|
|
56
|
-
t: g,
|
|
57
|
-
on: P,
|
|
58
|
-
init: L,
|
|
59
|
-
changeLanguage: M,
|
|
60
|
-
addResourceBundle: V,
|
|
61
|
-
options: c,
|
|
62
|
-
getResource: j
|
|
63
|
-
};
|
|
64
|
-
export {
|
|
65
|
-
n as default
|
|
66
|
-
};
|
package/dist/types/i18Lite.d.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
export type $Dictionary<T = unknown> = {
|
|
2
|
-
[key: string]: T;
|
|
3
|
-
};
|
|
4
|
-
export type ResourceKey = string | {
|
|
5
|
-
[key: string]: any;
|
|
6
|
-
};
|
|
7
|
-
export interface ResourceLanguage {
|
|
8
|
-
[namespace: string]: ResourceKey;
|
|
9
|
-
}
|
|
10
|
-
export interface Resource {
|
|
11
|
-
[language: string]: ResourceLanguage;
|
|
12
|
-
}
|
|
13
|
-
export interface InterpolationOptions {
|
|
14
|
-
prefix?: string;
|
|
15
|
-
suffix?: string;
|
|
16
|
-
}
|
|
17
|
-
export interface InitOptions {
|
|
18
|
-
resources: Resource;
|
|
19
|
-
lng?: string;
|
|
20
|
-
interpolation?: InterpolationOptions;
|
|
21
|
-
keySeparator?: false | string;
|
|
22
|
-
nsSeparator?: false | string;
|
|
23
|
-
defaultNS?: string;
|
|
24
|
-
ignoreJSONStructure?: boolean;
|
|
25
|
-
}
|
|
26
|
-
export interface TOptionsBase {
|
|
27
|
-
lng?: string;
|
|
28
|
-
}
|
|
29
|
-
export type TOptions<TInterpolationMap extends object = $Dictionary> = TOptionsBase & TInterpolationMap;
|
|
30
|
-
export type TFunction = {
|
|
31
|
-
(key: string, options?: {}): string;
|
|
32
|
-
(key: string, defaultValue: string, options?: {}): string;
|
|
33
|
-
(key: string, arg2?: string | {}, arg3?: {}): string;
|
|
34
|
-
};
|
|
35
|
-
export interface i18n {
|
|
36
|
-
language: string;
|
|
37
|
-
getResource: (lng: string, ns: string, key: string, options?: any) => any;
|
|
38
|
-
createInstance: () => i18n;
|
|
39
|
-
t: TFunction;
|
|
40
|
-
on: (...args: any[]) => void;
|
|
41
|
-
init: (options?: InitOptions, callback?: (error: any, t: TFunction) => void) => void;
|
|
42
|
-
changeLanguage: (lng: string) => Promise<TFunction>;
|
|
43
|
-
addResourceBundle(lng: string, ns: string, resources: any, deep?: boolean, overwrite?: boolean): i18n;
|
|
44
|
-
store?: {
|
|
45
|
-
data: Resource;
|
|
46
|
-
};
|
|
47
|
-
options?: InitOptions;
|
|
48
|
-
}
|
|
49
|
-
declare const instance: i18n;
|
|
50
|
-
export default instance;
|