@nerimity/solid-i18lite 1.5.9 → 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/browser.mjs +47 -107
- package/dist/browser.mjs.map +1 -0
- package/dist/ssr.js +2 -1
- package/dist/ssr.js.map +1 -0
- package/dist/ssr.mjs +47 -107
- 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 +3 -1
- package/dist/types/i18Lite.d.ts +0 -50
package/README.md
CHANGED
package/dist/browser.mjs
CHANGED
|
@@ -1,87 +1,26 @@
|
|
|
1
|
-
import { createComponent as
|
|
2
|
-
import { useContext as
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const i = R(r), u = R(s);
|
|
12
|
-
o = new RegExp(`${i}\\s*(\\w+)\\s*${u}`, "g"), m.set(a, o);
|
|
1
|
+
import { createComponent as f, memo as d } from "solid-js/web";
|
|
2
|
+
import { useContext as m, createContext as y, createSignal as x, children as C } from "solid-js";
|
|
3
|
+
import T from "@nerimity/i18lite";
|
|
4
|
+
const g = y();
|
|
5
|
+
function w(t, r) {
|
|
6
|
+
const [n, e] = x(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);
|
|
13
11
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
let r = j(c.language, E, t) ?? (typeof e == "string" ? e : t);
|
|
17
|
-
return n || typeof e == "object" && e !== null ? M(r, n || e) : r;
|
|
18
|
-
}, B = (t, e) => {
|
|
19
|
-
S[t] = e;
|
|
20
|
-
}, F = (t, e) => {
|
|
21
|
-
f = T, t && (f = { ...t, interpolation: t?.interpolation || {} }, f.interpolation.prefix = t.interpolation?.prefix || "{{", f.interpolation.suffix = t.interpolation?.suffix || "}}"), x = t?.lng || "", c.store = { data: f.resources || {} }, f.resources = {}, e?.(null, h);
|
|
22
|
-
}, J = (t) => (x = t, Promise.resolve(h)), b = (t, e, n = !0) => {
|
|
23
|
-
const r = t;
|
|
24
|
-
for (const s in e) {
|
|
25
|
-
const a = r[s], o = e[s];
|
|
26
|
-
o && typeof o == "object" && !Array.isArray(o) ? ((typeof a != "object" || Array.isArray(a)) && (r[s] = {}), b(r[s], o, n)) : (n || a === void 0) && (r[s] = o);
|
|
12
|
+
function o(a, i, h, u = {}) {
|
|
13
|
+
return t.addResourceBundle(a, i, h, u.deep, u.overwrite);
|
|
27
14
|
}
|
|
28
|
-
return
|
|
29
|
-
|
|
30
|
-
c.store = c.store || { data: {} }, c.store.data = c.store.data || {}, c.store.data[t] = c.store.data[t] || {}, c.store.data[t][e] = c.store.data[t][e] || {};
|
|
31
|
-
const a = c.store.data[t][e], o = n;
|
|
32
|
-
return r ? b(a, o, s) : c.store.data[t][e] = s ? o : { ...o, ...a }, (S.loaded || (() => {
|
|
33
|
-
}))(t, e), c;
|
|
34
|
-
}, j = (t, e, n, r) => {
|
|
35
|
-
const s = c.store;
|
|
36
|
-
if (!s) return;
|
|
37
|
-
const a = s.data?.[t];
|
|
38
|
-
if (!a) return;
|
|
39
|
-
const o = a[e];
|
|
40
|
-
if (!o || typeof o == "string") return;
|
|
41
|
-
const i = r?.keySeparator ?? c.options?.keySeparator ?? ".";
|
|
42
|
-
if (i === !1) return o[n];
|
|
43
|
-
const u = String(i);
|
|
44
|
-
if (n.indexOf(u) === -1) return o[n];
|
|
45
|
-
let l = o, y = 0;
|
|
46
|
-
for (let d = 0; d <= n.length; d++)
|
|
47
|
-
if (d === n.length || n[d] === u) {
|
|
48
|
-
const C = n.slice(y, d);
|
|
49
|
-
if (l == null || !(C in l)) return;
|
|
50
|
-
l = l[C], y = d + 1;
|
|
51
|
-
}
|
|
52
|
-
return l;
|
|
53
|
-
}, X = () => c, c = {
|
|
54
|
-
get language() {
|
|
55
|
-
return x;
|
|
56
|
-
},
|
|
57
|
-
createInstance: X,
|
|
58
|
-
t: h,
|
|
59
|
-
on: B,
|
|
60
|
-
init: F,
|
|
61
|
-
changeLanguage: J,
|
|
62
|
-
addResourceBundle: V,
|
|
63
|
-
options: f,
|
|
64
|
-
getResource: j
|
|
65
|
-
}, A = w();
|
|
66
|
-
function H(t, e) {
|
|
67
|
-
const [n, r] = L(e.resources ? t.t : () => null);
|
|
68
|
-
t.on("loaded", () => r(() => t.t)), t.init(e, (o, i) => r(() => i));
|
|
69
|
-
async function s(o) {
|
|
70
|
-
const i = await t.changeLanguage(o);
|
|
71
|
-
r(() => i);
|
|
72
|
-
}
|
|
73
|
-
function a(o, i, u, l = {}) {
|
|
74
|
-
return t.addResourceBundle(o, i, u, l.deep, l.overwrite);
|
|
75
|
-
}
|
|
76
|
-
return [((...o) => n().apply(null, o)), {
|
|
77
|
-
addResources: a,
|
|
15
|
+
return [((...a) => n().apply(null, a)), {
|
|
16
|
+
addResources: o,
|
|
78
17
|
getI18next: () => t,
|
|
79
|
-
changeLanguage:
|
|
18
|
+
changeLanguage: c
|
|
80
19
|
}];
|
|
81
20
|
}
|
|
82
|
-
const
|
|
21
|
+
const I = () => m(g), N = (t) => f(g.Provider, {
|
|
83
22
|
get value() {
|
|
84
|
-
return
|
|
23
|
+
return w(t.instance || T, {
|
|
85
24
|
lng: t.lng,
|
|
86
25
|
...t.options
|
|
87
26
|
});
|
|
@@ -89,54 +28,55 @@ const K = () => P(A), Y = (t) => I(A.Provider, {
|
|
|
89
28
|
get children() {
|
|
90
29
|
return t.children;
|
|
91
30
|
}
|
|
92
|
-
}),
|
|
93
|
-
const
|
|
94
|
-
if (
|
|
95
|
-
if (
|
|
96
|
-
const
|
|
97
|
-
|
|
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
|
+
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[k ? "t" : "textContent"] = t(o, n.options));
|
|
98
37
|
}
|
|
99
|
-
return
|
|
38
|
+
return e;
|
|
100
39
|
};
|
|
101
|
-
let
|
|
40
|
+
let s;
|
|
102
41
|
(async () => {
|
|
103
42
|
try {
|
|
104
|
-
|
|
43
|
+
s = (await import("html-parse-string")).parse;
|
|
105
44
|
} catch {
|
|
106
45
|
}
|
|
107
46
|
})();
|
|
108
|
-
const
|
|
47
|
+
const L = () => {
|
|
109
48
|
console.error(
|
|
110
49
|
"In order to use JSX nesting, install %chtml-parse-string",
|
|
111
50
|
"font-weight: 700",
|
|
112
51
|
"https://github.com/ryansolid/html-parse-string."
|
|
113
52
|
);
|
|
114
|
-
},
|
|
115
|
-
const
|
|
116
|
-
if (!n.children) return
|
|
117
|
-
if (
|
|
118
|
-
if (!
|
|
119
|
-
|
|
53
|
+
}, P = ({ 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(v(r, t, n));
|
|
57
|
+
if (!s) {
|
|
58
|
+
L();
|
|
120
59
|
return;
|
|
121
60
|
}
|
|
122
61
|
try {
|
|
123
|
-
const [
|
|
124
|
-
return
|
|
125
|
-
} catch (
|
|
126
|
-
console.error(
|
|
62
|
+
const [o] = s(`<0>${c}</0>`);
|
|
63
|
+
return e.map(S(o, t));
|
|
64
|
+
} catch (o) {
|
|
65
|
+
console.error(o);
|
|
127
66
|
}
|
|
128
|
-
},
|
|
129
|
-
const [
|
|
67
|
+
}, R = (t) => {
|
|
68
|
+
const [r, {
|
|
130
69
|
getI18next: n
|
|
131
|
-
}] =
|
|
132
|
-
return
|
|
70
|
+
}] = I();
|
|
71
|
+
return d(() => d(() => typeof t.children == "string")() ? r(t.key, t.children, t.options) : P({
|
|
133
72
|
i18n: n(),
|
|
134
|
-
t:
|
|
73
|
+
t: r,
|
|
135
74
|
props: t
|
|
136
|
-
},
|
|
75
|
+
}, C(() => t.children)()));
|
|
137
76
|
};
|
|
138
77
|
export {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
78
|
+
R as Trans,
|
|
79
|
+
N as TransProvider,
|
|
80
|
+
I as useTransContext
|
|
142
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;AAAAA,EAAAA,CACD;AAEL;AAEO,MAAMW,IAAkBA,MAAMC,EAAWzB,CAAY,GAE/C0B,IAA4FC,CAAAA,MACvGC,EACG5B,EAAa6B,UAAQ;AAAA,EAAA,IACpBC,QAAK;AAAA,WAAE5B,EAA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AE,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
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
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,EAAIC,eAC9BH,EAAQI,UAAYL,EAASM,EAAI,IAAM,IAC3C,EAEAN,EAASO,GAAG,SAAU,IAAMJ,EAAa,IAAMH,EAASM,CAAC,CAAC,EAC1DN,EAASQ,KAAKP,EAAS,CAAC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AS,IAAML,EAAMK,QAAQ,GAAa,CAGnG"}
|
package/dist/ssr.mjs
CHANGED
|
@@ -1,87 +1,26 @@
|
|
|
1
|
-
import { useContext as
|
|
2
|
-
import { createComponent as
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const i = v(r), u = v(s);
|
|
12
|
-
o = new RegExp(`${i}\\s*(\\w+)\\s*${u}`, "g"), R.set(a, o);
|
|
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);
|
|
13
11
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
let r = m(c.language, $, t) ?? (typeof e == "string" ? e : t);
|
|
17
|
-
return n || typeof e == "object" && e !== null ? E(r, n || e) : r;
|
|
18
|
-
}, M = (t, e) => {
|
|
19
|
-
S[t] = e;
|
|
20
|
-
}, B = (t, e) => {
|
|
21
|
-
f = T, t && (f = { ...t, interpolation: t?.interpolation || {} }, f.interpolation.prefix = t.interpolation?.prefix || "{{", f.interpolation.suffix = t.interpolation?.suffix || "}}"), x = t?.lng || "", c.store = { data: f.resources || {} }, f.resources = {}, e?.(null, h);
|
|
22
|
-
}, F = (t) => (x = t, Promise.resolve(h)), b = (t, e, n = !0) => {
|
|
23
|
-
const r = t;
|
|
24
|
-
for (const s in e) {
|
|
25
|
-
const a = r[s], o = e[s];
|
|
26
|
-
o && typeof o == "object" && !Array.isArray(o) ? ((typeof a != "object" || Array.isArray(a)) && (r[s] = {}), b(r[s], o, n)) : (n || a === void 0) && (r[s] = o);
|
|
12
|
+
function o(a, i, g, u = {}) {
|
|
13
|
+
return t.addResourceBundle(a, i, g, u.deep, u.overwrite);
|
|
27
14
|
}
|
|
28
|
-
return
|
|
29
|
-
|
|
30
|
-
c.store = c.store || { data: {} }, c.store.data = c.store.data || {}, c.store.data[t] = c.store.data[t] || {}, c.store.data[t][e] = c.store.data[t][e] || {};
|
|
31
|
-
const a = c.store.data[t][e], o = n;
|
|
32
|
-
return r ? b(a, o, s) : c.store.data[t][e] = s ? o : { ...o, ...a }, (S.loaded || (() => {
|
|
33
|
-
}))(t, e), c;
|
|
34
|
-
}, m = (t, e, n, r) => {
|
|
35
|
-
const s = c.store;
|
|
36
|
-
if (!s) return;
|
|
37
|
-
const a = s.data?.[t];
|
|
38
|
-
if (!a) return;
|
|
39
|
-
const o = a[e];
|
|
40
|
-
if (!o || typeof o == "string") return;
|
|
41
|
-
const i = r?.keySeparator ?? c.options?.keySeparator ?? ".";
|
|
42
|
-
if (i === !1) return o[n];
|
|
43
|
-
const u = String(i);
|
|
44
|
-
if (n.indexOf(u) === -1) return o[n];
|
|
45
|
-
let l = o, y = 0;
|
|
46
|
-
for (let d = 0; d <= n.length; d++)
|
|
47
|
-
if (d === n.length || n[d] === u) {
|
|
48
|
-
const C = n.slice(y, d);
|
|
49
|
-
if (l == null || !(C in l)) return;
|
|
50
|
-
l = l[C], y = d + 1;
|
|
51
|
-
}
|
|
52
|
-
return l;
|
|
53
|
-
}, V = () => c, c = {
|
|
54
|
-
get language() {
|
|
55
|
-
return x;
|
|
56
|
-
},
|
|
57
|
-
createInstance: V,
|
|
58
|
-
t: h,
|
|
59
|
-
on: M,
|
|
60
|
-
init: B,
|
|
61
|
-
changeLanguage: F,
|
|
62
|
-
addResourceBundle: J,
|
|
63
|
-
options: f,
|
|
64
|
-
getResource: m
|
|
65
|
-
}, j = I();
|
|
66
|
-
function X(t, e) {
|
|
67
|
-
const [n, r] = P(e.resources ? t.t : () => null);
|
|
68
|
-
t.on("loaded", () => r(() => t.t)), t.init(e, (o, i) => r(() => i));
|
|
69
|
-
async function s(o) {
|
|
70
|
-
const i = await t.changeLanguage(o);
|
|
71
|
-
r(() => i);
|
|
72
|
-
}
|
|
73
|
-
function a(o, i, u, l = {}) {
|
|
74
|
-
return t.addResourceBundle(o, i, u, l.deep, l.overwrite);
|
|
75
|
-
}
|
|
76
|
-
return [((...o) => n().apply(null, o)), {
|
|
77
|
-
addResources: a,
|
|
15
|
+
return [((...a) => n().apply(null, a)), {
|
|
16
|
+
addResources: o,
|
|
78
17
|
getI18next: () => t,
|
|
79
|
-
changeLanguage:
|
|
18
|
+
changeLanguage: c
|
|
80
19
|
}];
|
|
81
20
|
}
|
|
82
|
-
const
|
|
21
|
+
const w = () => h(d), J = (t) => x(d.Provider, {
|
|
83
22
|
get value() {
|
|
84
|
-
return
|
|
23
|
+
return T(t.instance || C, {
|
|
85
24
|
lng: t.lng,
|
|
86
25
|
...t.options
|
|
87
26
|
});
|
|
@@ -89,54 +28,55 @@ const H = () => A(j), U = (t) => L(j.Provider, {
|
|
|
89
28
|
get children() {
|
|
90
29
|
return t.children;
|
|
91
30
|
}
|
|
92
|
-
}),
|
|
93
|
-
const
|
|
94
|
-
if (
|
|
95
|
-
if (
|
|
96
|
-
const
|
|
97
|
-
|
|
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));
|
|
98
37
|
}
|
|
99
|
-
return
|
|
38
|
+
return e;
|
|
100
39
|
};
|
|
101
|
-
let
|
|
40
|
+
let s;
|
|
102
41
|
(async () => {
|
|
103
42
|
try {
|
|
104
|
-
|
|
43
|
+
s = (await import("html-parse-string")).parse;
|
|
105
44
|
} catch {
|
|
106
45
|
}
|
|
107
46
|
})();
|
|
108
|
-
const
|
|
47
|
+
const v = () => {
|
|
109
48
|
console.error(
|
|
110
49
|
"In order to use JSX nesting, install %chtml-parse-string",
|
|
111
50
|
"font-weight: 700",
|
|
112
51
|
"https://github.com/ryansolid/html-parse-string."
|
|
113
52
|
);
|
|
114
|
-
},
|
|
115
|
-
const
|
|
116
|
-
if (!n.children) return
|
|
117
|
-
if (
|
|
118
|
-
if (!
|
|
119
|
-
|
|
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();
|
|
120
59
|
return;
|
|
121
60
|
}
|
|
122
61
|
try {
|
|
123
|
-
const [
|
|
124
|
-
return
|
|
125
|
-
} catch (
|
|
126
|
-
console.error(
|
|
62
|
+
const [o] = s(`<0>${c}</0>`);
|
|
63
|
+
return e.map(I(o, t));
|
|
64
|
+
} catch (o) {
|
|
65
|
+
console.error(o);
|
|
127
66
|
}
|
|
128
|
-
},
|
|
129
|
-
const [
|
|
67
|
+
}, N = (t) => {
|
|
68
|
+
const [r, {
|
|
130
69
|
getI18next: n
|
|
131
|
-
}] =
|
|
132
|
-
return typeof t.children == "string" ?
|
|
70
|
+
}] = w();
|
|
71
|
+
return typeof t.children == "string" ? r(t.key, t.children, t.options) : L({
|
|
133
72
|
i18n: n(),
|
|
134
|
-
t:
|
|
73
|
+
t: r,
|
|
135
74
|
props: t
|
|
136
|
-
},
|
|
75
|
+
}, m(() => t.children)());
|
|
137
76
|
};
|
|
138
77
|
export {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
78
|
+
N as Trans,
|
|
79
|
+
J as TransProvider,
|
|
80
|
+
w as useTransContext
|
|
142
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,EAC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,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,SAEK,OAAOG,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",
|
|
@@ -50,6 +51,7 @@
|
|
|
50
51
|
"name": "@nerimity/solid-i18lite",
|
|
51
52
|
"peerDependencies": {
|
|
52
53
|
"html-parse-string": "<=1.x",
|
|
54
|
+
"@nerimity/i18lite": "<=1.x",
|
|
53
55
|
"solid-js": ">=1.8.x"
|
|
54
56
|
},
|
|
55
57
|
"peerDependenciesMeta": {
|
|
@@ -70,5 +72,5 @@
|
|
|
70
72
|
"sideEffects": false,
|
|
71
73
|
"type": "module",
|
|
72
74
|
"types": "./dist/types/index.d.ts",
|
|
73
|
-
"version": "1.
|
|
75
|
+
"version": "1.6.0"
|
|
74
76
|
}
|
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;
|