@xxanderwp/translate-module 1.0.1 → 1.0.3
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 +115 -0
- package/dist/index.js +111 -0
- package/package.json +1 -1
- package/logo.png +0 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A generic, type-safe internationalization (i18n) core class.
|
|
3
|
+
*
|
|
4
|
+
* Manages a collection of language translation maps and provides utilities
|
|
5
|
+
* for switching the active language and resolving translated strings with
|
|
6
|
+
* optional interpolation placeholders.
|
|
7
|
+
*
|
|
8
|
+
* @template T - A record whose keys are language identifiers and whose values
|
|
9
|
+
* are flat string-to-string translation maps (e.g. `{ en: { hello: "Hello" }, fr: { hello: "Bonjour" } }`).
|
|
10
|
+
* @template LangKey - Union of the top-level keys of `T`, inferred automatically.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const lc = new LanguageCore({
|
|
14
|
+
* en: { greeting: "Hello, {0}!" },
|
|
15
|
+
* es: { greeting: "¡Hola, {0}!" },
|
|
16
|
+
* }, "en");
|
|
17
|
+
*
|
|
18
|
+
* lc.translate("greeting", "World"); // "Hello, World!"
|
|
19
|
+
* lc.currentLanguage = "es";
|
|
20
|
+
* lc.translate("greeting", "Mundo"); // "¡Hola, Mundo!"
|
|
21
|
+
*/
|
|
1
22
|
export declare class LanguageCore<T extends Record<string, Record<string, string>>, LangKey extends keyof T = keyof T> {
|
|
23
|
+
/**
|
|
24
|
+
* The immutable translations dataset provided at construction time.
|
|
25
|
+
* Contains all language entries keyed by their language identifier.
|
|
26
|
+
*/
|
|
2
27
|
private readonly _languages_data;
|
|
28
|
+
/**
|
|
29
|
+
* The currently active language key.
|
|
30
|
+
* Defaults to the first key of the provided data object unless overridden
|
|
31
|
+
* via the `defaultLanguage` constructor parameter.
|
|
32
|
+
* `null` only while the class has not yet been fully initialised
|
|
33
|
+
* (should not occur under normal usage).
|
|
34
|
+
*/
|
|
3
35
|
private _currentLanguage;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a new `LanguageCore` instance.
|
|
38
|
+
*
|
|
39
|
+
* Validates the supplied translations data, ensures all language entries
|
|
40
|
+
* share the same set of translation keys, and sets the initial active language.
|
|
41
|
+
*
|
|
42
|
+
* @param data - The full translations dataset. Must be a non-empty object
|
|
43
|
+
* where every language entry is a flat `Record<string, string>` and all
|
|
44
|
+
* entries contain exactly the same keys.
|
|
45
|
+
* @param defaultLanguage - Optional. The language key to activate initially.
|
|
46
|
+
* If omitted, the first key of `data` is used.
|
|
47
|
+
*
|
|
48
|
+
* @throws {Error} If `data` is empty.
|
|
49
|
+
* @throws {Error} If any language entry is not a plain object.
|
|
50
|
+
* @throws {Error} If language entries have mismatched translation keys.
|
|
51
|
+
* @throws {Error} If the supplied `defaultLanguage` is not a key of `data`.
|
|
52
|
+
*/
|
|
4
53
|
constructor(data: T, defaultLanguage?: keyof T);
|
|
54
|
+
/**
|
|
55
|
+
* Validates the structure of the translations dataset.
|
|
56
|
+
*
|
|
57
|
+
* Ensures the dataset is non-empty, that every language entry is a plain
|
|
58
|
+
* (non-array) object, and that all language entries expose exactly the same
|
|
59
|
+
* set of translation keys as the first entry.
|
|
60
|
+
*
|
|
61
|
+
* @param data - The translations dataset to validate.
|
|
62
|
+
*
|
|
63
|
+
* @throws {Error} If `data` has no keys.
|
|
64
|
+
* @throws {Error} If any language entry is not a plain object or is an array.
|
|
65
|
+
* @throws {Error} If any language entry has a different set of keys than the
|
|
66
|
+
* first language entry.
|
|
67
|
+
*/
|
|
5
68
|
private validateLanguageData;
|
|
69
|
+
/**
|
|
70
|
+
* Returns the full, immutable translations dataset passed to the constructor.
|
|
71
|
+
*
|
|
72
|
+
* @returns The complete `T` object containing all language translation maps.
|
|
73
|
+
*/
|
|
6
74
|
get languagesData(): T;
|
|
75
|
+
/**
|
|
76
|
+
* Returns the key of the currently active language.
|
|
77
|
+
*
|
|
78
|
+
* @returns The active language key, or `null` if no language has been set.
|
|
79
|
+
*/
|
|
7
80
|
get currentLanguage(): LangKey | null;
|
|
81
|
+
/**
|
|
82
|
+
* Sets the active language used for subsequent `translate()` calls.
|
|
83
|
+
*
|
|
84
|
+
* @param lang - The language key to activate. Must be one of the keys
|
|
85
|
+
* present in the translations dataset.
|
|
86
|
+
*
|
|
87
|
+
* @throws {Error} If `lang` is not a recognised language key.
|
|
88
|
+
*/
|
|
8
89
|
set currentLanguage(lang: LangKey);
|
|
90
|
+
/**
|
|
91
|
+
* Returns all language keys available in the translations dataset.
|
|
92
|
+
*
|
|
93
|
+
* @returns An array of every top-level key of `T`, typed as `LangKey[]`.
|
|
94
|
+
*/
|
|
9
95
|
get langKeys(): LangKey[];
|
|
96
|
+
/**
|
|
97
|
+
* Returns the translation map for the currently active language.
|
|
98
|
+
*
|
|
99
|
+
* @returns The `Record<string, string>` of the active language,
|
|
100
|
+
* or `null` if no language is currently set.
|
|
101
|
+
*/
|
|
10
102
|
get currentLanguageData(): T[LangKey] | null;
|
|
103
|
+
/**
|
|
104
|
+
* Looks up a translation key in the active language and optionally
|
|
105
|
+
* interpolates positional placeholders (`{0}`, `{1}`, …) with the
|
|
106
|
+
* supplied arguments.
|
|
107
|
+
*
|
|
108
|
+
* Placeholders follow the pattern `{n}` where `n` is the zero-based index
|
|
109
|
+
* of the corresponding argument. Each placeholder is replaced globally, so
|
|
110
|
+
* `{0}` will be substituted everywhere it appears in the template string.
|
|
111
|
+
*
|
|
112
|
+
* @template K - The specific key within the active language's translation map.
|
|
113
|
+
*
|
|
114
|
+
* @param key - The translation key to look up.
|
|
115
|
+
* @param args - Zero or more values to interpolate into the translated string.
|
|
116
|
+
* Each value is coerced to a string and replaces the matching `{n}` token.
|
|
117
|
+
*
|
|
118
|
+
* @returns The translated (and interpolated) string, or `null` if no language
|
|
119
|
+
* is active or if the resolved translation is an empty string.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* lc.translate("greeting", "Alice"); // "Hello, Alice!"
|
|
123
|
+
* lc.translate("score", "Alice", 42); // "Alice scored 42 points."
|
|
124
|
+
*/
|
|
11
125
|
translate<K extends keyof T[LangKey]>(key: K, ...args: (string | number)[]): string | null;
|
|
12
126
|
}
|
|
127
|
+
export default LanguageCore;
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,53 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LanguageCore = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* A generic, type-safe internationalization (i18n) core class.
|
|
6
|
+
*
|
|
7
|
+
* Manages a collection of language translation maps and provides utilities
|
|
8
|
+
* for switching the active language and resolving translated strings with
|
|
9
|
+
* optional interpolation placeholders.
|
|
10
|
+
*
|
|
11
|
+
* @template T - A record whose keys are language identifiers and whose values
|
|
12
|
+
* are flat string-to-string translation maps (e.g. `{ en: { hello: "Hello" }, fr: { hello: "Bonjour" } }`).
|
|
13
|
+
* @template LangKey - Union of the top-level keys of `T`, inferred automatically.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const lc = new LanguageCore({
|
|
17
|
+
* en: { greeting: "Hello, {0}!" },
|
|
18
|
+
* es: { greeting: "¡Hola, {0}!" },
|
|
19
|
+
* }, "en");
|
|
20
|
+
*
|
|
21
|
+
* lc.translate("greeting", "World"); // "Hello, World!"
|
|
22
|
+
* lc.currentLanguage = "es";
|
|
23
|
+
* lc.translate("greeting", "Mundo"); // "¡Hola, Mundo!"
|
|
24
|
+
*/
|
|
4
25
|
class LanguageCore {
|
|
26
|
+
/**
|
|
27
|
+
* Creates a new `LanguageCore` instance.
|
|
28
|
+
*
|
|
29
|
+
* Validates the supplied translations data, ensures all language entries
|
|
30
|
+
* share the same set of translation keys, and sets the initial active language.
|
|
31
|
+
*
|
|
32
|
+
* @param data - The full translations dataset. Must be a non-empty object
|
|
33
|
+
* where every language entry is a flat `Record<string, string>` and all
|
|
34
|
+
* entries contain exactly the same keys.
|
|
35
|
+
* @param defaultLanguage - Optional. The language key to activate initially.
|
|
36
|
+
* If omitted, the first key of `data` is used.
|
|
37
|
+
*
|
|
38
|
+
* @throws {Error} If `data` is empty.
|
|
39
|
+
* @throws {Error} If any language entry is not a plain object.
|
|
40
|
+
* @throws {Error} If language entries have mismatched translation keys.
|
|
41
|
+
* @throws {Error} If the supplied `defaultLanguage` is not a key of `data`.
|
|
42
|
+
*/
|
|
5
43
|
constructor(data, defaultLanguage) {
|
|
44
|
+
/**
|
|
45
|
+
* The currently active language key.
|
|
46
|
+
* Defaults to the first key of the provided data object unless overridden
|
|
47
|
+
* via the `defaultLanguage` constructor parameter.
|
|
48
|
+
* `null` only while the class has not yet been fully initialised
|
|
49
|
+
* (should not occur under normal usage).
|
|
50
|
+
*/
|
|
6
51
|
this._currentLanguage = null;
|
|
7
52
|
this.validateLanguageData(data);
|
|
8
53
|
this._languages_data = data;
|
|
@@ -16,6 +61,20 @@ class LanguageCore {
|
|
|
16
61
|
this._currentLanguage = Object.keys(data)[0];
|
|
17
62
|
}
|
|
18
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Validates the structure of the translations dataset.
|
|
66
|
+
*
|
|
67
|
+
* Ensures the dataset is non-empty, that every language entry is a plain
|
|
68
|
+
* (non-array) object, and that all language entries expose exactly the same
|
|
69
|
+
* set of translation keys as the first entry.
|
|
70
|
+
*
|
|
71
|
+
* @param data - The translations dataset to validate.
|
|
72
|
+
*
|
|
73
|
+
* @throws {Error} If `data` has no keys.
|
|
74
|
+
* @throws {Error} If any language entry is not a plain object or is an array.
|
|
75
|
+
* @throws {Error} If any language entry has a different set of keys than the
|
|
76
|
+
* first language entry.
|
|
77
|
+
*/
|
|
19
78
|
validateLanguageData(data) {
|
|
20
79
|
if (Object.keys(data).length === 0) {
|
|
21
80
|
throw new Error("Languages data cannot be empty.");
|
|
@@ -37,26 +96,77 @@ class LanguageCore {
|
|
|
37
96
|
}
|
|
38
97
|
}
|
|
39
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* Returns the full, immutable translations dataset passed to the constructor.
|
|
101
|
+
*
|
|
102
|
+
* @returns The complete `T` object containing all language translation maps.
|
|
103
|
+
*/
|
|
40
104
|
get languagesData() {
|
|
41
105
|
return this._languages_data;
|
|
42
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* Returns the key of the currently active language.
|
|
109
|
+
*
|
|
110
|
+
* @returns The active language key, or `null` if no language has been set.
|
|
111
|
+
*/
|
|
43
112
|
get currentLanguage() {
|
|
44
113
|
return this._currentLanguage;
|
|
45
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Sets the active language used for subsequent `translate()` calls.
|
|
117
|
+
*
|
|
118
|
+
* @param lang - The language key to activate. Must be one of the keys
|
|
119
|
+
* present in the translations dataset.
|
|
120
|
+
*
|
|
121
|
+
* @throws {Error} If `lang` is not a recognised language key.
|
|
122
|
+
*/
|
|
46
123
|
set currentLanguage(lang) {
|
|
47
124
|
if (!this.langKeys.includes(lang)) {
|
|
48
125
|
throw new Error(`Language ${String(lang)} is not supported.`);
|
|
49
126
|
}
|
|
50
127
|
this._currentLanguage = lang;
|
|
51
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Returns all language keys available in the translations dataset.
|
|
131
|
+
*
|
|
132
|
+
* @returns An array of every top-level key of `T`, typed as `LangKey[]`.
|
|
133
|
+
*/
|
|
52
134
|
get langKeys() {
|
|
53
135
|
return Object.keys(this._languages_data);
|
|
54
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* Returns the translation map for the currently active language.
|
|
139
|
+
*
|
|
140
|
+
* @returns The `Record<string, string>` of the active language,
|
|
141
|
+
* or `null` if no language is currently set.
|
|
142
|
+
*/
|
|
55
143
|
get currentLanguageData() {
|
|
56
144
|
if (!this._currentLanguage)
|
|
57
145
|
return null;
|
|
58
146
|
return this._languages_data[this._currentLanguage];
|
|
59
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* Looks up a translation key in the active language and optionally
|
|
150
|
+
* interpolates positional placeholders (`{0}`, `{1}`, …) with the
|
|
151
|
+
* supplied arguments.
|
|
152
|
+
*
|
|
153
|
+
* Placeholders follow the pattern `{n}` where `n` is the zero-based index
|
|
154
|
+
* of the corresponding argument. Each placeholder is replaced globally, so
|
|
155
|
+
* `{0}` will be substituted everywhere it appears in the template string.
|
|
156
|
+
*
|
|
157
|
+
* @template K - The specific key within the active language's translation map.
|
|
158
|
+
*
|
|
159
|
+
* @param key - The translation key to look up.
|
|
160
|
+
* @param args - Zero or more values to interpolate into the translated string.
|
|
161
|
+
* Each value is coerced to a string and replaces the matching `{n}` token.
|
|
162
|
+
*
|
|
163
|
+
* @returns The translated (and interpolated) string, or `null` if no language
|
|
164
|
+
* is active or if the resolved translation is an empty string.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* lc.translate("greeting", "Alice"); // "Hello, Alice!"
|
|
168
|
+
* lc.translate("score", "Alice", 42); // "Alice scored 42 points."
|
|
169
|
+
*/
|
|
60
170
|
translate(key, ...args) {
|
|
61
171
|
if (!this._currentLanguage)
|
|
62
172
|
return null;
|
|
@@ -69,3 +179,4 @@ class LanguageCore {
|
|
|
69
179
|
}
|
|
70
180
|
}
|
|
71
181
|
exports.LanguageCore = LanguageCore;
|
|
182
|
+
exports.default = LanguageCore;
|
package/package.json
CHANGED
package/logo.png
DELETED
|
Binary file
|