i18n-keyless-core 1.15.1 → 1.16.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/dist/package.json +1 -1
- package/dist/service.js +11 -1
- package/dist/types.d.ts +9 -2
- package/package.json +1 -1
package/dist/package.json
CHANGED
package/dist/service.js
CHANGED
|
@@ -28,7 +28,17 @@ export function getTranslationCore(key, store, options) {
|
|
|
28
28
|
if (!translation) {
|
|
29
29
|
translateKey(key, store, options);
|
|
30
30
|
}
|
|
31
|
-
|
|
31
|
+
if (!options?.replace) {
|
|
32
|
+
return translation || key;
|
|
33
|
+
}
|
|
34
|
+
// Create a regex that matches all keys to replace
|
|
35
|
+
// Escape special regex characters in keys
|
|
36
|
+
const pattern = Object.keys(options.replace)
|
|
37
|
+
.map((key) => key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
|
|
38
|
+
.join("|");
|
|
39
|
+
const regex = new RegExp(pattern, "g");
|
|
40
|
+
// Replace all occurrences in a single pass
|
|
41
|
+
return translation.replace(regex, (matched) => options.replace?.[matched] || matched);
|
|
32
42
|
}
|
|
33
43
|
const translating = {};
|
|
34
44
|
/**
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type PrimaryLang = "fr" | "en";
|
|
2
|
-
export type Lang = "fr" | "en" | "nl" | "it" | "de" | "es" | "pl" | "pt" | "ro" | "hu" | "sv" | "tr" | "ja" | "cn" | "ru" | "ko" | "ar";
|
|
2
|
+
export type Lang = "fr" | "en" | "nl" | "it" | "de" | "es" | "pl" | "pt" | "ro" | "hu" | "sv" | "tr" | "ja" | "cn" | "cz" | "ru" | "ko" | "ar";
|
|
3
3
|
/**
|
|
4
4
|
* The translations for a key
|
|
5
5
|
* { "un text": "a text" }
|
|
@@ -35,7 +35,7 @@ export type LanguagesConfig = {
|
|
|
35
35
|
/**
|
|
36
36
|
* the languages supported for the user.
|
|
37
37
|
* For now we support:
|
|
38
|
-
* fr, nl, it, de, es, pl, pt, ro, hu, sv, tr, ja, cn, ru, ko, ar
|
|
38
|
+
* fr, nl, it, de, es, pl, pt, ro, hu, sv, tr, ja, cn, cz, ru, ko, ar
|
|
39
39
|
*
|
|
40
40
|
* If you need more, please reach out to @ambroselli_io on X/Twitter or by mail at arnaud.ambroselli.io@gmail.com
|
|
41
41
|
*/
|
|
@@ -66,6 +66,13 @@ export type TranslationOptions = {
|
|
|
66
66
|
* You can leave it there forever, or remove it once your translation is saved.
|
|
67
67
|
*/
|
|
68
68
|
forceTemporary?: Partial<Record<Lang, string>>;
|
|
69
|
+
/**
|
|
70
|
+
* The keys to replace in the text.
|
|
71
|
+
* It's an object where the key is the placeholder and the value is the replacement.
|
|
72
|
+
* Example: { "{{name}}": "John" } will replace all the {{name}} in the text with "John".
|
|
73
|
+
* RegEx is `key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))` so you can use use your own syntax.
|
|
74
|
+
*/
|
|
75
|
+
replace?: Record<string, string>;
|
|
69
76
|
};
|
|
70
77
|
export interface I18nKeylessRequestBody {
|
|
71
78
|
key: string;
|