@digitaldefiance/i18n-lib 1.2.4 → 1.2.5
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 +22 -0
- package/dist/create-translation-adapter.d.ts +20 -0
- package/dist/create-translation-adapter.js +36 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1059,6 +1059,28 @@ Part of the DigitalBurnbag project - a secure file sharing and automated protoco
|
|
|
1059
1059
|
|
|
1060
1060
|
## ChangeLog
|
|
1061
1061
|
|
|
1062
|
+
### Version 1.2.5
|
|
1063
|
+
|
|
1064
|
+
- Sat Oct 25 2025 15:0100 GMT-0700 (Pacific Daylight Time)
|
|
1065
|
+
|
|
1066
|
+
#### Added
|
|
1067
|
+
- **`createTranslationAdapter`** - Generic utility function to adapt `PluginI18nEngine` instances to the `TranslationEngine` interface, enabling seamless integration with error classes and other components expecting the simpler interface
|
|
1068
|
+
- Maintains full type safety with generic string key and language types
|
|
1069
|
+
- Provides graceful error handling with fallback to key strings
|
|
1070
|
+
- Zero overhead - direct delegation to underlying `PluginI18nEngine`
|
|
1071
|
+
- Comprehensive test coverage (19 tests)
|
|
1072
|
+
|
|
1073
|
+
#### Benefits
|
|
1074
|
+
- Eliminates need for custom adapter implementations in consuming packages
|
|
1075
|
+
- Standardizes translation engine integration across the monorepo
|
|
1076
|
+
- Simplifies error class constructors that require translation engines
|
|
1077
|
+
|
|
1078
|
+
#### Migration
|
|
1079
|
+
Packages using custom translation adapters can now replace them with:
|
|
1080
|
+
```typescript
|
|
1081
|
+
import { createTranslationAdapter } from '@digitaldefiance/i18n-lib';
|
|
1082
|
+
const adapter = createTranslationAdapter(pluginEngine, 'component-id');
|
|
1083
|
+
|
|
1062
1084
|
### Version 1.2.4
|
|
1063
1085
|
|
|
1064
1086
|
- Sat Oct 25 2025 14:29:00 GMT-0700 (Pacific Daylight Time)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { PluginI18nEngine } from './plugin-i18n-engine';
|
|
2
|
+
import { TranslationEngine } from './translation-engine';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a TranslationEngine adapter from a PluginI18nEngine for a specific component.
|
|
5
|
+
* This allows PluginI18nEngine to be used where TranslationEngine interface is expected.
|
|
6
|
+
*
|
|
7
|
+
* @param pluginEngine - The PluginI18nEngine instance to wrap
|
|
8
|
+
* @param componentId - The component ID to use for translations
|
|
9
|
+
* @returns A TranslationEngine that delegates to the PluginI18nEngine
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const pluginEngine = getMyPluginI18nEngine();
|
|
14
|
+
* const adapter = createTranslationAdapter(pluginEngine, 'my-component');
|
|
15
|
+
*
|
|
16
|
+
* // Now can be used where TranslationEngine is expected
|
|
17
|
+
* const error = new MyError(errorType, adapter);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare function createTranslationAdapter<TStringKey extends string, TLanguage extends string>(pluginEngine: PluginI18nEngine<TLanguage>, componentId: string): TranslationEngine<TStringKey>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createTranslationAdapter = createTranslationAdapter;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a TranslationEngine adapter from a PluginI18nEngine for a specific component.
|
|
6
|
+
* This allows PluginI18nEngine to be used where TranslationEngine interface is expected.
|
|
7
|
+
*
|
|
8
|
+
* @param pluginEngine - The PluginI18nEngine instance to wrap
|
|
9
|
+
* @param componentId - The component ID to use for translations
|
|
10
|
+
* @returns A TranslationEngine that delegates to the PluginI18nEngine
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const pluginEngine = getMyPluginI18nEngine();
|
|
15
|
+
* const adapter = createTranslationAdapter(pluginEngine, 'my-component');
|
|
16
|
+
*
|
|
17
|
+
* // Now can be used where TranslationEngine is expected
|
|
18
|
+
* const error = new MyError(errorType, adapter);
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
function createTranslationAdapter(pluginEngine, componentId) {
|
|
22
|
+
return {
|
|
23
|
+
translate: (key, vars, lang) => {
|
|
24
|
+
try {
|
|
25
|
+
return pluginEngine.translate(componentId, key, vars, lang);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
// Fallback to key if translation fails
|
|
29
|
+
return String(key);
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
safeTranslate: (key, vars, lang) => {
|
|
33
|
+
return pluginEngine.safeTranslate(componentId, key, vars, lang);
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -35,6 +35,7 @@ export * from './registry-error-type';
|
|
|
35
35
|
export * from './translation-engine';
|
|
36
36
|
export * from './translation-request';
|
|
37
37
|
export * from './translation-response';
|
|
38
|
+
export * from './create-translation-adapter';
|
|
38
39
|
export { createCoreI18nEngine as createCoreI18n } from './core-i18n';
|
|
39
40
|
export { I18nEngine as I18n } from './i18n-engine';
|
|
40
41
|
export { PluginI18nEngine as PluginI18n } from './plugin-i18n-engine';
|
package/dist/index.js
CHANGED
|
@@ -54,6 +54,7 @@ __exportStar(require("./registry-error-type"), exports);
|
|
|
54
54
|
__exportStar(require("./translation-engine"), exports);
|
|
55
55
|
__exportStar(require("./translation-request"), exports);
|
|
56
56
|
__exportStar(require("./translation-response"), exports);
|
|
57
|
+
__exportStar(require("./create-translation-adapter"), exports);
|
|
57
58
|
// Re-export for convenience
|
|
58
59
|
var core_i18n_1 = require("./core-i18n");
|
|
59
60
|
Object.defineProperty(exports, "createCoreI18n", { enumerable: true, get: function () { return core_i18n_1.createCoreI18nEngine; } });
|