@digitaldefiance/i18n-lib 1.0.0 → 1.0.1

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 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/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;
package/package.json CHANGED
@@ -1,15 +1,19 @@
1
1
  {
2
2
  "name": "@digitaldefiance/i18n-lib",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
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",
9
+ "test": "yarn jest",
10
10
  "prepublishOnly": "yarn build"
11
11
  },
12
12
  "devDependencies": {
13
+ "@types/jest": "^29.0.0",
14
+ "jest": "^29.0.0",
15
+ "jest-util": "^30.0.5",
16
+ "ts-jest": "^29.0.0",
13
17
  "typescript": "^5.9.2"
14
18
  },
15
19
  "files": [
@@ -25,5 +29,5 @@
25
29
  ],
26
30
  "author": "Digital Defiance",
27
31
  "license": "MIT",
28
- "packageManager": "yarn@4.9.2+sha512.1fc009bc09d13cfd0e19efa44cbfc2b9cf6ca61482725eb35bbc5e257e093ebf4130db6dfe15d604ff4b79efd8e1e8e99b25fa7d0a6197c9f9826358d4d65c3c"
32
+ "packageManager": "yarn@4.10.3"
29
33
  }