@gruvjs/i18n 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/LICENSE +21 -0
- package/README.md +61 -0
- package/package.json +37 -0
- package/src/core/I18n.js +66 -0
- package/src/helpers/createT.js +16 -0
- package/src/index.js +7 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Saiteja Madha
|
|
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/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<br />
|
|
3
|
+
<p>
|
|
4
|
+
<a href="https://github.com/gruvjs/gruvjs"><img src="https://raw.githubusercontent.com/gruvjs/gruvjs/main/assets/logo.svg" width="546" alt="gruvjs" /></a>
|
|
5
|
+
</p>
|
|
6
|
+
<br />
|
|
7
|
+
<p>
|
|
8
|
+
<a href="https://www.npmjs.com/package/@gruvjs/i18n"><img src="https://img.shields.io/npm/v/@gruvjs/i18n.svg?maxAge=3600" alt="npm version" /></a>
|
|
9
|
+
<a href="https://www.npmjs.com/package/@gruvjs/i18n"><img src="https://img.shields.io/npm/dt/@gruvjs/i18n.svg?maxAge=3600" alt="npm downloads" /></a>
|
|
10
|
+
</p>
|
|
11
|
+
</div>
|
|
12
|
+
|
|
13
|
+
## About
|
|
14
|
+
|
|
15
|
+
`@gruvjs/i18n` is a lightweight i18n engine with dot-notation keys, interpolation, pluralization and fallback locale.
|
|
16
|
+
|
|
17
|
+
Node.js 18.0.0 or newer is required.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
npm install @gruvjs/i18n
|
|
23
|
+
yarn add @gruvjs/i18n
|
|
24
|
+
pnpm add @gruvjs/i18n
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Examples
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
const { I18n, createT } = require("@gruvjs/i18n");
|
|
31
|
+
|
|
32
|
+
const i18n = new I18n({ fallback: "en" });
|
|
33
|
+
|
|
34
|
+
i18n.load("en", {
|
|
35
|
+
greet: "Hello, {{name}}!",
|
|
36
|
+
items: { one: "{{n}} item", other: "{{n}} items" },
|
|
37
|
+
commands: { ping: { reply: "Pong! Latency: {{ms}}ms" } },
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
i18n.load("pt", {
|
|
41
|
+
greet: "Olá, {{name}}!",
|
|
42
|
+
commands: { ping: { reply: "Pong! Latência: {{ms}}ms" } },
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
i18n.t("greet", "pt", { name: "Mundo" }); // → "Olá, Mundo!"
|
|
46
|
+
i18n.t("commands.ping.reply", "pt", { ms: 42 }); // → "Pong! Latência: 42ms"
|
|
47
|
+
i18n.t("items", "en", { n: 5 }); // → "5 items"
|
|
48
|
+
i18n.t("greet", "es", { name: "Mundo" }); // → "Hello, Mundo!" (fallback)
|
|
49
|
+
|
|
50
|
+
// Pre-bound translator
|
|
51
|
+
const t = createT(i18n, "pt");
|
|
52
|
+
t("greet", { name: "Mundo" }); // → "Olá, Mundo!"
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
See the [docs](./docs) folder for detailed usage.
|
|
56
|
+
|
|
57
|
+
## Links
|
|
58
|
+
|
|
59
|
+
- [npm](https://www.npmjs.com/package/@gruvjs/i18n)
|
|
60
|
+
- [GitHub](https://github.com/gruvjs/gruvjs/tree/main/packages/i18n)
|
|
61
|
+
- [gruvjs monorepo](https://github.com/gruvjs/gruvjs)
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gruvjs/i18n",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Lightweight i18n engine with dot-notation keys, interpolation, pluralization and fallback locale.",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"author": "gruvjs",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=18.0.0"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"i18n",
|
|
13
|
+
"internationalization",
|
|
14
|
+
"locale",
|
|
15
|
+
"translation",
|
|
16
|
+
"bot",
|
|
17
|
+
"zero-dependency"
|
|
18
|
+
],
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/gruvjs/gruvjs.git",
|
|
22
|
+
"directory": "packages/i18n"
|
|
23
|
+
},
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/gruvjs/gruvjs/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/gruvjs/gruvjs/tree/main/packages/i18n#readme",
|
|
28
|
+
"files": [
|
|
29
|
+
"src/",
|
|
30
|
+
"README.md",
|
|
31
|
+
"LICENSE"
|
|
32
|
+
],
|
|
33
|
+
"dependencies": {},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"test": "node tests/run.js"
|
|
36
|
+
}
|
|
37
|
+
}
|
package/src/core/I18n.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* I18n — Core translation engine.
|
|
3
|
+
* Handles locale loading, dot-notation key resolution, interpolation and pluralization.
|
|
4
|
+
*/
|
|
5
|
+
class I18n {
|
|
6
|
+
constructor(options = {}) {
|
|
7
|
+
this._fallback = options.fallback ?? "en";
|
|
8
|
+
this._missingTpl = options.missing ?? "__MISSING__{{key}}__";
|
|
9
|
+
this._locales = new Map();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
load(locale, messages) {
|
|
13
|
+
if (!locale || typeof locale !== "string") throw new TypeError("locale must be a non-empty string");
|
|
14
|
+
if (!messages || typeof messages !== "object") throw new TypeError("messages must be a plain object");
|
|
15
|
+
this._locales.set(locale, this._merge(this._locales.get(locale) ?? {}, messages));
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
t(key, locale, vars = {}) {
|
|
20
|
+
const raw = this._resolve(key, locale) ?? this._resolve(key, this._fallback);
|
|
21
|
+
if (raw === undefined) return this._interpolate(this._missingTpl, { key, ...vars });
|
|
22
|
+
return this._interpolate(this._pluralize(raw, vars), vars);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
has(key, locale) {
|
|
26
|
+
return this._resolve(key, locale) !== undefined || this._resolve(key, this._fallback) !== undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
locales() { return [...this._locales.keys()]; }
|
|
30
|
+
|
|
31
|
+
_resolve(key, locale) {
|
|
32
|
+
const messages = this._locales.get(locale);
|
|
33
|
+
if (!messages) return undefined;
|
|
34
|
+
let node = messages;
|
|
35
|
+
for (const part of key.split(".")) {
|
|
36
|
+
if (node === null || typeof node !== "object" || !(part in node)) return undefined;
|
|
37
|
+
node = node[part];
|
|
38
|
+
}
|
|
39
|
+
return node;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
_pluralize(raw, vars) {
|
|
43
|
+
if (typeof raw !== "object" || raw === null) return String(raw);
|
|
44
|
+
const n = Number(vars.n ?? vars.count ?? 1);
|
|
45
|
+
if (n === 0 && "zero" in raw) return raw.zero;
|
|
46
|
+
if (n === 1 && "one" in raw) return raw.one;
|
|
47
|
+
return raw.other ?? raw.one ?? JSON.stringify(raw);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
_interpolate(str, vars) {
|
|
51
|
+
if (!vars || typeof str !== "string") return str;
|
|
52
|
+
return str.replace(/\{\{(\w+)\}\}/g, (_, k) => (k in vars ? String(vars[k]) : `{{${k}}}`));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
_merge(target, source) {
|
|
56
|
+
const out = { ...target };
|
|
57
|
+
for (const [k, v] of Object.entries(source)) {
|
|
58
|
+
out[k] = (v && typeof v === "object" && !Array.isArray(v) && typeof out[k] === "object")
|
|
59
|
+
? this._merge(out[k], v)
|
|
60
|
+
: v;
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
module.exports = { I18n };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a pre-bound translate function for a fixed locale.
|
|
3
|
+
*
|
|
4
|
+
* @param {import("../core/I18n").I18n} i18n
|
|
5
|
+
* @param {string} locale
|
|
6
|
+
* @returns {(key: string, vars?: object) => string}
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const t = createT(i18n, "pt");
|
|
10
|
+
* t("greet", { name: "Mundo" }); // → "Olá, Mundo!"
|
|
11
|
+
*/
|
|
12
|
+
function createT(i18n, locale) {
|
|
13
|
+
return (key, vars) => i18n.t(key, locale, vars);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = { createT };
|