i18n-keyless-core 1.15.0 → 1.15.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "i18n-keyless-core",
3
3
  "private": false,
4
- "version": "1.15.0",
4
+ "version": "1.15.2",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",
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
- return translation || key;
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
@@ -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;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "i18n-keyless-core",
3
3
  "private": false,
4
- "version": "1.15.0",
4
+ "version": "1.15.2",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",