@xxanderwp/translate-module 1.0.2 → 1.0.4

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 CHANGED
@@ -1,13 +1,125 @@
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;
7
- get currentLanguage(): LangKey | null;
75
+ /**
76
+ * Returns the key of the currently active language.
77
+ *
78
+ * @returns The active language key.
79
+ */
80
+ get currentLanguage(): LangKey;
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[];
10
- get currentLanguageData(): T[LangKey] | null;
96
+ /**
97
+ * Returns the translation map for the currently active language.
98
+ *
99
+ * @returns The `Record<string, string>` of the active language.
100
+ */
101
+ get currentLanguageData(): T[LangKey];
102
+ /**
103
+ * Looks up a translation key in the active language and optionally
104
+ * interpolates positional placeholders (`{0}`, `{1}`, …) with the
105
+ * supplied arguments.
106
+ *
107
+ * Placeholders follow the pattern `{n}` where `n` is the zero-based index
108
+ * of the corresponding argument. Each placeholder is replaced globally, so
109
+ * `{0}` will be substituted everywhere it appears in the template string.
110
+ *
111
+ * @template K - The specific key within the active language's translation map.
112
+ *
113
+ * @param key - The translation key to look up.
114
+ * @param args - Zero or more values to interpolate into the translated string.
115
+ * Each value is coerced to a string and replaces the matching `{n}` token.
116
+ *
117
+ * @returns The translated (and interpolated) string, or `null` if the resolved translation is an empty string.
118
+ *
119
+ * @example
120
+ * lc.translate("greeting", "Alice"); // "Hello, Alice!"
121
+ * lc.translate("score", "Alice", 42); // "Alice scored 42 points."
122
+ */
11
123
  translate<K extends keyof T[LangKey]>(key: K, ...args: (string | number)[]): string | null;
12
124
  }
13
125
  export default LanguageCore;
package/dist/index.js CHANGED
@@ -1,9 +1,46 @@
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) {
6
- this._currentLanguage = null;
7
44
  this.validateLanguageData(data);
8
45
  this._languages_data = data;
9
46
  if (defaultLanguage) {
@@ -16,6 +53,20 @@ class LanguageCore {
16
53
  this._currentLanguage = Object.keys(data)[0];
17
54
  }
18
55
  }
56
+ /**
57
+ * Validates the structure of the translations dataset.
58
+ *
59
+ * Ensures the dataset is non-empty, that every language entry is a plain
60
+ * (non-array) object, and that all language entries expose exactly the same
61
+ * set of translation keys as the first entry.
62
+ *
63
+ * @param data - The translations dataset to validate.
64
+ *
65
+ * @throws {Error} If `data` has no keys.
66
+ * @throws {Error} If any language entry is not a plain object or is an array.
67
+ * @throws {Error} If any language entry has a different set of keys than the
68
+ * first language entry.
69
+ */
19
70
  validateLanguageData(data) {
20
71
  if (Object.keys(data).length === 0) {
21
72
  throw new Error("Languages data cannot be empty.");
@@ -37,29 +88,74 @@ class LanguageCore {
37
88
  }
38
89
  }
39
90
  }
91
+ /**
92
+ * Returns the full, immutable translations dataset passed to the constructor.
93
+ *
94
+ * @returns The complete `T` object containing all language translation maps.
95
+ */
40
96
  get languagesData() {
41
97
  return this._languages_data;
42
98
  }
99
+ /**
100
+ * Returns the key of the currently active language.
101
+ *
102
+ * @returns The active language key.
103
+ */
43
104
  get currentLanguage() {
44
105
  return this._currentLanguage;
45
106
  }
107
+ /**
108
+ * Sets the active language used for subsequent `translate()` calls.
109
+ *
110
+ * @param lang - The language key to activate. Must be one of the keys
111
+ * present in the translations dataset.
112
+ *
113
+ * @throws {Error} If `lang` is not a recognised language key.
114
+ */
46
115
  set currentLanguage(lang) {
47
116
  if (!this.langKeys.includes(lang)) {
48
117
  throw new Error(`Language ${String(lang)} is not supported.`);
49
118
  }
50
119
  this._currentLanguage = lang;
51
120
  }
121
+ /**
122
+ * Returns all language keys available in the translations dataset.
123
+ *
124
+ * @returns An array of every top-level key of `T`, typed as `LangKey[]`.
125
+ */
52
126
  get langKeys() {
53
127
  return Object.keys(this._languages_data);
54
128
  }
129
+ /**
130
+ * Returns the translation map for the currently active language.
131
+ *
132
+ * @returns The `Record<string, string>` of the active language.
133
+ */
55
134
  get currentLanguageData() {
56
- if (!this._currentLanguage)
57
- return null;
58
135
  return this._languages_data[this._currentLanguage];
59
136
  }
137
+ /**
138
+ * Looks up a translation key in the active language and optionally
139
+ * interpolates positional placeholders (`{0}`, `{1}`, …) with the
140
+ * supplied arguments.
141
+ *
142
+ * Placeholders follow the pattern `{n}` where `n` is the zero-based index
143
+ * of the corresponding argument. Each placeholder is replaced globally, so
144
+ * `{0}` will be substituted everywhere it appears in the template string.
145
+ *
146
+ * @template K - The specific key within the active language's translation map.
147
+ *
148
+ * @param key - The translation key to look up.
149
+ * @param args - Zero or more values to interpolate into the translated string.
150
+ * Each value is coerced to a string and replaces the matching `{n}` token.
151
+ *
152
+ * @returns The translated (and interpolated) string, or `null` if the resolved translation is an empty string.
153
+ *
154
+ * @example
155
+ * lc.translate("greeting", "Alice"); // "Hello, Alice!"
156
+ * lc.translate("score", "Alice", 42); // "Alice scored 42 points."
157
+ */
60
158
  translate(key, ...args) {
61
- if (!this._currentLanguage)
62
- return null;
63
159
  const langData = this._languages_data[this._currentLanguage];
64
160
  let res = langData[key];
65
161
  args.forEach((arg, index) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xxanderwp/translate-module",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "directories": {