canopy-i18n 0.5.0 → 0.6.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 +10 -5
- package/dist/chainBuilder.d.ts +6 -6
- package/dist/testtest.js +8 -0
- package/package.json +8 -7
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Masaaki Ota
|
|
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
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
A tiny, type-safe i18n library for building localized messages with builder pattern and applying locales across nested data structures.
|
|
4
4
|
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
|
|
5
8
|
## Features
|
|
6
9
|
- **AI-friendly**: Full type safety and single-file colocation give AI assistants complete context for accurate code generation.
|
|
7
10
|
- **Type-safe**: Compile-time safety for locale keys with full TypeScript IntelliSense support.
|
|
@@ -144,11 +147,12 @@ const builder = createI18n(['ja', 'en', 'fr'] as const);
|
|
|
144
147
|
### `ChainBuilder`
|
|
145
148
|
A builder class for creating multiple localized messages with method chaining.
|
|
146
149
|
|
|
147
|
-
#### `.add<ReturnType = string>(entries)`
|
|
150
|
+
#### `.add<ReturnType = string, K extends string = string>(entries)`
|
|
148
151
|
Adds multiple messages at once. By default, returns `string`, but you can specify a custom return type.
|
|
149
152
|
|
|
150
153
|
- **ReturnType**: (optional) Type parameter for the return value (defaults to `string`)
|
|
151
|
-
- **
|
|
154
|
+
- **K**: (optional) Type parameter for the keys of the entries record (defaults to `string`)
|
|
155
|
+
- **entries**: `Record<K, Record<Locale, ReturnType>>`
|
|
152
156
|
- Returns: `ChainBuilder` with added messages
|
|
153
157
|
|
|
154
158
|
```ts
|
|
@@ -184,14 +188,15 @@ const menu = createI18n(['ja', 'en'] as const)
|
|
|
184
188
|
});
|
|
185
189
|
```
|
|
186
190
|
|
|
187
|
-
#### `.addTemplates<Context, ReturnType = string>()(entries)`
|
|
191
|
+
#### `.addTemplates<Context, ReturnType = string, K extends string = string>()(entries)`
|
|
188
192
|
Adds multiple template function messages at once with a unified context type and custom return type.
|
|
189
193
|
|
|
190
|
-
Note: This uses a curried API for better type inference. Call `addTemplates<Context, ReturnType>()` first, then call the returned function with entries.
|
|
194
|
+
Note: This uses a curried API for better type inference. Call `addTemplates<Context, ReturnType, K>()` first, then call the returned function with entries.
|
|
191
195
|
|
|
192
196
|
- **Context**: Type parameter for the template function context
|
|
193
197
|
- **ReturnType**: (optional) Type parameter for the return value (defaults to `string`)
|
|
194
|
-
- **
|
|
198
|
+
- **K**: (optional) Type parameter for the keys of the entries record (defaults to `string`)
|
|
199
|
+
- **entries**: `Record<K, Record<Locale, (ctx: Context) => ReturnType>>`
|
|
195
200
|
- Returns: `ChainBuilder` with added template messages
|
|
196
201
|
|
|
197
202
|
```ts
|
package/dist/chainBuilder.d.ts
CHANGED
|
@@ -8,18 +8,18 @@ export declare class ChainBuilder<const Ls extends readonly string[], Messages e
|
|
|
8
8
|
* 複数のメッセージを一度に追加
|
|
9
9
|
* 型パラメータRでカスタム型も指定可能(デフォルトはstring)
|
|
10
10
|
*/
|
|
11
|
-
add<R = string, Entries extends Record<
|
|
12
|
-
[
|
|
11
|
+
add<R = string, K extends string = string, Entries extends Record<K, Record<Ls[number], R>> = Record<K, Record<Ls[number], R>>>(entries: {
|
|
12
|
+
[Key in keyof Entries]: Key extends keyof Messages ? never : Entries[Key];
|
|
13
13
|
}): ChainBuilder<Ls, Messages & {
|
|
14
|
-
[
|
|
14
|
+
[Key in keyof Entries]: I18nMessage<Ls, void, R>;
|
|
15
15
|
}>;
|
|
16
16
|
/**
|
|
17
17
|
* 関数指定版: 複数のテンプレート関数を一度に追加(型は統一)
|
|
18
18
|
*/
|
|
19
|
-
addTemplates<C, R = string>(): <Entries extends Record<
|
|
20
|
-
[
|
|
19
|
+
addTemplates<C, R = string, K extends string = string>(): <Entries extends Record<K, Record<Ls[number], (ctx: C) => R>>>(entries: {
|
|
20
|
+
[Key in keyof Entries]: Key extends keyof Messages ? never : Entries[Key];
|
|
21
21
|
}) => ChainBuilder<Ls, Messages & {
|
|
22
|
-
[
|
|
22
|
+
[Key in keyof Entries]: I18nMessage<Ls, C, R>;
|
|
23
23
|
}>;
|
|
24
24
|
private deepCloneWithLocale;
|
|
25
25
|
build<M = {
|
package/dist/testtest.js
CHANGED
|
@@ -7,6 +7,14 @@ const b = a
|
|
|
7
7
|
});
|
|
8
8
|
const c = b.build("en");
|
|
9
9
|
const e = { ...c.abc, toString: () => "aaa" };
|
|
10
|
+
const d = b.add({
|
|
11
|
+
aaa: { ja: "aaa", en: "aaa" },
|
|
12
|
+
bbb: { ja: "bbb", en: "bbb" },
|
|
13
|
+
});
|
|
14
|
+
const f = b.addTemplates()({
|
|
15
|
+
aaa: { ja: ({ a }) => `${a}aaa`, en: ({ a }) => `${a}aaa` },
|
|
16
|
+
bbb: { ja: ({ a }) => `${a}aaa`, en: ({ a }) => `${a}aaa` },
|
|
17
|
+
});
|
|
10
18
|
console.log(c.bb({ aa: "name" }));
|
|
11
19
|
console.log(`${c.bb({ aa: "name" })}`);
|
|
12
20
|
console.log("aaa" + c.bb({ aa: "name" }));
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "canopy-i18n",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "The Type-Safe i18n library that your IDE will Love",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -25,14 +25,15 @@
|
|
|
25
25
|
"prepublishOnly": "pnpm run build",
|
|
26
26
|
"type-check": "tsc -p . --noEmit",
|
|
27
27
|
"test": "vitest run",
|
|
28
|
-
"test:watch": "vitest"
|
|
28
|
+
"test:watch": "vitest",
|
|
29
|
+
"release": "release-it"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"@tsconfig/node20": "^20.1.8",
|
|
32
|
-
"@types/node": "^
|
|
33
|
-
"release-it": "^19.2.
|
|
33
|
+
"@types/node": "^25.0.10",
|
|
34
|
+
"release-it": "^19.2.4",
|
|
34
35
|
"typescript": "^5.9.3",
|
|
35
|
-
"vitest": "^4.0.
|
|
36
|
+
"vitest": "^4.0.18"
|
|
36
37
|
},
|
|
37
38
|
"keywords": [
|
|
38
39
|
"i18n",
|
|
@@ -48,6 +49,6 @@
|
|
|
48
49
|
"license": "MIT",
|
|
49
50
|
"repository": {
|
|
50
51
|
"type": "git",
|
|
51
|
-
"url": "https://github.com/
|
|
52
|
+
"url": "https://github.com/mohhh-ok/canopy-i18n"
|
|
52
53
|
}
|
|
53
54
|
}
|