@dayanulanov/i18n-light 0.1.3 → 0.1.7
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.cjs +1 -46
- package/dist/index.js +1 -44
- package/package.json +6 -27
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,46 +1 @@
|
|
|
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
|
|
1
|
+
'use strict';function b({locale:g,fallbackLocale:c,messages:a}){let r=String(g),l=String(c??g),t={...a};function i(n,s){return t[s]?.[n]}function d(n,s){return s?n.replace(/\{(\w+)\}/g,(o,e)=>s[e]!=null?String(s[e]):`{${e}}`):n}function f(n,s,o){let R=i(n,o??r)??i(n,l)??n;return d(R,s)}function u(n){r=n;}function L(){return r}function M(n,s){t[n]?Object.assign(t[n],s):t[n]={...s};}return {t:f,setLocale:u,getLocale:L,loadLocale:M}}exports.createI18n=b;
|
package/dist/index.js
CHANGED
|
@@ -1,44 +1 @@
|
|
|
1
|
-
|
|
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
|
|
1
|
+
function b({locale:g,fallbackLocale:c,messages:a}){let r=String(g),l=String(c??g),t={...a};function i(n,s){return t[s]?.[n]}function d(n,s){return s?n.replace(/\{(\w+)\}/g,(o,e)=>s[e]!=null?String(s[e]):`{${e}}`):n}function f(n,s,o){let R=i(n,o??r)??i(n,l)??n;return d(R,s)}function u(n){r=n;}function L(){return r}function M(n,s){t[n]?Object.assign(t[n],s):t[n]={...s};}return {t:f,setLocale:u,getLocale:L,loadLocale:M}}export{b as createI18n};
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dayanulanov/i18n-light",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
|
+
"sideEffects": false,
|
|
4
5
|
"description": "Ultra-light i18n core && Type-safe i18n library for JavaScript/TypeScript",
|
|
5
6
|
"files": [
|
|
6
|
-
"dist"
|
|
7
|
+
"dist",
|
|
8
|
+
"README.md",
|
|
9
|
+
"LICENSE"
|
|
7
10
|
],
|
|
8
11
|
"exports": {
|
|
9
12
|
".": {
|
|
@@ -31,29 +34,5 @@
|
|
|
31
34
|
"url": "https://github.com/dayan-ulanov/i18n-light/issues"
|
|
32
35
|
},
|
|
33
36
|
"homepage": "https://github.com/dayan-ulanov/i18n-light#readme",
|
|
34
|
-
"
|
|
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
|
-
}
|
|
37
|
+
"scripts": {}
|
|
59
38
|
}
|
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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"]}
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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,QA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A;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"]}
|