@digitaldefiance/i18n-lib 1.0.0 → 1.0.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/README.md +112 -0
- package/dist/enum-registry.d.ts +2 -2
- package/dist/i18n-engine.d.ts +2 -2
- package/dist/types.d.ts +20 -4
- package/dist/types.js +7 -0
- package/package.json +9 -4
package/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# @digitaldefiance/i18n-lib
|
|
2
|
+
|
|
3
|
+
A generic TypeScript i18n library with enum translation support and template variable replacement.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Generic Design**: Works with any string and language enums
|
|
8
|
+
- **Template Variables**: Replace `{variable}` placeholders in strings
|
|
9
|
+
- **Enum Translation**: Translate enum values with type safety
|
|
10
|
+
- **Context Support**: Admin vs user translation contexts
|
|
11
|
+
- **Fallback Languages**: Graceful degradation when translations missing
|
|
12
|
+
- **Zero Dependencies**: Lightweight with no external dependencies
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @digitaldefiance/i18n-lib
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { I18nEngine, I18nConfig, createContext } from '@digitaldefiance/i18n-lib';
|
|
24
|
+
|
|
25
|
+
// Define your enums
|
|
26
|
+
enum MyStrings {
|
|
27
|
+
Welcome = 'welcome',
|
|
28
|
+
UserGreetingTemplate = 'userGreetingTemplate'
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
enum MyLanguages {
|
|
32
|
+
English = 'English',
|
|
33
|
+
Spanish = 'Español'
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Configure the engine
|
|
37
|
+
const config: I18nConfig<MyStrings, MyLanguages> = {
|
|
38
|
+
strings: {
|
|
39
|
+
[MyLanguages.English]: {
|
|
40
|
+
[MyStrings.Welcome]: 'Welcome!',
|
|
41
|
+
[MyStrings.UserGreetingTemplate]: 'Hello, {name}!'
|
|
42
|
+
},
|
|
43
|
+
[MyLanguages.Spanish]: {
|
|
44
|
+
[MyStrings.Welcome]: '¡Bienvenido!',
|
|
45
|
+
[MyStrings.UserGreetingTemplate]: '¡Hola, {name}!'
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
defaultLanguage: MyLanguages.English,
|
|
49
|
+
languageCodes: {
|
|
50
|
+
[MyLanguages.English]: 'en',
|
|
51
|
+
[MyLanguages.Spanish]: 'es'
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// Create engine and context
|
|
56
|
+
const i18n = new I18nEngine(config);
|
|
57
|
+
const context = createContext(MyLanguages.English);
|
|
58
|
+
|
|
59
|
+
// Translate strings
|
|
60
|
+
const welcome = i18n.translate(MyStrings.Welcome, context);
|
|
61
|
+
// "Welcome!"
|
|
62
|
+
|
|
63
|
+
const greeting = i18n.translate(MyStrings.UserGreetingTemplate, context, { name: 'John' });
|
|
64
|
+
// "Hello, John!"
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Enum Translation
|
|
68
|
+
```typescript
|
|
69
|
+
enum Status {
|
|
70
|
+
Active = 'active',
|
|
71
|
+
Inactive = 'inactive'
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Register enum translations
|
|
75
|
+
i18n.registerEnum(Status, {
|
|
76
|
+
[MyLanguages.English]: {
|
|
77
|
+
[Status.Active]: 'Active',
|
|
78
|
+
[Status.Inactive]: 'Inactive'
|
|
79
|
+
},
|
|
80
|
+
[MyLanguages.Spanish]: {
|
|
81
|
+
[Status.Active]: 'Activo',
|
|
82
|
+
[Status.Inactive]: 'Inactivo'
|
|
83
|
+
}
|
|
84
|
+
}, 'Status');
|
|
85
|
+
|
|
86
|
+
// Translate enum values
|
|
87
|
+
const statusText = i18n.translateEnum(Status, Status.Active, MyLanguages.Spanish);
|
|
88
|
+
// "Activo"
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## API Reference
|
|
92
|
+
### i18nEngine
|
|
93
|
+
- translate(key, context, vars?, language?, fallback?) - Translate string with optional variables
|
|
94
|
+
|
|
95
|
+
- translateEnum(enumObj, value, language) - Translate enum value
|
|
96
|
+
|
|
97
|
+
- registerEnum(enumObj, translations, name) - Register enum translations
|
|
98
|
+
|
|
99
|
+
- getLanguageCode(language) - Get language code for language
|
|
100
|
+
|
|
101
|
+
### Context Management
|
|
102
|
+
- createContext(defaultLanguage) - Create new context
|
|
103
|
+
|
|
104
|
+
- setLanguage(context, language) - Set user language
|
|
105
|
+
|
|
106
|
+
- setAdminLanguage(context, language) - Set admin language
|
|
107
|
+
|
|
108
|
+
- setContext(context, 'admin' | 'user') - Set context type
|
|
109
|
+
|
|
110
|
+
## License
|
|
111
|
+
|
|
112
|
+
MIT
|
package/dist/enum-registry.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EnumLanguageTranslation } from './types';
|
|
2
2
|
export declare class EnumTranslationRegistry<TLanguage extends string> {
|
|
3
3
|
private translations;
|
|
4
4
|
private enumNames;
|
|
5
|
-
register<TEnum extends string | number>(enumObj: Record<string, TEnum>, translations:
|
|
5
|
+
register<TEnum extends string | number>(enumObj: Record<string, TEnum>, translations: EnumLanguageTranslation<TEnum, TLanguage>, enumName: string): void;
|
|
6
6
|
translate<TEnum extends string | number>(enumObj: Record<string, TEnum>, value: TEnum, language: TLanguage): string;
|
|
7
7
|
private getEnumName;
|
|
8
8
|
has(enumObj: any): boolean;
|
package/dist/i18n-engine.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { I18nConfig, I18nContext } from './types';
|
|
1
|
+
import { I18nConfig, I18nContext, EnumLanguageTranslation } from './types';
|
|
2
2
|
export declare class I18nEngine<TStringKey extends string, TLanguage extends string> {
|
|
3
3
|
private config;
|
|
4
4
|
private enumRegistry;
|
|
5
5
|
constructor(config: I18nConfig<TStringKey, TLanguage>);
|
|
6
6
|
translate(key: TStringKey, context: I18nContext<TLanguage>, vars?: Record<string, string | number>, language?: TLanguage, fallbackLanguage?: TLanguage): string;
|
|
7
7
|
translateEnum<TEnum extends string | number>(enumObj: Record<string, TEnum>, value: TEnum, language: TLanguage): string;
|
|
8
|
-
registerEnum<TEnum extends string | number>(enumObj: Record<string, TEnum>, translations:
|
|
8
|
+
registerEnum<TEnum extends string | number>(enumObj: Record<string, TEnum>, translations: EnumLanguageTranslation<TEnum, TLanguage>, enumName: string): void;
|
|
9
9
|
private getString;
|
|
10
10
|
getLanguageCode(language: TLanguage): string;
|
|
11
11
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export type LanguageContext = 'admin' | 'user';
|
|
2
2
|
export type CurrencyPosition = 'prefix' | 'postfix' | 'infix';
|
|
3
|
-
export type StringsCollection<TStringKey extends string> = Record<TStringKey, string
|
|
4
|
-
export type MasterStringsCollection<TStringKey extends string, TLanguage extends string> = Record<TLanguage, StringsCollection<TStringKey
|
|
5
|
-
export type LanguageCodeCollection<TLanguage extends string> = Record<TLanguage, string
|
|
6
|
-
export type EnumTranslationMap<TEnum extends string | number, TLanguage extends string> = Record<TLanguage, Record<TEnum, string
|
|
3
|
+
export type StringsCollection<TStringKey extends string> = Partial<Record<TStringKey, string>>;
|
|
4
|
+
export type MasterStringsCollection<TStringKey extends string, TLanguage extends string> = Partial<Record<TLanguage, StringsCollection<TStringKey>>>;
|
|
5
|
+
export type LanguageCodeCollection<TLanguage extends string> = Partial<Record<TLanguage, string>>;
|
|
6
|
+
export type EnumTranslationMap<TEnum extends string | number, TLanguage extends string> = Partial<Record<TLanguage, Partial<Record<TEnum, string>>>>;
|
|
7
7
|
export interface I18nConfig<TStringKey extends string, TLanguage extends string> {
|
|
8
8
|
strings: MasterStringsCollection<TStringKey, TLanguage>;
|
|
9
9
|
defaultLanguage: TLanguage;
|
|
@@ -15,3 +15,19 @@ export interface I18nContext<TLanguage extends string> {
|
|
|
15
15
|
adminLanguage: TLanguage;
|
|
16
16
|
currentContext: LanguageContext;
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Generic translation type for any enumeration
|
|
20
|
+
*/
|
|
21
|
+
export type EnumTranslation<T extends string | number> = {
|
|
22
|
+
[K in T]: string;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Generic language translation type for any enumeration
|
|
26
|
+
*/
|
|
27
|
+
export type EnumLanguageTranslation<T extends string | number, TLanguage extends string = string> = {
|
|
28
|
+
[L in TLanguage]: EnumTranslation<T>;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Helper function to create typed translations for an enumeration
|
|
32
|
+
*/
|
|
33
|
+
export declare function createTranslations<T extends string | number, TLanguage extends string>(translations: EnumLanguageTranslation<T, TLanguage>): EnumLanguageTranslation<T, TLanguage>;
|
package/dist/types.js
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createTranslations = createTranslations;
|
|
4
|
+
/**
|
|
5
|
+
* Helper function to create typed translations for an enumeration
|
|
6
|
+
*/
|
|
7
|
+
function createTranslations(translations) {
|
|
8
|
+
return translations;
|
|
9
|
+
}
|
package/package.json
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@digitaldefiance/i18n-lib",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Generic i18n library with enum translation support",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "yarn tsc",
|
|
9
|
-
"test": "jest",
|
|
10
|
-
"prepublishOnly": "yarn build"
|
|
9
|
+
"test": "yarn jest",
|
|
10
|
+
"prepublishOnly": "yarn build",
|
|
11
|
+
"publish": "npm publish --access public"
|
|
11
12
|
},
|
|
12
13
|
"devDependencies": {
|
|
14
|
+
"@types/jest": "^29.0.0",
|
|
15
|
+
"jest": "^29.0.0",
|
|
16
|
+
"jest-util": "^30.0.5",
|
|
17
|
+
"ts-jest": "^29.0.0",
|
|
13
18
|
"typescript": "^5.9.2"
|
|
14
19
|
},
|
|
15
20
|
"files": [
|
|
@@ -25,5 +30,5 @@
|
|
|
25
30
|
],
|
|
26
31
|
"author": "Digital Defiance",
|
|
27
32
|
"license": "MIT",
|
|
28
|
-
"packageManager": "yarn@4.
|
|
33
|
+
"packageManager": "yarn@4.10.3"
|
|
29
34
|
}
|