i18n-keyless-react 1.4.0 → 1.5.1
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/dist/I18nKeylessText.d.ts +11 -0
- package/dist/I18nKeylessText.js +40 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/package.json +1 -1
- package/dist/store.js +16 -14
- package/package.json +1 -1
- package/dist/I18nText.d.ts +0 -6
- package/dist/I18nText.js +0 -18
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
interface I18nTextProps {
|
|
3
|
+
children: string;
|
|
4
|
+
}
|
|
5
|
+
export declare const I18nKeylessText: React.FC<I18nTextProps>;
|
|
6
|
+
interface I18nKeylessTextWithReplacementProps {
|
|
7
|
+
children: string;
|
|
8
|
+
replace?: Record<string, string>;
|
|
9
|
+
}
|
|
10
|
+
export declare const I18nKeylessTextWithReplacement: React.FC<I18nKeylessTextWithReplacementProps>;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import React, { useEffect, useMemo } from "react";
|
|
3
|
+
import { useI18nKeyless } from "./store";
|
|
4
|
+
export const I18nKeylessText = ({ children }) => {
|
|
5
|
+
const translations = useI18nKeyless((store) => store.translations);
|
|
6
|
+
const currentLanguage = useI18nKeyless((store) => store.currentLanguage);
|
|
7
|
+
const config = useI18nKeyless((store) => store.config);
|
|
8
|
+
const translateKey = useI18nKeyless((store) => store.translateKey);
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
translateKey(children);
|
|
11
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
12
|
+
}, [children, currentLanguage]);
|
|
13
|
+
const translatedText = currentLanguage === config.languages.primary ? children : translations[children] || children;
|
|
14
|
+
return _jsx(React.Fragment, { children: translatedText }, currentLanguage);
|
|
15
|
+
};
|
|
16
|
+
export const I18nKeylessTextWithReplacement = ({ children, replace, }) => {
|
|
17
|
+
const translations = useI18nKeyless((store) => store.translations);
|
|
18
|
+
const currentLanguage = useI18nKeyless((store) => store.currentLanguage);
|
|
19
|
+
const config = useI18nKeyless((store) => store.config);
|
|
20
|
+
const translateKey = useI18nKeyless((store) => store.translateKey);
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
translateKey(children);
|
|
23
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
24
|
+
}, [children, currentLanguage]);
|
|
25
|
+
const translatedText = currentLanguage === config.languages.primary ? children : translations[children] || children;
|
|
26
|
+
const replacedText = useMemo(() => {
|
|
27
|
+
if (!replace) {
|
|
28
|
+
return translatedText;
|
|
29
|
+
}
|
|
30
|
+
// Create a regex that matches all keys to replace
|
|
31
|
+
// Escape special regex characters in keys
|
|
32
|
+
const pattern = Object.keys(replace)
|
|
33
|
+
.map((key) => key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
|
|
34
|
+
.join("|");
|
|
35
|
+
const regex = new RegExp(pattern, "g");
|
|
36
|
+
// Replace all occurrences in a single pass
|
|
37
|
+
return translatedText.replace(regex, (matched) => replace[matched] || matched);
|
|
38
|
+
}, [translatedText, replace]);
|
|
39
|
+
return _jsx(React.Fragment, { children: replacedText }, currentLanguage);
|
|
40
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { I18nKeylessText, I18nKeylessTextWithReplacement } from "./I18nKeylessText";
|
|
2
2
|
export { init, getTranslation, setCurrentLanguage, useCurrentLanguage, clearI18nKeylessStorage, fetchAllTranslations, } from "./store";
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { I18nKeylessText, I18nKeylessTextWithReplacement } from "./I18nKeylessText";
|
|
2
2
|
export { init, getTranslation, setCurrentLanguage, useCurrentLanguage, clearI18nKeylessStorage, fetchAllTranslations, } from "./store";
|
package/dist/package.json
CHANGED
package/dist/store.js
CHANGED
|
@@ -66,24 +66,29 @@ export const useI18nKeyless = create((set, get) => ({
|
|
|
66
66
|
storage: null,
|
|
67
67
|
_hydrate: async () => {
|
|
68
68
|
const storage = get().config?.storage;
|
|
69
|
+
const debug = get().config?.debug;
|
|
69
70
|
if (!storage) {
|
|
70
71
|
throw new Error("i18n-keyless: storage is not initialized");
|
|
71
72
|
}
|
|
72
73
|
const translations = await getItem(storeKeys.translations, storage);
|
|
73
74
|
if (translations) {
|
|
74
|
-
|
|
75
|
+
if (debug)
|
|
76
|
+
console.log("i18n-keyless: _hydrate", translations);
|
|
75
77
|
set({ translations: JSON.parse(translations) });
|
|
76
78
|
}
|
|
77
79
|
else {
|
|
78
|
-
|
|
80
|
+
if (debug)
|
|
81
|
+
console.log("i18n-keyless: _hydrate: no translations");
|
|
79
82
|
}
|
|
80
83
|
const currentLanguage = await getItem(storeKeys.currentLanguage, storage);
|
|
81
84
|
if (currentLanguage) {
|
|
82
|
-
|
|
85
|
+
if (debug)
|
|
86
|
+
console.log("i18n-keyless: _hydrate", currentLanguage);
|
|
83
87
|
set({ currentLanguage: currentLanguage });
|
|
84
88
|
}
|
|
85
89
|
else {
|
|
86
|
-
|
|
90
|
+
if (debug)
|
|
91
|
+
console.log("i18n-keyless: _hydrate: no current language");
|
|
87
92
|
set({ currentLanguage: get().config?.languages.initWithDefault });
|
|
88
93
|
}
|
|
89
94
|
const uniqueId = await getItem(storeKeys.uniqueId, storage);
|
|
@@ -112,10 +117,10 @@ export const useI18nKeyless = create((set, get) => ({
|
|
|
112
117
|
setItem(storeKeys.translations, JSON.stringify(nextTranslations), storage);
|
|
113
118
|
},
|
|
114
119
|
translateKey: (key) => {
|
|
115
|
-
if (key.length > 280) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
120
|
+
// if (key.length > 280) {
|
|
121
|
+
// console.error("i18n-keyless: Key length exceeds 280 characters limit:", key);
|
|
122
|
+
// return;
|
|
123
|
+
// }
|
|
119
124
|
const translation = get().translations[key];
|
|
120
125
|
if (translation) {
|
|
121
126
|
return;
|
|
@@ -125,7 +130,6 @@ export const useI18nKeyless = create((set, get) => ({
|
|
|
125
130
|
return;
|
|
126
131
|
}
|
|
127
132
|
queue.add(async () => {
|
|
128
|
-
// console.log("i18n-keyless: translateKey", key);
|
|
129
133
|
try {
|
|
130
134
|
if (get().translating[key]) {
|
|
131
135
|
return;
|
|
@@ -144,7 +148,6 @@ export const useI18nKeyless = create((set, get) => ({
|
|
|
144
148
|
};
|
|
145
149
|
const apiUrl = config.API_URL || "https://api.i18n-keyless.com";
|
|
146
150
|
const url = `${apiUrl}/translate`;
|
|
147
|
-
// console.log("i18n-keyless: POST", url);
|
|
148
151
|
const response = await fetch(url, {
|
|
149
152
|
method: "POST",
|
|
150
153
|
headers: {
|
|
@@ -169,8 +172,10 @@ export const useI18nKeyless = create((set, get) => ({
|
|
|
169
172
|
}, { priority: 1, id: key });
|
|
170
173
|
},
|
|
171
174
|
setLanguage: async (lang) => {
|
|
172
|
-
console.log("i18n-keyless: setLanguage", lang);
|
|
173
175
|
const config = get().config;
|
|
176
|
+
const debug = get().config?.debug;
|
|
177
|
+
if (debug)
|
|
178
|
+
console.log("i18n-keyless: setLanguage", lang);
|
|
174
179
|
const sanitizedLang = !config || !config.languages.supported.includes(lang) ? config?.languages.fallback : lang;
|
|
175
180
|
set({ currentLanguage: sanitizedLang });
|
|
176
181
|
if (config?.storage) {
|
|
@@ -183,7 +188,6 @@ export const useI18nKeyless = create((set, get) => ({
|
|
|
183
188
|
},
|
|
184
189
|
}));
|
|
185
190
|
export async function init(config) {
|
|
186
|
-
// console.log("i18n-keyless: init", config);
|
|
187
191
|
if (!config.languages) {
|
|
188
192
|
throw new Error("i18n-keyless: languages is required");
|
|
189
193
|
}
|
|
@@ -229,7 +233,6 @@ export function setCurrentLanguage(lang) {
|
|
|
229
233
|
return useI18nKeyless.getState().setLanguage(lang);
|
|
230
234
|
}
|
|
231
235
|
export async function fetchAllTranslations(targetLanguage) {
|
|
232
|
-
// console.log("i18n-keyless: fetchAllTranslations", targetLanguage);
|
|
233
236
|
const store = useI18nKeyless.getState();
|
|
234
237
|
const config = store.config;
|
|
235
238
|
if (!config) {
|
|
@@ -237,7 +240,6 @@ export async function fetchAllTranslations(targetLanguage) {
|
|
|
237
240
|
return;
|
|
238
241
|
}
|
|
239
242
|
if (config.languages.primary === targetLanguage) {
|
|
240
|
-
// console.log("i18n-keyless: using primary language");
|
|
241
243
|
return;
|
|
242
244
|
}
|
|
243
245
|
try {
|
package/package.json
CHANGED
package/dist/I18nText.d.ts
DELETED
package/dist/I18nText.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import React, { useEffect } from "react";
|
|
3
|
-
import { useI18nKeyless } from "./store";
|
|
4
|
-
export const I18nText = ({ children }) => {
|
|
5
|
-
const translations = useI18nKeyless((store) => store.translations);
|
|
6
|
-
const currentLanguage = useI18nKeyless((store) => store.currentLanguage);
|
|
7
|
-
const config = useI18nKeyless((store) => store.config);
|
|
8
|
-
const translateKey = useI18nKeyless((store) => store.translateKey);
|
|
9
|
-
useEffect(() => {
|
|
10
|
-
translateKey(children);
|
|
11
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
12
|
-
}, [children, currentLanguage]);
|
|
13
|
-
if (!config) {
|
|
14
|
-
return null;
|
|
15
|
-
}
|
|
16
|
-
const translatedText = currentLanguage === config.languages.primary ? children : translations[children] || children;
|
|
17
|
-
return _jsx(React.Fragment, { children: translatedText }, currentLanguage);
|
|
18
|
-
};
|