canopy-i18n 0.5.1 → 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/README.md +7 -5
- package/dist/chainBuilder.d.ts +6 -6
- package/dist/testtest.js +8 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -147,11 +147,12 @@ const builder = createI18n(['ja', 'en', 'fr'] as const);
|
|
|
147
147
|
### `ChainBuilder`
|
|
148
148
|
A builder class for creating multiple localized messages with method chaining.
|
|
149
149
|
|
|
150
|
-
#### `.add<ReturnType = string>(entries)`
|
|
150
|
+
#### `.add<ReturnType = string, K extends string = string>(entries)`
|
|
151
151
|
Adds multiple messages at once. By default, returns `string`, but you can specify a custom return type.
|
|
152
152
|
|
|
153
153
|
- **ReturnType**: (optional) Type parameter for the return value (defaults to `string`)
|
|
154
|
-
- **
|
|
154
|
+
- **K**: (optional) Type parameter for the keys of the entries record (defaults to `string`)
|
|
155
|
+
- **entries**: `Record<K, Record<Locale, ReturnType>>`
|
|
155
156
|
- Returns: `ChainBuilder` with added messages
|
|
156
157
|
|
|
157
158
|
```ts
|
|
@@ -187,14 +188,15 @@ const menu = createI18n(['ja', 'en'] as const)
|
|
|
187
188
|
});
|
|
188
189
|
```
|
|
189
190
|
|
|
190
|
-
#### `.addTemplates<Context, ReturnType = string>()(entries)`
|
|
191
|
+
#### `.addTemplates<Context, ReturnType = string, K extends string = string>()(entries)`
|
|
191
192
|
Adds multiple template function messages at once with a unified context type and custom return type.
|
|
192
193
|
|
|
193
|
-
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.
|
|
194
195
|
|
|
195
196
|
- **Context**: Type parameter for the template function context
|
|
196
197
|
- **ReturnType**: (optional) Type parameter for the return value (defaults to `string`)
|
|
197
|
-
- **
|
|
198
|
+
- **K**: (optional) Type parameter for the keys of the entries record (defaults to `string`)
|
|
199
|
+
- **entries**: `Record<K, Record<Locale, (ctx: Context) => ReturnType>>`
|
|
198
200
|
- Returns: `ChainBuilder` with added template messages
|
|
199
201
|
|
|
200
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" }));
|