@digitaldefiance/i18n-lib 1.0.8 → 1.0.9

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/index.d.ts CHANGED
@@ -3,4 +3,5 @@ export * from './i18n-engine';
3
3
  export * from './enum-registry';
4
4
  export * from './context';
5
5
  export * from './utils';
6
+ export * from './template';
6
7
  export { I18nEngine as I18n } from './i18n-engine';
package/dist/index.js CHANGED
@@ -20,6 +20,7 @@ __exportStar(require("./i18n-engine"), exports);
20
20
  __exportStar(require("./enum-registry"), exports);
21
21
  __exportStar(require("./context"), exports);
22
22
  __exportStar(require("./utils"), exports);
23
+ __exportStar(require("./template"), exports);
23
24
  // Re-export for convenience
24
25
  var i18n_engine_1 = require("./i18n-engine");
25
26
  Object.defineProperty(exports, "I18n", { enumerable: true, get: function () { return i18n_engine_1.I18nEngine; } });
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Template processing utilities for i18n
3
+ */
4
+ export type EnumKeys<T> = keyof T;
5
+ export type IsValidEnumTemplate<T extends string, TEnum> = T extends `${string}{{${string}.${infer Key}}}${infer Rest}` ? Key extends EnumKeys<TEnum> ? IsValidEnumTemplate<Rest, TEnum> : false : true;
6
+ /**
7
+ * Template function that processes {{EnumName.EnumKey}} patterns
8
+ */
9
+ export declare function createTemplateProcessor<TEnum extends Record<string, string>, TLanguage extends string>(enumObj: TEnum, translateFn: (key: TEnum[keyof TEnum], vars?: Record<string, string | number>, language?: TLanguage) => string, enumName: string): <T extends string>(str: IsValidEnumTemplate<T, TEnum> extends true ? T : never, language?: TLanguage, ...otherVars: Record<string, string | number>[]) => string;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ /**
3
+ * Template processing utilities for i18n
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createTemplateProcessor = createTemplateProcessor;
7
+ /**
8
+ * Template function that processes {{EnumName.EnumKey}} patterns
9
+ */
10
+ function createTemplateProcessor(enumObj, translateFn, enumName) {
11
+ return function t(str, language, ...otherVars) {
12
+ let varIndex = 0;
13
+ const pattern = new RegExp(`\\{\\{${enumName}\\.(\\w+)\\}\\}`, 'g');
14
+ // First replace enum patterns
15
+ let result = str.replace(pattern, (match, enumKey) => {
16
+ const enumValue = enumObj[enumKey];
17
+ if (!enumValue) {
18
+ return match; // Return original if enum key not found
19
+ }
20
+ const needsVars = enumValue.toLowerCase().endsWith('template');
21
+ const vars = needsVars ? otherVars[varIndex++] ?? {} : {};
22
+ return translateFn(enumValue, vars, language);
23
+ });
24
+ // Then replace any remaining variables from all otherVars
25
+ const allVars = otherVars.reduce((acc, vars) => ({ ...acc, ...vars }), {});
26
+ result = result.replace(/\{(\w+)\}/g, (match, varName) => {
27
+ return allVars[varName] !== undefined ? String(allVars[varName]) : match;
28
+ });
29
+ return result;
30
+ };
31
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digitaldefiance/i18n-lib",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "Generic i18n library with enum translation support",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",