@gaurav_bhandari/common-frontend-services 1.0.0
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 +40 -0
- package/dist/bin/cli.js +22 -0
- package/dist/src/cli/commands/add.js +104 -0
- package/dist/src/cli/commands/list.js +19 -0
- package/dist/src/cli/registry.js +125 -0
- package/package.json +39 -0
- package/src/services/API-Service/baseApiService.ts +296 -0
- package/src/services/API-Service/csrfAPIService.ts +93 -0
- package/src/services/API-Service/docs/WhatIsBaseApiService.md +121 -0
- package/src/services/API-Service/docs/WhatIsCsrfApiService.md +67 -0
- package/src/services/Architecture-Reports-Service/architecture-checklist.md +54 -0
- package/src/services/Architecture-Reports-Service/report-generation-prompt.md +60 -0
- package/src/services/Authorization-Service/docs/WhatIsRBACService.md +112 -0
- package/src/services/Authorization-Service/permissions.ts +16 -0
- package/src/services/Authorization-Service/rbacService.ts +63 -0
- package/src/services/Authorization-Service/rolePermissions.ts +10 -0
- package/src/services/Authorization-Service/userPermissions/adminPermissions.ts +13 -0
- package/src/services/Cache-Service/README.md +50 -0
- package/src/services/Cache-Service/cacheService.ts +142 -0
- package/src/services/Cache-Service/simple-explanation.md +55 -0
- package/src/services/Cache-Service/sw-strategies.ts +51 -0
- package/src/services/Date-Service/dateService.ts +93 -0
- package/src/services/Date-Service/docs/WhatIsDateService.md +63 -0
- package/src/services/Environment-Config-Service/docs/WhatIsEnvironmentConfigService.md +68 -0
- package/src/services/Environment-Config-Service/environmentConfigService.ts +112 -0
- package/src/services/Environment-Config-Service/simple-explanation.md +49 -0
- package/src/services/Feature-Flag-Service/docs/WhatIsFeatureFlagService.md +98 -0
- package/src/services/Feature-Flag-Service/featureFlagService.ts +91 -0
- package/src/services/File-Service/docs/WhatIsFileService.md +78 -0
- package/src/services/File-Service/fileService.ts +224 -0
- package/src/services/File-Service/simple-explanation.md +49 -0
- package/src/services/Google-Login-Service/README.md +48 -0
- package/src/services/Google-Login-Service/googleLoginService.ts +209 -0
- package/src/services/GraphQl-Service/docs/WhatIsGraphQLService.md +101 -0
- package/src/services/GraphQl-Service/graphqlService.ts +81 -0
- package/src/services/Logger-Service/README.md +44 -0
- package/src/services/Logger-Service/loggerService.ts +125 -0
- package/src/services/Logger-Service/simple-explanation.md +43 -0
- package/src/services/Logger-Service/verify-logger.ts +62 -0
- package/src/services/Monitoring-Service/README.md +48 -0
- package/src/services/Monitoring-Service/monitoringService.ts +229 -0
- package/src/services/Monitoring-Service/simple-explanation.md +48 -0
- package/src/services/Monitoring-Service/verify-monitoring.ts +74 -0
- package/src/services/Security-Reports-Service/report-generation-prompt.md +52 -0
- package/src/services/Security-Reports-Service/security-checklist.md +66 -0
- package/src/services/Storage-Service/docs/WhatIsStorageService.md +70 -0
- package/src/services/Storage-Service/indexedDBService.ts +103 -0
- package/src/services/Storage-Service/localStorageService.ts +42 -0
- package/src/services/Storage-Service/sessionStorageService.ts +42 -0
- package/src/services/Validation-Service/form.ts +66 -0
- package/src/services/Validation-Service/rules.ts +338 -0
- package/src/services/Worker-Service/docs/WhatIsWorkerService.md +57 -0
- package/src/services/Worker-Service/workerService.ts +122 -0
- package/src/services/i18n-Service/docs/WhatIsI18nService.md +108 -0
- package/src/services/i18n-Service/i18nService.ts +127 -0
- package/src/services/i18n-Service/locales/en.json +17 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# I18nService
|
|
2
|
+
|
|
3
|
+
The `I18nService` provides a lightweight, framework-agnostic solution for managing translations and multiple languages in your application.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This service handles loading translation files (JSON), managing the active locale, and interpolating dynamic values into translated strings.
|
|
8
|
+
|
|
9
|
+
### Key Features
|
|
10
|
+
|
|
11
|
+
- **Lazy Loading**: Translation files are only loaded when needed.
|
|
12
|
+
- **Nested Keys**: Support for dot notation (e.g., `dashboard.title`).
|
|
13
|
+
- **Interpolation**: Simple value replacement (e.g., `Hello {name}`).
|
|
14
|
+
- **Framework Agnostice**: Can be used with any UI library.
|
|
15
|
+
|
|
16
|
+
## How to Use
|
|
17
|
+
|
|
18
|
+
### 1. Structure
|
|
19
|
+
|
|
20
|
+
Place your translation files in the `locales/` directory.
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
/i18n-Service/locales/
|
|
24
|
+
├── en.json
|
|
25
|
+
├── es.json
|
|
26
|
+
└── fr.json
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**Example `en.json`**:
|
|
30
|
+
|
|
31
|
+
```json
|
|
32
|
+
{
|
|
33
|
+
"welcome": "Welcome, {name}!",
|
|
34
|
+
"buttons": {
|
|
35
|
+
"save": "Save Changes"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### 2. Initialization
|
|
41
|
+
|
|
42
|
+
Initialize the service at your application entry point.
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { i18nService } from "@/i18n-Service/i18nService";
|
|
46
|
+
|
|
47
|
+
async function initApp() {
|
|
48
|
+
await i18nService.init("en"); // Sets default language
|
|
49
|
+
// mount app...
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 3. Usage
|
|
54
|
+
|
|
55
|
+
Use the `t()` method to translate keys.
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { i18nService } from "@/i18n-Service/i18nService";
|
|
59
|
+
|
|
60
|
+
// Simple key
|
|
61
|
+
const buttonLabel = i18nService.t("buttons.save"); // "Save Changes"
|
|
62
|
+
|
|
63
|
+
// With parameters
|
|
64
|
+
const welcomeMsg = i18nService.t("welcome", { name: "Alice" }); // "Welcome, Alice!"
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### 4. Changing Language
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
async function switchLanguage(lang: string) {
|
|
71
|
+
await i18nService.setLocale(lang);
|
|
72
|
+
// You might need to trigger a re-render in your framework
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Framework Integration
|
|
77
|
+
|
|
78
|
+
### Vue Plugin (Example)
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
// plugins/i18n.ts
|
|
82
|
+
import { i18nService } from "@/i18n-Service/i18nService";
|
|
83
|
+
|
|
84
|
+
export default {
|
|
85
|
+
install: (app) => {
|
|
86
|
+
app.config.globalProperties.$t = (key, params) =>
|
|
87
|
+
i18nService.t(key, params);
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### React Hook (Example)
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
// hooks/useI18n.ts
|
|
96
|
+
import { useState, useEffect } from "react";
|
|
97
|
+
import { i18nService } from "@/i18n-Service/i18nService";
|
|
98
|
+
|
|
99
|
+
export function useI18n() {
|
|
100
|
+
const [locale, setLocale] = useState(i18nService.getLocale());
|
|
101
|
+
|
|
102
|
+
const t = (key, params) => i18nService.t(key, params);
|
|
103
|
+
|
|
104
|
+
// Logic to listen for locale changes would go here
|
|
105
|
+
|
|
106
|
+
return { t, locale };
|
|
107
|
+
}
|
|
108
|
+
```
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
export type Locale = string;
|
|
2
|
+
export type Translations = Record<string, any>;
|
|
3
|
+
|
|
4
|
+
export class I18nService {
|
|
5
|
+
private static instance: I18nService;
|
|
6
|
+
private currentLocale: Locale = "en";
|
|
7
|
+
private translations: Record<Locale, Translations> = {};
|
|
8
|
+
private isInitialized = false;
|
|
9
|
+
private fallbackLocale: Locale = "en";
|
|
10
|
+
|
|
11
|
+
private constructor() {}
|
|
12
|
+
|
|
13
|
+
public static getInstance(): I18nService {
|
|
14
|
+
if (!I18nService.instance) {
|
|
15
|
+
I18nService.instance = new I18nService();
|
|
16
|
+
}
|
|
17
|
+
return I18nService.instance;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Initialize the service.
|
|
22
|
+
* @param defaultLocale The default language to start with.
|
|
23
|
+
*/
|
|
24
|
+
public async init(defaultLocale: Locale = "en"): Promise<void> {
|
|
25
|
+
if (this.isInitialized) return;
|
|
26
|
+
|
|
27
|
+
this.currentLocale = defaultLocale;
|
|
28
|
+
await this.loadLocale(defaultLocale);
|
|
29
|
+
|
|
30
|
+
if (defaultLocale !== this.fallbackLocale) {
|
|
31
|
+
// Optimistically load fallback just in case
|
|
32
|
+
this.loadLocale(this.fallbackLocale).catch(console.error);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
this.isInitialized = true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Change the current language.
|
|
40
|
+
* Fetches the file if not already loaded.
|
|
41
|
+
*/
|
|
42
|
+
public async setLocale(locale: Locale): Promise<void> {
|
|
43
|
+
if (this.currentLocale === locale) return;
|
|
44
|
+
|
|
45
|
+
if (!this.translations[locale]) {
|
|
46
|
+
await this.loadLocale(locale);
|
|
47
|
+
}
|
|
48
|
+
this.currentLocale = locale;
|
|
49
|
+
|
|
50
|
+
// Persist preference if needed (e.g. localStorage)
|
|
51
|
+
// localStorage.setItem('user-locale', locale);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public getLocale(): Locale {
|
|
55
|
+
return this.currentLocale;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Translate a key with optional dynamic parameters.
|
|
60
|
+
* Supports nested keys via dot notation: 'common.welcome'
|
|
61
|
+
* @param key The translation key
|
|
62
|
+
* @param params Object containing values to replace {name: 'John'}
|
|
63
|
+
*/
|
|
64
|
+
public t(key: string, params?: Record<string, string | number>): string {
|
|
65
|
+
const translation = this.getValue(
|
|
66
|
+
key,
|
|
67
|
+
this.translations[this.currentLocale],
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
if (translation === undefined) {
|
|
71
|
+
// Try fallback
|
|
72
|
+
const fallback = this.getValue(
|
|
73
|
+
key,
|
|
74
|
+
this.translations[this.fallbackLocale],
|
|
75
|
+
);
|
|
76
|
+
if (fallback !== undefined) {
|
|
77
|
+
return this.interpolate(fallback, params);
|
|
78
|
+
}
|
|
79
|
+
console.warn(`[i18n] Missing translation for key: ${key}`);
|
|
80
|
+
return key;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return this.interpolate(translation, params);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Helper to retrieve nested values
|
|
88
|
+
*/
|
|
89
|
+
private getValue(key: string, data: Translations): string | undefined {
|
|
90
|
+
if (!data) return undefined;
|
|
91
|
+
return key.split(".").reduce((obj, i) => obj?.[i], data);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Interpolate values into the string.
|
|
96
|
+
* "Hello {name}" + {name: "John"} => "Hello John"
|
|
97
|
+
*/
|
|
98
|
+
private interpolate(
|
|
99
|
+
text: string,
|
|
100
|
+
params?: Record<string, string | number>,
|
|
101
|
+
): string {
|
|
102
|
+
if (!params) return text;
|
|
103
|
+
|
|
104
|
+
return text.replace(/{(\w+)}/g, (match, paramName) => {
|
|
105
|
+
const value = params[paramName];
|
|
106
|
+
return value !== undefined ? String(value) : match;
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Load a locale file.
|
|
112
|
+
* You might need to adjust the import path pattern for your bundler (Vite/Webpack).
|
|
113
|
+
*/
|
|
114
|
+
private async loadLocale(locale: Locale): Promise<void> {
|
|
115
|
+
try {
|
|
116
|
+
// Vite glob import or dynamic import
|
|
117
|
+
// Note: Dynamic imports with variables often require a specific structure for bundlers to analyze
|
|
118
|
+
const messages = await import(`./locales/${locale}.json`);
|
|
119
|
+
this.translations[locale] = messages.default || messages;
|
|
120
|
+
} catch (error) {
|
|
121
|
+
console.error(`[i18n] Failed to load locale: ${locale}`, error);
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export const i18nService = I18nService.getInstance();
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"common": {
|
|
3
|
+
"welcome": "Welcome, {name}!",
|
|
4
|
+
"login": "Login",
|
|
5
|
+
"logout": "Logout",
|
|
6
|
+
"save": "Save",
|
|
7
|
+
"cancel": "Cancel"
|
|
8
|
+
},
|
|
9
|
+
"dashboard": {
|
|
10
|
+
"title": "Dashboard",
|
|
11
|
+
"stats": "Statistics"
|
|
12
|
+
},
|
|
13
|
+
"errors": {
|
|
14
|
+
"network": "Network error occurred.",
|
|
15
|
+
"required": "{field} is required."
|
|
16
|
+
}
|
|
17
|
+
}
|