@dayanulanov/i18n-light 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Dayan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+
3
+ // src/index.ts
4
+ function createI18n({ locale, fallbackLocale, messages }) {
5
+ let _currentLocale = String(locale);
6
+ const _fallbackLocale = String(fallbackLocale ?? locale);
7
+ const _messages = { ...messages };
8
+ function _resolveMessage(key, locale2) {
9
+ return _messages[locale2]?.[key];
10
+ }
11
+ function _interpolate(message, vars) {
12
+ if (!vars) return message;
13
+ return message.replace(
14
+ /\{(\w+)\}/g,
15
+ (_, k) => vars[k] != null ? String(vars[k]) : `{${k}}`
16
+ );
17
+ }
18
+ function t(key, vars, locale2) {
19
+ const targetLocale = locale2 ?? _currentLocale;
20
+ const message = _resolveMessage(key, targetLocale) ?? _resolveMessage(key, _fallbackLocale) ?? key;
21
+ return _interpolate(message, vars);
22
+ }
23
+ function setLocale(locale2) {
24
+ _currentLocale = locale2;
25
+ }
26
+ function getLocale() {
27
+ return _currentLocale;
28
+ }
29
+ function loadLocale(locale2, messages2) {
30
+ if (!_messages[locale2]) {
31
+ _messages[locale2] = { ...messages2 };
32
+ } else {
33
+ Object.assign(_messages[locale2], messages2);
34
+ }
35
+ }
36
+ return {
37
+ t,
38
+ setLocale,
39
+ getLocale,
40
+ loadLocale
41
+ };
42
+ }
43
+
44
+ exports.createI18n = createI18n;
45
+ //# sourceMappingURL=index.cjs.map
46
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":["locale","messages"],"mappings":";;;AAoEA,SAAS,UAAA,CAKP,EAAE,MAAA,EAAQ,cAAA,EAAgB,UAAS,EAAkC;AACrE,EAAA,IAAI,cAAA,GAAiB,OAAO,MAAM,CAAA;AAClC,EAAA,MAAM,eAAA,GAAkB,MAAA,CAAO,cAAA,IAAkB,MAAM,CAAA;AACvD,EAAA,MAAM,SAAA,GAAoD,EAAE,GAAG,QAAA,EAAS;AASxE,EAAA,SAAS,eAAA,CAAgB,KAAaA,OAAAA,EAAoC;AACxE,IAAA,OAAO,SAAA,CAAUA,OAAM,CAAA,GAAI,GAAG,CAAA;AAAA,EAChC;AASA,EAAA,SAAS,YAAA,CACP,SACA,IAAA,EACQ;AACR,IAAA,IAAI,CAAC,MAAM,OAAO,OAAA;AAClB,IAAA,OAAO,OAAA,CAAQ,OAAA;AAAA,MAAQ,YAAA;AAAA,MAAc,CAAC,CAAA,EAAG,CAAA,KACvC,IAAA,CAAK,CAAC,CAAA,IAAK,IAAA,GAAO,MAAA,CAAO,IAAA,CAAK,CAAC,CAAC,CAAA,GAAI,IAAI,CAAC,CAAA,CAAA;AAAA,KAC3C;AAAA,EACF;AAWA,EAAA,SAAS,CAAA,CACP,GAAA,EACA,IAAA,EACAA,OAAAA,EACQ;AACR,IAAA,MAAM,eAAgBA,OAAAA,IAAU,cAAA;AAChC,IAAA,MAAM,OAAA,GACJ,gBAAgB,GAAA,EAAK,YAAY,KACjC,eAAA,CAAgB,GAAA,EAAK,eAAe,CAAA,IACpC,GAAA;AAEF,IAAA,OAAO,YAAA,CAAa,SAAS,IAAI,CAAA;AAAA,EACnC;AAOA,EAAA,SAAS,UAAUA,OAAAA,EAAoC;AACrD,IAAA,cAAA,GAAiBA,OAAAA;AAAA,EACnB;AAOA,EAAA,SAAS,SAAA,GAAY;AACnB,IAAA,OAAO,cAAA;AAAA,EACT;AAQA,EAAA,SAAS,UAAA,CAAWA,SAAgBC,SAAAA,EAAwC;AAC1E,IAAA,IAAI,CAAC,SAAA,CAAUD,OAAM,CAAA,EAAG;AACtB,MAAA,SAAA,CAAUA,OAAM,CAAA,GAAI,EAAE,GAAGC,SAAAA,EAAS;AAAA,IACpC,CAAA,MAAO;AACL,MAAA,MAAA,CAAO,MAAA,CAAO,SAAA,CAAUD,OAAM,CAAA,EAAGC,SAAQ,CAAA;AAAA,IAC3C;AAAA,EACF;AAEA,EAAA,OAAO;AAAA,IACL,CAAA;AAAA,IACA,SAAA;AAAA,IACA,SAAA;AAAA,IACA;AAAA,GACF;AACF","file":"index.cjs","sourcesContent":["/**\n * Options for creating an i18n instance.\n *\n * @template Msgs - The shape of the messages object, mapping locales to key-value pairs.\n */\ninterface I18nOptions<Msgs extends Record<string, Record<string, string>>> {\n /** Current locale to use */\n locale: keyof Msgs | string;\n /** Optional fallback locale if a translation is missing in the current locale */\n fallbackLocale?: keyof Msgs | string;\n /** Messages object containing all translations */\n messages: Msgs;\n}\n\n/**\n * Internationalization (i18n) instance interface.\n *\n * @template Msgs - The shape of the messages object, mapping locales to key-value pairs.\n */\ninterface I18n<Msgs extends Record<string, Record<string, string>>> {\n /**\n * Translate a key into the target locale.\n *\n * @template L - Locale key\n * @param key - Message key to translate\n * @param vars - Optional variables for interpolation\n * @param locale - Optional locale to override the current one\n * @returns Translated string\n * @example\n * const i18n = createI18n({ locale: 'en', messages: { en: { hello: 'Hello' } } });\n * i18n.t('hello'); // \"Hello\"\n */\n t<L extends keyof Msgs>(\n key: keyof Msgs[L] & string,\n vars?: Record<string, string | number | boolean>,\n locale?: L | (string & {}),\n ): string;\n\n /**\n * Set the current locale.\n *\n * @param locale - Locale to set\n */\n setLocale(locale: keyof Msgs | (string & {})): void;\n\n /**\n * Get the current locale.\n *\n * @returns Current locale string\n */\n getLocale(): string;\n\n /**\n * Load or merge messages for a specific locale.\n *\n * @param locale - Locale to load messages for\n * @param messages - Messages to merge into the locale\n */\n loadLocale(locale: string, messages: Record<string, string>): void;\n}\n\n/**\n * Create a new i18n instance.\n *\n * @template Msgs - The shape of the messages object, mapping locales to key-value pairs.\n * @param options - I18n options\n * @returns I18n instance\n */\nfunction createI18n<\n Msgs extends Record<string, Record<string, string>> = Record<\n string,\n Record<string, string>\n >,\n>({ locale, fallbackLocale, messages }: I18nOptions<Msgs>): I18n<Msgs> {\n let _currentLocale = String(locale);\n const _fallbackLocale = String(fallbackLocale ?? locale);\n const _messages: Record<string, Record<string, string>> = { ...messages };\n\n /**\n * Resolve a message by key and locale.\n *\n * @param key - Message key to resolve\n * @param locale - Locale to resolve the message for\n * @returns The message string if found, otherwise undefined\n */\n function _resolveMessage(key: string, locale: string): string | undefined {\n return _messages[locale]?.[key];\n }\n\n /**\n * Interpolate variables into a message string.\n *\n * @param message - Message string containing placeholders like {var}\n * @param vars - Optional object containing variables for interpolation\n * @returns Interpolated message string\n */\n function _interpolate(\n message: string,\n vars?: Record<string, string | number | boolean>,\n ): string {\n if (!vars) return message;\n return message.replace(/\\{(\\w+)\\}/g, (_, k) =>\n vars[k] != null ? String(vars[k]) : `{${k}}`,\n );\n }\n\n /**\n * Translate a key into the target locale with optional interpolation.\n *\n * @template L - Locale key\n * @param key - Message key to translate\n * @param vars - Optional variables for interpolation\n * @param locale - Optional locale to override the current one\n * @returns Translated string\n */\n function t<L extends keyof Msgs>(\n key: keyof Msgs[L] & string,\n vars?: Record<string, string | number | boolean>,\n locale?: L | string,\n ): string {\n const targetLocale = (locale ?? _currentLocale) as string;\n const message =\n _resolveMessage(key, targetLocale) ??\n _resolveMessage(key, _fallbackLocale) ??\n key;\n\n return _interpolate(message, vars);\n }\n\n /**\n * Set the current locale.\n *\n * @param locale - Locale to set\n */\n function setLocale(locale: keyof Msgs | (string & {})) {\n _currentLocale = locale as string;\n }\n\n /**\n * Get the current locale.\n *\n * @returns Current locale string\n */\n function getLocale() {\n return _currentLocale;\n }\n\n /**\n * Load or merge messages for a specific locale.\n *\n * @param locale - Locale to load messages for\n * @param messages - Messages to merge into the locale\n */\n function loadLocale(locale: string, messages: Record<string, string>): void {\n if (!_messages[locale]) {\n _messages[locale] = { ...messages };\n } else {\n Object.assign(_messages[locale], messages);\n }\n }\n\n return {\n t,\n setLocale,\n getLocale,\n loadLocale,\n };\n}\n\nexport { createI18n };\nexport type { I18nOptions, I18n };\n"]}
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Options for creating an i18n instance.
3
+ *
4
+ * @template Msgs - The shape of the messages object, mapping locales to key-value pairs.
5
+ */
6
+ interface I18nOptions<Msgs extends Record<string, Record<string, string>>> {
7
+ /** Current locale to use */
8
+ locale: keyof Msgs | string;
9
+ /** Optional fallback locale if a translation is missing in the current locale */
10
+ fallbackLocale?: keyof Msgs | string;
11
+ /** Messages object containing all translations */
12
+ messages: Msgs;
13
+ }
14
+ /**
15
+ * Internationalization (i18n) instance interface.
16
+ *
17
+ * @template Msgs - The shape of the messages object, mapping locales to key-value pairs.
18
+ */
19
+ interface I18n<Msgs extends Record<string, Record<string, string>>> {
20
+ /**
21
+ * Translate a key into the target locale.
22
+ *
23
+ * @template L - Locale key
24
+ * @param key - Message key to translate
25
+ * @param vars - Optional variables for interpolation
26
+ * @param locale - Optional locale to override the current one
27
+ * @returns Translated string
28
+ * @example
29
+ * const i18n = createI18n({ locale: 'en', messages: { en: { hello: 'Hello' } } });
30
+ * i18n.t('hello'); // "Hello"
31
+ */
32
+ t<L extends keyof Msgs>(key: keyof Msgs[L] & string, vars?: Record<string, string | number | boolean>, locale?: L | (string & {})): string;
33
+ /**
34
+ * Set the current locale.
35
+ *
36
+ * @param locale - Locale to set
37
+ */
38
+ setLocale(locale: keyof Msgs | (string & {})): void;
39
+ /**
40
+ * Get the current locale.
41
+ *
42
+ * @returns Current locale string
43
+ */
44
+ getLocale(): string;
45
+ /**
46
+ * Load or merge messages for a specific locale.
47
+ *
48
+ * @param locale - Locale to load messages for
49
+ * @param messages - Messages to merge into the locale
50
+ */
51
+ loadLocale(locale: string, messages: Record<string, string>): void;
52
+ }
53
+ /**
54
+ * Create a new i18n instance.
55
+ *
56
+ * @template Msgs - The shape of the messages object, mapping locales to key-value pairs.
57
+ * @param options - I18n options
58
+ * @returns I18n instance
59
+ */
60
+ declare function createI18n<Msgs extends Record<string, Record<string, string>> = Record<string, Record<string, string>>>({ locale, fallbackLocale, messages }: I18nOptions<Msgs>): I18n<Msgs>;
61
+
62
+ export { type I18n, type I18nOptions, createI18n };
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Options for creating an i18n instance.
3
+ *
4
+ * @template Msgs - The shape of the messages object, mapping locales to key-value pairs.
5
+ */
6
+ interface I18nOptions<Msgs extends Record<string, Record<string, string>>> {
7
+ /** Current locale to use */
8
+ locale: keyof Msgs | string;
9
+ /** Optional fallback locale if a translation is missing in the current locale */
10
+ fallbackLocale?: keyof Msgs | string;
11
+ /** Messages object containing all translations */
12
+ messages: Msgs;
13
+ }
14
+ /**
15
+ * Internationalization (i18n) instance interface.
16
+ *
17
+ * @template Msgs - The shape of the messages object, mapping locales to key-value pairs.
18
+ */
19
+ interface I18n<Msgs extends Record<string, Record<string, string>>> {
20
+ /**
21
+ * Translate a key into the target locale.
22
+ *
23
+ * @template L - Locale key
24
+ * @param key - Message key to translate
25
+ * @param vars - Optional variables for interpolation
26
+ * @param locale - Optional locale to override the current one
27
+ * @returns Translated string
28
+ * @example
29
+ * const i18n = createI18n({ locale: 'en', messages: { en: { hello: 'Hello' } } });
30
+ * i18n.t('hello'); // "Hello"
31
+ */
32
+ t<L extends keyof Msgs>(key: keyof Msgs[L] & string, vars?: Record<string, string | number | boolean>, locale?: L | (string & {})): string;
33
+ /**
34
+ * Set the current locale.
35
+ *
36
+ * @param locale - Locale to set
37
+ */
38
+ setLocale(locale: keyof Msgs | (string & {})): void;
39
+ /**
40
+ * Get the current locale.
41
+ *
42
+ * @returns Current locale string
43
+ */
44
+ getLocale(): string;
45
+ /**
46
+ * Load or merge messages for a specific locale.
47
+ *
48
+ * @param locale - Locale to load messages for
49
+ * @param messages - Messages to merge into the locale
50
+ */
51
+ loadLocale(locale: string, messages: Record<string, string>): void;
52
+ }
53
+ /**
54
+ * Create a new i18n instance.
55
+ *
56
+ * @template Msgs - The shape of the messages object, mapping locales to key-value pairs.
57
+ * @param options - I18n options
58
+ * @returns I18n instance
59
+ */
60
+ declare function createI18n<Msgs extends Record<string, Record<string, string>> = Record<string, Record<string, string>>>({ locale, fallbackLocale, messages }: I18nOptions<Msgs>): I18n<Msgs>;
61
+
62
+ export { type I18n, type I18nOptions, createI18n };
package/dist/index.js ADDED
@@ -0,0 +1,44 @@
1
+ // src/index.ts
2
+ function createI18n({ locale, fallbackLocale, messages }) {
3
+ let _currentLocale = String(locale);
4
+ const _fallbackLocale = String(fallbackLocale ?? locale);
5
+ const _messages = { ...messages };
6
+ function _resolveMessage(key, locale2) {
7
+ return _messages[locale2]?.[key];
8
+ }
9
+ function _interpolate(message, vars) {
10
+ if (!vars) return message;
11
+ return message.replace(
12
+ /\{(\w+)\}/g,
13
+ (_, k) => vars[k] != null ? String(vars[k]) : `{${k}}`
14
+ );
15
+ }
16
+ function t(key, vars, locale2) {
17
+ const targetLocale = locale2 ?? _currentLocale;
18
+ const message = _resolveMessage(key, targetLocale) ?? _resolveMessage(key, _fallbackLocale) ?? key;
19
+ return _interpolate(message, vars);
20
+ }
21
+ function setLocale(locale2) {
22
+ _currentLocale = locale2;
23
+ }
24
+ function getLocale() {
25
+ return _currentLocale;
26
+ }
27
+ function loadLocale(locale2, messages2) {
28
+ if (!_messages[locale2]) {
29
+ _messages[locale2] = { ...messages2 };
30
+ } else {
31
+ Object.assign(_messages[locale2], messages2);
32
+ }
33
+ }
34
+ return {
35
+ t,
36
+ setLocale,
37
+ getLocale,
38
+ loadLocale
39
+ };
40
+ }
41
+
42
+ export { createI18n };
43
+ //# sourceMappingURL=index.js.map
44
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":["locale","messages"],"mappings":";AAoEA,SAAS,UAAA,CAKP,EAAE,MAAA,EAAQ,cAAA,EAAgB,UAAS,EAAkC;AACrE,EAAA,IAAI,cAAA,GAAiB,OAAO,MAAM,CAAA;AAClC,EAAA,MAAM,eAAA,GAAkB,MAAA,CAAO,cAAA,IAAkB,MAAM,CAAA;AACvD,EAAA,MAAM,SAAA,GAAoD,EAAE,GAAG,QAAA,EAAS;AASxE,EAAA,SAAS,eAAA,CAAgB,KAAaA,OAAAA,EAAoC;AACxE,IAAA,OAAO,SAAA,CAAUA,OAAM,CAAA,GAAI,GAAG,CAAA;AAAA,EAChC;AASA,EAAA,SAAS,YAAA,CACP,SACA,IAAA,EACQ;AACR,IAAA,IAAI,CAAC,MAAM,OAAO,OAAA;AAClB,IAAA,OAAO,OAAA,CAAQ,OAAA;AAAA,MAAQ,YAAA;AAAA,MAAc,CAAC,CAAA,EAAG,CAAA,KACvC,IAAA,CAAK,CAAC,CAAA,IAAK,IAAA,GAAO,MAAA,CAAO,IAAA,CAAK,CAAC,CAAC,CAAA,GAAI,IAAI,CAAC,CAAA,CAAA;AAAA,KAC3C;AAAA,EACF;AAWA,EAAA,SAAS,CAAA,CACP,GAAA,EACA,IAAA,EACAA,OAAAA,EACQ;AACR,IAAA,MAAM,eAAgBA,OAAAA,IAAU,cAAA;AAChC,IAAA,MAAM,OAAA,GACJ,gBAAgB,GAAA,EAAK,YAAY,KACjC,eAAA,CAAgB,GAAA,EAAK,eAAe,CAAA,IACpC,GAAA;AAEF,IAAA,OAAO,YAAA,CAAa,SAAS,IAAI,CAAA;AAAA,EACnC;AAOA,EAAA,SAAS,UAAUA,OAAAA,EAAoC;AACrD,IAAA,cAAA,GAAiBA,OAAAA;AAAA,EACnB;AAOA,EAAA,SAAS,SAAA,GAAY;AACnB,IAAA,OAAO,cAAA;AAAA,EACT;AAQA,EAAA,SAAS,UAAA,CAAWA,SAAgBC,SAAAA,EAAwC;AAC1E,IAAA,IAAI,CAAC,SAAA,CAAUD,OAAM,CAAA,EAAG;AACtB,MAAA,SAAA,CAAUA,OAAM,CAAA,GAAI,EAAE,GAAGC,SAAAA,EAAS;AAAA,IACpC,CAAA,MAAO;AACL,MAAA,MAAA,CAAO,MAAA,CAAO,SAAA,CAAUD,OAAM,CAAA,EAAGC,SAAQ,CAAA;AAAA,IAC3C;AAAA,EACF;AAEA,EAAA,OAAO;AAAA,IACL,CAAA;AAAA,IACA,SAAA;AAAA,IACA,SAAA;AAAA,IACA;AAAA,GACF;AACF","file":"index.js","sourcesContent":["/**\n * Options for creating an i18n instance.\n *\n * @template Msgs - The shape of the messages object, mapping locales to key-value pairs.\n */\ninterface I18nOptions<Msgs extends Record<string, Record<string, string>>> {\n /** Current locale to use */\n locale: keyof Msgs | string;\n /** Optional fallback locale if a translation is missing in the current locale */\n fallbackLocale?: keyof Msgs | string;\n /** Messages object containing all translations */\n messages: Msgs;\n}\n\n/**\n * Internationalization (i18n) instance interface.\n *\n * @template Msgs - The shape of the messages object, mapping locales to key-value pairs.\n */\ninterface I18n<Msgs extends Record<string, Record<string, string>>> {\n /**\n * Translate a key into the target locale.\n *\n * @template L - Locale key\n * @param key - Message key to translate\n * @param vars - Optional variables for interpolation\n * @param locale - Optional locale to override the current one\n * @returns Translated string\n * @example\n * const i18n = createI18n({ locale: 'en', messages: { en: { hello: 'Hello' } } });\n * i18n.t('hello'); // \"Hello\"\n */\n t<L extends keyof Msgs>(\n key: keyof Msgs[L] & string,\n vars?: Record<string, string | number | boolean>,\n locale?: L | (string & {}),\n ): string;\n\n /**\n * Set the current locale.\n *\n * @param locale - Locale to set\n */\n setLocale(locale: keyof Msgs | (string & {})): void;\n\n /**\n * Get the current locale.\n *\n * @returns Current locale string\n */\n getLocale(): string;\n\n /**\n * Load or merge messages for a specific locale.\n *\n * @param locale - Locale to load messages for\n * @param messages - Messages to merge into the locale\n */\n loadLocale(locale: string, messages: Record<string, string>): void;\n}\n\n/**\n * Create a new i18n instance.\n *\n * @template Msgs - The shape of the messages object, mapping locales to key-value pairs.\n * @param options - I18n options\n * @returns I18n instance\n */\nfunction createI18n<\n Msgs extends Record<string, Record<string, string>> = Record<\n string,\n Record<string, string>\n >,\n>({ locale, fallbackLocale, messages }: I18nOptions<Msgs>): I18n<Msgs> {\n let _currentLocale = String(locale);\n const _fallbackLocale = String(fallbackLocale ?? locale);\n const _messages: Record<string, Record<string, string>> = { ...messages };\n\n /**\n * Resolve a message by key and locale.\n *\n * @param key - Message key to resolve\n * @param locale - Locale to resolve the message for\n * @returns The message string if found, otherwise undefined\n */\n function _resolveMessage(key: string, locale: string): string | undefined {\n return _messages[locale]?.[key];\n }\n\n /**\n * Interpolate variables into a message string.\n *\n * @param message - Message string containing placeholders like {var}\n * @param vars - Optional object containing variables for interpolation\n * @returns Interpolated message string\n */\n function _interpolate(\n message: string,\n vars?: Record<string, string | number | boolean>,\n ): string {\n if (!vars) return message;\n return message.replace(/\\{(\\w+)\\}/g, (_, k) =>\n vars[k] != null ? String(vars[k]) : `{${k}}`,\n );\n }\n\n /**\n * Translate a key into the target locale with optional interpolation.\n *\n * @template L - Locale key\n * @param key - Message key to translate\n * @param vars - Optional variables for interpolation\n * @param locale - Optional locale to override the current one\n * @returns Translated string\n */\n function t<L extends keyof Msgs>(\n key: keyof Msgs[L] & string,\n vars?: Record<string, string | number | boolean>,\n locale?: L | string,\n ): string {\n const targetLocale = (locale ?? _currentLocale) as string;\n const message =\n _resolveMessage(key, targetLocale) ??\n _resolveMessage(key, _fallbackLocale) ??\n key;\n\n return _interpolate(message, vars);\n }\n\n /**\n * Set the current locale.\n *\n * @param locale - Locale to set\n */\n function setLocale(locale: keyof Msgs | (string & {})) {\n _currentLocale = locale as string;\n }\n\n /**\n * Get the current locale.\n *\n * @returns Current locale string\n */\n function getLocale() {\n return _currentLocale;\n }\n\n /**\n * Load or merge messages for a specific locale.\n *\n * @param locale - Locale to load messages for\n * @param messages - Messages to merge into the locale\n */\n function loadLocale(locale: string, messages: Record<string, string>): void {\n if (!_messages[locale]) {\n _messages[locale] = { ...messages };\n } else {\n Object.assign(_messages[locale], messages);\n }\n }\n\n return {\n t,\n setLocale,\n getLocale,\n loadLocale,\n };\n}\n\nexport { createI18n };\nexport type { I18nOptions, I18n };\n"]}
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@dayanulanov/i18n-light",
3
+ "version": "0.1.3",
4
+ "description": "Ultra-light i18n core && Type-safe i18n library for JavaScript/TypeScript",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "require": "./dist/index.cjs"
12
+ }
13
+ },
14
+ "main": "./dist/index.cjs",
15
+ "module": "./dist/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/dayan-ulanov/i18n-light.git"
20
+ },
21
+ "keywords": [
22
+ "i18n",
23
+ "typescript",
24
+ "translations",
25
+ "library"
26
+ ],
27
+ "author": "",
28
+ "license": "ISC",
29
+ "type": "module",
30
+ "bugs": {
31
+ "url": "https://github.com/dayan-ulanov/i18n-light/issues"
32
+ },
33
+ "homepage": "https://github.com/dayan-ulanov/i18n-light#readme",
34
+ "lint-staged": {
35
+ "*.{js,ts,json}": [
36
+ "prettier --write",
37
+ "eslint --fix"
38
+ ]
39
+ },
40
+ "devDependencies": {
41
+ "@eslint/js": "^9.39.2",
42
+ "@eslint/json": "^0.14.0",
43
+ "eslint": "^9.39.2",
44
+ "globals": "^17.0.0",
45
+ "husky": "^9.1.7",
46
+ "is-ci": "^4.1.0",
47
+ "jiti": "^2.6.1",
48
+ "lint-staged": "^16.2.7",
49
+ "prettier": "3.7.4",
50
+ "tsup": "^8.5.1",
51
+ "typescript": "^5.9.3",
52
+ "typescript-eslint": "^8.51.0"
53
+ },
54
+ "scripts": {
55
+ "build": "tsup",
56
+ "format": "prettier --write .",
57
+ "lint": "eslint ."
58
+ }
59
+ }