@mikionatsu/t9n 0.1.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 +99 -0
- package/dist/index.cjs +78 -0
- package/dist/index.d.cts +16 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +53 -0
- package/dist/react.cjs +77 -0
- package/dist/react.d.cts +24 -0
- package/dist/react.d.ts +24 -0
- package/dist/react.js +49 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MikioNatsu
|
|
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,99 @@
|
|
|
1
|
+
# @mikionatsu/t9n
|
|
2
|
+
|
|
3
|
+
Tiny translation toolkit for Node/JS + React.
|
|
4
|
+
|
|
5
|
+
✅ Nested keys (`common.hello`)
|
|
6
|
+
✅ Interpolation (`"Salom, {name}!"`)
|
|
7
|
+
✅ Fallback locale
|
|
8
|
+
✅ Plural (`Intl.PluralRules`)
|
|
9
|
+
✅ React Provider + hooks (`@mikionatsu/t9n/react`)
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm i @mikionatsu/t9n
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Core usage (Node/JS)
|
|
20
|
+
|
|
21
|
+
import { createT9n } from "@mikionatsu/t9n";
|
|
22
|
+
|
|
23
|
+
const t9n = createT9n({
|
|
24
|
+
locale: "uz",
|
|
25
|
+
fallbackLocale: "en",
|
|
26
|
+
resources: {
|
|
27
|
+
uz: { common: { hello: "Salom, {name}!" } },
|
|
28
|
+
en: { common: { hello: "Hello, {name}!" } }
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
console.log(t9n.t("common.hello", { name: "Natsu" })); // Salom, Natsu!
|
|
33
|
+
|
|
34
|
+
## Plural
|
|
35
|
+
|
|
36
|
+
const t9n = createT9n({
|
|
37
|
+
locale: "en",
|
|
38
|
+
resources: {
|
|
39
|
+
en: { items: { one: "{count} item", other: "{count} items" } }
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
t9n.t("items", { count: 1 }); // 1 item
|
|
44
|
+
t9n.t("items", { count: 5 }); // 5 items
|
|
45
|
+
|
|
46
|
+
## React usage
|
|
47
|
+
|
|
48
|
+
import { createT9n } from "@mikionatsu/t9n";
|
|
49
|
+
|
|
50
|
+
export const t9n = createT9n({
|
|
51
|
+
locale: "uz",
|
|
52
|
+
fallbackLocale: "en",
|
|
53
|
+
resources: {
|
|
54
|
+
uz: {
|
|
55
|
+
common: { hello: "Salom, {name}!" }
|
|
56
|
+
},
|
|
57
|
+
en: {
|
|
58
|
+
common: { hello: "Hello, {name}!" }
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
## Provider
|
|
64
|
+
|
|
65
|
+
import React from "react";
|
|
66
|
+
import ReactDOM from "react-dom/client";
|
|
67
|
+
import App from "./App";
|
|
68
|
+
import { T9nProvider } from "@mikionatsu/t9n/react";
|
|
69
|
+
import { t9n } from "./i18n";
|
|
70
|
+
|
|
71
|
+
ReactDOM.createRoot(document.getElementById("root")!).render(
|
|
72
|
+
<React.StrictMode>
|
|
73
|
+
<T9nProvider t9n={t9n} initialLocale="uz">
|
|
74
|
+
<App />
|
|
75
|
+
</T9nProvider>
|
|
76
|
+
</React.StrictMode>
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
## Hooks
|
|
81
|
+
|
|
82
|
+
import React from "react";
|
|
83
|
+
import { useT, useLocale } from "@mikionatsu/t9n/react";
|
|
84
|
+
|
|
85
|
+
export function Header() {
|
|
86
|
+
const t = useT();
|
|
87
|
+
const { locale, setLocale } = useLocale();
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<div>
|
|
91
|
+
<h1>{t("common.hello", { name: "Natsu" })}</h1>
|
|
92
|
+
|
|
93
|
+
<select value={locale} onChange={(e) => setLocale(e.target.value)}>
|
|
94
|
+
<option value="uz">uz</option>
|
|
95
|
+
<option value="en">en</option>
|
|
96
|
+
</select>
|
|
97
|
+
</div>
|
|
98
|
+
);
|
|
99
|
+
}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createT9n: () => createT9n
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
function getByPath(obj, path) {
|
|
27
|
+
return path.split(".").reduce((acc, k) => acc == null ? void 0 : acc[k], obj);
|
|
28
|
+
}
|
|
29
|
+
function interpolate(s, vars) {
|
|
30
|
+
return s.replace(/\{(\w+)\}/g, (_, k) => {
|
|
31
|
+
const v = vars[k];
|
|
32
|
+
return v === void 0 || v === null ? "" : String(v);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
function deepMerge(a, b) {
|
|
36
|
+
if (Array.isArray(a) || Array.isArray(b)) return b;
|
|
37
|
+
if (!a || typeof a !== "object") return b;
|
|
38
|
+
if (!b || typeof b !== "object") return b;
|
|
39
|
+
const out = { ...a };
|
|
40
|
+
for (const k of Object.keys(b)) {
|
|
41
|
+
out[k] = k in out ? deepMerge(out[k], b[k]) : b[k];
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
function pickPluralKey(locale, count) {
|
|
46
|
+
return new Intl.PluralRules(locale).select(count);
|
|
47
|
+
}
|
|
48
|
+
function createT9n(opts) {
|
|
49
|
+
let locale = opts.locale;
|
|
50
|
+
const fallbackLocale = opts.fallbackLocale ?? "en";
|
|
51
|
+
const resources = { ...opts.resources ?? {} };
|
|
52
|
+
function setLocale(l) {
|
|
53
|
+
locale = l;
|
|
54
|
+
}
|
|
55
|
+
function getLocale() {
|
|
56
|
+
return locale;
|
|
57
|
+
}
|
|
58
|
+
function addResources(l, dict) {
|
|
59
|
+
resources[l] = resources[l] ? deepMerge(resources[l], dict) : dict;
|
|
60
|
+
}
|
|
61
|
+
function t(key, vars = {}) {
|
|
62
|
+
let val = getByPath(resources[locale], key);
|
|
63
|
+
if (val === void 0) val = getByPath(resources[fallbackLocale], key);
|
|
64
|
+
if (val === void 0) return key;
|
|
65
|
+
if (typeof val === "object" && val && typeof vars.count === "number") {
|
|
66
|
+
const pk = pickPluralKey(locale, vars.count);
|
|
67
|
+
const msg = val[pk] ?? val.other;
|
|
68
|
+
if (typeof msg === "string") return interpolate(msg, vars);
|
|
69
|
+
}
|
|
70
|
+
if (typeof val === "string") return interpolate(val, vars);
|
|
71
|
+
return String(val);
|
|
72
|
+
}
|
|
73
|
+
return { t, setLocale, getLocale, addResources };
|
|
74
|
+
}
|
|
75
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
76
|
+
0 && (module.exports = {
|
|
77
|
+
createT9n
|
|
78
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
type Dict = Record<string, any>;
|
|
2
|
+
type CreateT9nOptions = {
|
|
3
|
+
locale: string;
|
|
4
|
+
fallbackLocale?: string;
|
|
5
|
+
resources?: Record<string, Dict>;
|
|
6
|
+
};
|
|
7
|
+
type T9n = ReturnType<typeof createT9n>;
|
|
8
|
+
type Vars = Record<string, any>;
|
|
9
|
+
declare function createT9n(opts: CreateT9nOptions): {
|
|
10
|
+
t: (key: string, vars?: Vars) => string;
|
|
11
|
+
setLocale: (l: string) => void;
|
|
12
|
+
getLocale: () => string;
|
|
13
|
+
addResources: (l: string, dict: Dict) => void;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export { type CreateT9nOptions, type Dict, type T9n, createT9n };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
type Dict = Record<string, any>;
|
|
2
|
+
type CreateT9nOptions = {
|
|
3
|
+
locale: string;
|
|
4
|
+
fallbackLocale?: string;
|
|
5
|
+
resources?: Record<string, Dict>;
|
|
6
|
+
};
|
|
7
|
+
type T9n = ReturnType<typeof createT9n>;
|
|
8
|
+
type Vars = Record<string, any>;
|
|
9
|
+
declare function createT9n(opts: CreateT9nOptions): {
|
|
10
|
+
t: (key: string, vars?: Vars) => string;
|
|
11
|
+
setLocale: (l: string) => void;
|
|
12
|
+
getLocale: () => string;
|
|
13
|
+
addResources: (l: string, dict: Dict) => void;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export { type CreateT9nOptions, type Dict, type T9n, createT9n };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function getByPath(obj, path) {
|
|
3
|
+
return path.split(".").reduce((acc, k) => acc == null ? void 0 : acc[k], obj);
|
|
4
|
+
}
|
|
5
|
+
function interpolate(s, vars) {
|
|
6
|
+
return s.replace(/\{(\w+)\}/g, (_, k) => {
|
|
7
|
+
const v = vars[k];
|
|
8
|
+
return v === void 0 || v === null ? "" : String(v);
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
function deepMerge(a, b) {
|
|
12
|
+
if (Array.isArray(a) || Array.isArray(b)) return b;
|
|
13
|
+
if (!a || typeof a !== "object") return b;
|
|
14
|
+
if (!b || typeof b !== "object") return b;
|
|
15
|
+
const out = { ...a };
|
|
16
|
+
for (const k of Object.keys(b)) {
|
|
17
|
+
out[k] = k in out ? deepMerge(out[k], b[k]) : b[k];
|
|
18
|
+
}
|
|
19
|
+
return out;
|
|
20
|
+
}
|
|
21
|
+
function pickPluralKey(locale, count) {
|
|
22
|
+
return new Intl.PluralRules(locale).select(count);
|
|
23
|
+
}
|
|
24
|
+
function createT9n(opts) {
|
|
25
|
+
let locale = opts.locale;
|
|
26
|
+
const fallbackLocale = opts.fallbackLocale ?? "en";
|
|
27
|
+
const resources = { ...opts.resources ?? {} };
|
|
28
|
+
function setLocale(l) {
|
|
29
|
+
locale = l;
|
|
30
|
+
}
|
|
31
|
+
function getLocale() {
|
|
32
|
+
return locale;
|
|
33
|
+
}
|
|
34
|
+
function addResources(l, dict) {
|
|
35
|
+
resources[l] = resources[l] ? deepMerge(resources[l], dict) : dict;
|
|
36
|
+
}
|
|
37
|
+
function t(key, vars = {}) {
|
|
38
|
+
let val = getByPath(resources[locale], key);
|
|
39
|
+
if (val === void 0) val = getByPath(resources[fallbackLocale], key);
|
|
40
|
+
if (val === void 0) return key;
|
|
41
|
+
if (typeof val === "object" && val && typeof vars.count === "number") {
|
|
42
|
+
const pk = pickPluralKey(locale, vars.count);
|
|
43
|
+
const msg = val[pk] ?? val.other;
|
|
44
|
+
if (typeof msg === "string") return interpolate(msg, vars);
|
|
45
|
+
}
|
|
46
|
+
if (typeof val === "string") return interpolate(val, vars);
|
|
47
|
+
return String(val);
|
|
48
|
+
}
|
|
49
|
+
return { t, setLocale, getLocale, addResources };
|
|
50
|
+
}
|
|
51
|
+
export {
|
|
52
|
+
createT9n
|
|
53
|
+
};
|
package/dist/react.cjs
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/react.tsx
|
|
21
|
+
var react_exports = {};
|
|
22
|
+
__export(react_exports, {
|
|
23
|
+
T9nProvider: () => T9nProvider,
|
|
24
|
+
useLocale: () => useLocale,
|
|
25
|
+
useT: () => useT,
|
|
26
|
+
useT9n: () => useT9n
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(react_exports);
|
|
29
|
+
var import_react = require("react");
|
|
30
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
31
|
+
var T9nContext = (0, import_react.createContext)(null);
|
|
32
|
+
function T9nProvider(props) {
|
|
33
|
+
const { t9n } = props;
|
|
34
|
+
const [locale, _setLocale] = (0, import_react.useState)(() => props.initialLocale ?? t9n.getLocale());
|
|
35
|
+
const setLocale = (0, import_react.useCallback)(
|
|
36
|
+
(l) => {
|
|
37
|
+
t9n.setLocale(l);
|
|
38
|
+
_setLocale(l);
|
|
39
|
+
},
|
|
40
|
+
[t9n]
|
|
41
|
+
);
|
|
42
|
+
const addResources = (0, import_react.useCallback)(
|
|
43
|
+
(l, dict) => {
|
|
44
|
+
t9n.addResources(l, dict);
|
|
45
|
+
_setLocale((x) => x);
|
|
46
|
+
},
|
|
47
|
+
[t9n]
|
|
48
|
+
);
|
|
49
|
+
const t = (0, import_react.useCallback)((key, vars) => t9n.t(key, vars), [t9n]);
|
|
50
|
+
const value = (0, import_react.useMemo)(
|
|
51
|
+
() => ({ t9n, locale, setLocale, t, addResources }),
|
|
52
|
+
[t9n, locale, setLocale, t, addResources]
|
|
53
|
+
);
|
|
54
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(T9nContext.Provider, { value, children: props.children });
|
|
55
|
+
}
|
|
56
|
+
function useT() {
|
|
57
|
+
const ctx = (0, import_react.useContext)(T9nContext);
|
|
58
|
+
if (!ctx) throw new Error("useT must be used inside <T9nProvider />");
|
|
59
|
+
return ctx.t;
|
|
60
|
+
}
|
|
61
|
+
function useLocale() {
|
|
62
|
+
const ctx = (0, import_react.useContext)(T9nContext);
|
|
63
|
+
if (!ctx) throw new Error("useLocale must be used inside <T9nProvider />");
|
|
64
|
+
return { locale: ctx.locale, setLocale: ctx.setLocale };
|
|
65
|
+
}
|
|
66
|
+
function useT9n() {
|
|
67
|
+
const ctx = (0, import_react.useContext)(T9nContext);
|
|
68
|
+
if (!ctx) throw new Error("useT9n must be used inside <T9nProvider />");
|
|
69
|
+
return ctx;
|
|
70
|
+
}
|
|
71
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
72
|
+
0 && (module.exports = {
|
|
73
|
+
T9nProvider,
|
|
74
|
+
useLocale,
|
|
75
|
+
useT,
|
|
76
|
+
useT9n
|
|
77
|
+
});
|
package/dist/react.d.cts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { T9n, Dict } from './index.cjs';
|
|
4
|
+
|
|
5
|
+
type T9nContextValue = {
|
|
6
|
+
t9n: T9n;
|
|
7
|
+
locale: string;
|
|
8
|
+
setLocale: (locale: string) => void;
|
|
9
|
+
t: (key: string, vars?: Record<string, any>) => string;
|
|
10
|
+
addResources: (locale: string, dict: Dict) => void;
|
|
11
|
+
};
|
|
12
|
+
declare function T9nProvider(props: {
|
|
13
|
+
t9n: T9n;
|
|
14
|
+
initialLocale?: string;
|
|
15
|
+
children: ReactNode;
|
|
16
|
+
}): react_jsx_runtime.JSX.Element;
|
|
17
|
+
declare function useT(): (key: string, vars?: Record<string, any>) => string;
|
|
18
|
+
declare function useLocale(): {
|
|
19
|
+
locale: string;
|
|
20
|
+
setLocale: (locale: string) => void;
|
|
21
|
+
};
|
|
22
|
+
declare function useT9n(): T9nContextValue;
|
|
23
|
+
|
|
24
|
+
export { T9nProvider, useLocale, useT, useT9n };
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { T9n, Dict } from './index.js';
|
|
4
|
+
|
|
5
|
+
type T9nContextValue = {
|
|
6
|
+
t9n: T9n;
|
|
7
|
+
locale: string;
|
|
8
|
+
setLocale: (locale: string) => void;
|
|
9
|
+
t: (key: string, vars?: Record<string, any>) => string;
|
|
10
|
+
addResources: (locale: string, dict: Dict) => void;
|
|
11
|
+
};
|
|
12
|
+
declare function T9nProvider(props: {
|
|
13
|
+
t9n: T9n;
|
|
14
|
+
initialLocale?: string;
|
|
15
|
+
children: ReactNode;
|
|
16
|
+
}): react_jsx_runtime.JSX.Element;
|
|
17
|
+
declare function useT(): (key: string, vars?: Record<string, any>) => string;
|
|
18
|
+
declare function useLocale(): {
|
|
19
|
+
locale: string;
|
|
20
|
+
setLocale: (locale: string) => void;
|
|
21
|
+
};
|
|
22
|
+
declare function useT9n(): T9nContextValue;
|
|
23
|
+
|
|
24
|
+
export { T9nProvider, useLocale, useT, useT9n };
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// src/react.tsx
|
|
2
|
+
import { createContext, useCallback, useContext, useMemo, useState } from "react";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
var T9nContext = createContext(null);
|
|
5
|
+
function T9nProvider(props) {
|
|
6
|
+
const { t9n } = props;
|
|
7
|
+
const [locale, _setLocale] = useState(() => props.initialLocale ?? t9n.getLocale());
|
|
8
|
+
const setLocale = useCallback(
|
|
9
|
+
(l) => {
|
|
10
|
+
t9n.setLocale(l);
|
|
11
|
+
_setLocale(l);
|
|
12
|
+
},
|
|
13
|
+
[t9n]
|
|
14
|
+
);
|
|
15
|
+
const addResources = useCallback(
|
|
16
|
+
(l, dict) => {
|
|
17
|
+
t9n.addResources(l, dict);
|
|
18
|
+
_setLocale((x) => x);
|
|
19
|
+
},
|
|
20
|
+
[t9n]
|
|
21
|
+
);
|
|
22
|
+
const t = useCallback((key, vars) => t9n.t(key, vars), [t9n]);
|
|
23
|
+
const value = useMemo(
|
|
24
|
+
() => ({ t9n, locale, setLocale, t, addResources }),
|
|
25
|
+
[t9n, locale, setLocale, t, addResources]
|
|
26
|
+
);
|
|
27
|
+
return /* @__PURE__ */ jsx(T9nContext.Provider, { value, children: props.children });
|
|
28
|
+
}
|
|
29
|
+
function useT() {
|
|
30
|
+
const ctx = useContext(T9nContext);
|
|
31
|
+
if (!ctx) throw new Error("useT must be used inside <T9nProvider />");
|
|
32
|
+
return ctx.t;
|
|
33
|
+
}
|
|
34
|
+
function useLocale() {
|
|
35
|
+
const ctx = useContext(T9nContext);
|
|
36
|
+
if (!ctx) throw new Error("useLocale must be used inside <T9nProvider />");
|
|
37
|
+
return { locale: ctx.locale, setLocale: ctx.setLocale };
|
|
38
|
+
}
|
|
39
|
+
function useT9n() {
|
|
40
|
+
const ctx = useContext(T9nContext);
|
|
41
|
+
if (!ctx) throw new Error("useT9n must be used inside <T9nProvider />");
|
|
42
|
+
return ctx;
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
T9nProvider,
|
|
46
|
+
useLocale,
|
|
47
|
+
useT,
|
|
48
|
+
useT9n
|
|
49
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mikionatsu/t9n",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Tiny translation toolkit (nested keys, interpolation, plural, React hooks).",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.cjs"
|
|
15
|
+
},
|
|
16
|
+
"./react": {
|
|
17
|
+
"types": "./dist/react.d.ts",
|
|
18
|
+
"import": "./dist/react.js",
|
|
19
|
+
"require": "./dist/react.cjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"README.md",
|
|
25
|
+
"LICENSE"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "vitest run",
|
|
29
|
+
"test:watch": "vitest",
|
|
30
|
+
"build": "tsup src/index.ts src/react.tsx --format esm,cjs --dts --clean",
|
|
31
|
+
"prepublishOnly": "npm run build && vitest run"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"react": ">=17",
|
|
35
|
+
"react-dom": ">=17"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/node": "^24.0.0",
|
|
39
|
+
"@types/react": "^19.0.0",
|
|
40
|
+
"@types/react-dom": "^19.0.0",
|
|
41
|
+
"react": "^19.0.0",
|
|
42
|
+
"react-dom": "^19.0.0",
|
|
43
|
+
"tsup": "^8.0.0",
|
|
44
|
+
"typescript": "^5.0.0",
|
|
45
|
+
"vitest": "^4.0.0"
|
|
46
|
+
},
|
|
47
|
+
"keywords": [
|
|
48
|
+
"t9n",
|
|
49
|
+
"i18n",
|
|
50
|
+
"translation",
|
|
51
|
+
"locale",
|
|
52
|
+
"react"
|
|
53
|
+
],
|
|
54
|
+
"license": "MIT"
|
|
55
|
+
}
|