@nano_kit/intl 3.0.0-prerelease.20260910112126 → 3.2.1
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/context.d.ts +1 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/format/choose.d.ts +16 -0
- package/dist/format/choose.d.ts.map +1 -0
- package/dist/format/format.d.ts +3 -2
- package/dist/format/format.d.ts.map +1 -1
- package/dist/format/index.d.ts +2 -0
- package/dist/format/index.d.ts.map +1 -1
- package/dist/format/markup.d.ts +18 -0
- package/dist/format/markup.d.ts.map +1 -1
- package/dist/format/match.d.ts.map +1 -1
- package/dist/format/params.d.ts +1 -1
- package/dist/format/relativetime.d.ts +24 -10
- package/dist/format/relativetime.d.ts.map +1 -1
- package/dist/format/rich.d.ts +25 -5
- package/dist/format/rich.d.ts.map +1 -1
- package/dist/format/types.d.ts +1 -1
- package/dist/format/types.d.ts.map +1 -1
- package/dist/format/wrap.d.ts +15 -0
- package/dist/format/wrap.d.ts.map +1 -0
- package/dist/index.development.js +105 -31
- package/dist/index.development.js.map +1 -1
- package/dist/index.production.js +104 -30
- package/dist/index.production.js.map +1 -1
- package/dist/utils.d.ts +1 -0
- package/dist/utils.d.ts.map +1 -1
- package/package.json +3 -3
package/dist/context.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export declare class IntlContext<L extends string, T extends TranslationData<T>
|
|
|
11
11
|
#private;
|
|
12
12
|
/**
|
|
13
13
|
* Active locale accessor.
|
|
14
|
+
* With a full-data loader, it keeps the previous locale until the translations of the next one load.
|
|
14
15
|
*/
|
|
15
16
|
readonly $locale: Accessor<L>;
|
|
16
17
|
/**
|
package/dist/context.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,cAAc,EAOpB,MAAM,iBAAiB,CAAA;A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1
|
+
import type { Signalish } from '@nano_kit/store';
|
|
2
|
+
import type { AnyFormat, Format, FormatInput, FormatOutput } from './types.js';
|
|
3
|
+
export type ChooseFormats<F extends Record<string, AnyFormat>> = {
|
|
4
|
+
[K in keyof F]: (value: FormatInput<F[K]> | null) => FormatOutput<F[K]>;
|
|
5
|
+
};
|
|
6
|
+
export type ChooseArg<V> = [V] extends [Record<string, unknown>] ? {
|
|
7
|
+
[K in keyof V]: Signalish<V[K]>;
|
|
8
|
+
} : Signalish<V>;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a callable message that renders a value with the formats a function chooses.
|
|
11
|
+
* @param formats - Formats keyed by name, like `datetime` or `relativetime`.
|
|
12
|
+
* @param fn - Function that receives the formats bound to the active locale, the value of the call read from a signal or an accessor, and the active locale, and returns what the message renders. Each value of a plain object passed instead is read the same way.
|
|
13
|
+
* @returns Format that returns the callable message.
|
|
14
|
+
*/
|
|
15
|
+
export declare function choose<F extends Record<string, AnyFormat>, V = FormatInput<F[keyof F]> | null, O = unknown>(formats: F, fn: (formats: ChooseFormats<F>, value: V, locale: string) => O): Format<undefined, (value: ChooseArg<V>) => O>;
|
|
16
|
+
//# sourceMappingURL=choose.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"choose.d.ts","sourceRoot":"","sources":["../../src/format/choose.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAGhD,OAAO,KAAK,EACV,SAAS,EACT,MAAM,EACN,WAAW,EACX,YAAY,EACb,MAAM,YAAY,CAAA;AAEnB,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,IAAI;KAC9D,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,KAAK,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CACxE,CAAA;AAED,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAC5D;KACC,CAAC,IAAI,MAAM,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAChC,GACC,SAAS,CAAC,CAAC,CAAC,CAAA;AAEhB;;;;;GAKG;AACH,wBAAgB,MAAM,CACpB,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EACnC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,IAAI,EAClC,CAAC,GAAG,OAAO,EAEX,OAAO,EAAE,CAAC,EACV,EAAE,EAAE,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,KAAK,CAAC,GAC7D,MAAM,CAAC,SAAS,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAA"}
|
package/dist/format/format.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { type Signalish } from '@nano_kit/store';
|
|
1
2
|
import type { AnyFormat, Format, FormatInput, FormatOutput } from './types.js';
|
|
2
|
-
export type Formatter<F extends AnyFormat> = (value: FormatInput<F>) => FormatOutput<F>;
|
|
3
|
+
export type Formatter<F extends AnyFormat> = (value: Signalish<FormatInput<F> | null>) => FormatOutput<F>;
|
|
3
4
|
/**
|
|
4
5
|
* Creates a formatter value from another format.
|
|
5
6
|
* @param type - Format used to format values later.
|
|
6
|
-
* @returns Format that returns a value formatter bound to the current context.
|
|
7
|
+
* @returns Format that returns a value formatter bound to the current context. The formatter reads a signal or an accessor passed as the value and takes `null` as a missing value.
|
|
7
8
|
*/
|
|
8
9
|
export declare function format<F extends AnyFormat>(type: F): Format<undefined, Formatter<F>>;
|
|
9
10
|
//# sourceMappingURL=format.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../src/format/format.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../src/format/format.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,SAAS,EAEf,MAAM,iBAAiB,CAAA;AAExB,OAAO,KAAK,EACV,SAAS,EACT,MAAM,EACN,WAAW,EACX,YAAY,EACb,MAAM,YAAY,CAAA;AAEnB,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,SAAS,IAAI,CAC3C,KAAK,EAAE,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,KACpC,YAAY,CAAC,CAAC,CAAC,CAAA;AAEpB;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,CAAC,SAAS,SAAS,EACxC,IAAI,EAAE,CAAC,GACN,MAAM,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,CAAA"}
|
package/dist/format/index.d.ts
CHANGED
|
@@ -11,6 +11,8 @@ export * from './rich.js';
|
|
|
11
11
|
export * from './markup.js';
|
|
12
12
|
export * from './format.js';
|
|
13
13
|
export * from './params.js';
|
|
14
|
+
export * from './wrap.js';
|
|
15
|
+
export * from './choose.js';
|
|
14
16
|
export * from './match.js';
|
|
15
17
|
export * from './plural.js';
|
|
16
18
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/format/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,MAAM,EACN,SAAS,EACT,UAAU,EACV,eAAe,EAChB,MAAM,YAAY,CAAA;AACnB,cAAc,UAAU,CAAA;AACxB,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,mBAAmB,CAAA;AACjC,cAAc,eAAe,CAAA;AAC7B,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/format/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,MAAM,EACN,SAAS,EACT,UAAU,EACV,eAAe,EAChB,MAAM,YAAY,CAAA;AACnB,cAAc,UAAU,CAAA;AACxB,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,mBAAmB,CAAA;AACjC,cAAc,eAAe,CAAA;AAC7B,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA"}
|
package/dist/format/markup.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Format } from './types.js';
|
|
2
|
+
import { type RichPreformat } from './rich.js';
|
|
2
3
|
export type MarkupTag = (chunks: string) => string;
|
|
3
4
|
export type MarkupTags = Record<string, MarkupTag>;
|
|
4
5
|
/**
|
|
@@ -14,4 +15,21 @@ export declare function markup(tags: MarkupTags): Format<string | undefined, str
|
|
|
14
15
|
* @returns Formatter that returns a mapped string.
|
|
15
16
|
*/
|
|
16
17
|
export declare function markup(fallback: string, tags: MarkupTags): Format<string | undefined, string>;
|
|
18
|
+
/**
|
|
19
|
+
* Creates a markup formatter that formats each text piece before the tag handlers get it.
|
|
20
|
+
* Values filled in by the preformat are never parsed as tags.
|
|
21
|
+
* @param tags - Tag handlers used to map markup tags to strings.
|
|
22
|
+
* @param preformat - Formatter of each text piece between the tags, like `params`.
|
|
23
|
+
* @returns Formatter that returns a message function of the preformat parameters.
|
|
24
|
+
*/
|
|
25
|
+
export declare function markup<P>(tags: MarkupTags, preformat: RichPreformat<P>): Format<string | undefined, (params: P) => string | undefined>;
|
|
26
|
+
/**
|
|
27
|
+
* Creates a markup formatter with a fallback message that formats each text piece before the tag handlers get it.
|
|
28
|
+
* Values filled in by the preformat are never parsed as tags.
|
|
29
|
+
* @param fallback - Message used when the input is `undefined` or `null`.
|
|
30
|
+
* @param tags - Tag handlers used to map markup tags to strings.
|
|
31
|
+
* @param preformat - Formatter of each text piece between the tags, like `params`.
|
|
32
|
+
* @returns Formatter that returns a message function of the preformat parameters.
|
|
33
|
+
*/
|
|
34
|
+
export declare function markup<P>(fallback: string, tags: MarkupTags, preformat: RichPreformat<P>): Format<string | undefined, (params: P) => string>;
|
|
17
35
|
//# sourceMappingURL=markup.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markup.d.ts","sourceRoot":"","sources":["../../src/format/markup.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"markup.d.ts","sourceRoot":"","sources":["../../src/format/markup.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACxC,OAAO,EACL,KAAK,aAAa,EAGnB,MAAM,WAAW,CAAA;AAElB,MAAM,MAAM,SAAS,GAAG,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,CAAA;AAElD,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;AAElD;;;;GAIG;AACH,wBAAgB,MAAM,CACpB,IAAI,EAAE,UAAU,GACf,MAAM,CAAC,MAAM,GAAG,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;AAEjD;;;;;GAKG;AACH,wBAAgB,MAAM,CACpB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,UAAU,GACf,MAAM,CAAC,MAAM,GAAG,SAAS,EAAE,MAAM,CAAC,CAAA;AAErC;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,CAAC,EACtB,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,aAAa,CAAC,CAAC,CAAC,GAC1B,MAAM,CAAC,MAAM,GAAG,SAAS,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,MAAM,GAAG,SAAS,CAAC,CAAA;AAEhE;;;;;;;GAOG;AACH,wBAAgB,MAAM,CAAC,CAAC,EACtB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,aAAa,CAAC,CAAC,CAAC,GAC1B,MAAM,CAAC,MAAM,GAAG,SAAS,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,MAAM,CAAC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"match.d.ts","sourceRoot":"","sources":["../../src/format/match.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,SAAS,EAIf,MAAM,iBAAiB,CAAA;AACxB,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAChD,OAAO,KAAK,EACV,SAAS,
|
|
1
|
+
{"version":3,"file":"match.d.ts","sourceRoot":"","sources":["../../src/format/match.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,SAAS,EAIf,MAAM,iBAAiB,CAAA;AACxB,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAChD,OAAO,KAAK,EACV,SAAS,EAET,YAAY,EACZ,MAAM,EACN,YAAY,EACZ,mBAAmB,EACnB,QAAQ,EACT,MAAM,YAAY,CAAA;AAEnB,KAAK,UAAU,CAAC,CAAC,IAAI,CAAC,SAAS,SAAS,CAAC,MAAM,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,EAAE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,SAAS,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,EAElD,KAAK,EAAE,CAAC,GACP,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CAWf;AAED;;;;GAIG;AAEH,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEzC;AAED;;;;;GAKG;AACH,wBAAgB,KAAK,CACnB,KAAK,CAAC,CAAC,SAAS,MAAM,EACtB,KAAK,CAAC,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,EAElD,KAAK,EAAE,CAAC,EACR,MAAM,CAAC,EAAE,MAAM,GACd,MAAM,CACP,YAAY,CAAC,CAAC,CAAC,GAAG,MAAM,EACxB,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CACd,CAAA;AAED;;;;;;GAMG;AACH,wBAAgB,KAAK,CACnB,KAAK,CAAC,CAAC,SAAS,MAAM,EACtB,KAAK,CAAC,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,EAElD,KAAK,EAAE,CAAC,EACR,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,EACrB,MAAM,CAAC,EAAE,MAAM,GACd,MAAM,CACP,YAAY,CAAC,CAAC,CAAC,GAAG,MAAM,EACxB,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CACd,CAAA"}
|
package/dist/format/params.d.ts
CHANGED
|
@@ -23,7 +23,7 @@ export declare function params<const P extends ParamsFormats>(params: P): Format
|
|
|
23
23
|
/**
|
|
24
24
|
* Creates a parameter interpolation formatter that first applies another formatter.
|
|
25
25
|
* @param params - Parameter formatters keyed by placeholder name.
|
|
26
|
-
* @param preformat - Formatter applied before replacing placeholders.
|
|
26
|
+
* @param preformat - Formatter applied before replacing placeholders. `match` and `plural` leave the placeholders of the parameters listed here to be replaced by these formatters.
|
|
27
27
|
* @returns Formatter that replaces `{key}` placeholders using provided parameter values.
|
|
28
28
|
*/
|
|
29
29
|
export declare function params<const P extends ParamsFormats, F = string | undefined, O extends PreformatOutput = string | undefined | ParamsFn<P>>(params: P, preformat: Format<F, O>): Format<F, ComposedParamsFn<P, O>>;
|
|
@@ -1,31 +1,45 @@
|
|
|
1
|
+
import { type Signalish } from '@nano_kit/store';
|
|
1
2
|
import type { Format } from './types.js';
|
|
2
|
-
export type
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
export type RelativeTimeUnit = Exclude<Intl.RelativeTimeFormatUnitSingular, 'quarter'>;
|
|
4
|
+
export interface RelativeTimeOptions extends Intl.RelativeTimeFormatOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The largest unit to format in, `'year'` by default.
|
|
7
|
+
*/
|
|
8
|
+
largestUnit?: RelativeTimeUnit;
|
|
9
|
+
/**
|
|
10
|
+
* The smallest unit to format in, `'second'` by default.
|
|
11
|
+
*/
|
|
12
|
+
smallestUnit?: RelativeTimeUnit;
|
|
13
|
+
/**
|
|
14
|
+
* The moment to count from, the current time by default. A signal or an accessor is followed.
|
|
15
|
+
*/
|
|
16
|
+
relativeTo?: Signalish<Date | number | string>;
|
|
17
|
+
}
|
|
5
18
|
/**
|
|
6
19
|
* Creates a relative time formatter in raw mode.
|
|
7
20
|
* @param options - Pass `false` to return the input value without formatting.
|
|
8
21
|
* @returns Formatter that returns the input value or `undefined`.
|
|
9
22
|
*/
|
|
10
|
-
export declare function relativetime(options: false): Format<number | undefined>;
|
|
23
|
+
export declare function relativetime(options: false): Format<Date | number | string | undefined>;
|
|
11
24
|
/**
|
|
12
|
-
* Creates a locale-aware relative time formatter.
|
|
13
|
-
*
|
|
25
|
+
* Creates a locale-aware relative time formatter. It counts the distance from the input to
|
|
26
|
+
* `relativeTo` and formats it in the largest unit the distance fills.
|
|
27
|
+
* @param options - `Intl.RelativeTimeFormat` options with the unit range and the moment to count from.
|
|
14
28
|
* @returns Formatter that returns a formatted string or `undefined`.
|
|
15
29
|
*/
|
|
16
|
-
export declare function relativetime(options
|
|
30
|
+
export declare function relativetime(options?: RelativeTimeOptions): Format<Date | number | string | undefined, string | undefined>;
|
|
17
31
|
/**
|
|
18
32
|
* Creates a relative time formatter in raw mode with a fallback value.
|
|
19
33
|
* @param fallback - Value returned when the input is `undefined` or `null`.
|
|
20
34
|
* @param options - Pass `false` to return the input value or fallback without formatting.
|
|
21
35
|
* @returns Formatter that returns the input value or fallback.
|
|
22
36
|
*/
|
|
23
|
-
export declare function relativetime(fallback: number, options: false): Format<number | undefined, number>;
|
|
37
|
+
export declare function relativetime(fallback: Date | number | string, options: false): Format<Date | number | string | undefined, Date | number | string>;
|
|
24
38
|
/**
|
|
25
39
|
* Creates a locale-aware relative time formatter with a fallback value.
|
|
26
40
|
* @param fallback - Value used when the input is `undefined` or `null`.
|
|
27
|
-
* @param options - `Intl.RelativeTimeFormat` options with
|
|
41
|
+
* @param options - `Intl.RelativeTimeFormat` options with the unit range and the moment to count from, or `{}` for the defaults.
|
|
28
42
|
* @returns Formatter that returns a formatted string or `undefined`.
|
|
29
43
|
*/
|
|
30
|
-
export declare function relativetime(fallback: number, options: RelativeTimeOptions): Format<number | undefined, string | undefined>;
|
|
44
|
+
export declare function relativetime(fallback: Date | number | string, options: RelativeTimeOptions): Format<Date | number | string | undefined, string | undefined>;
|
|
31
45
|
//# sourceMappingURL=relativetime.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"relativetime.d.ts","sourceRoot":"","sources":["../../src/format/relativetime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAGxC,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"relativetime.d.ts","sourceRoot":"","sources":["../../src/format/relativetime.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,SAAS,EAEf,MAAM,iBAAiB,CAAA;AACxB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAGxC,MAAM,MAAM,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,8BAA8B,EAAE,SAAS,CAAC,CAAA;AAEtF,MAAM,WAAW,mBAAoB,SAAQ,IAAI,CAAC,yBAAyB;IACzE;;OAEG;IACH,WAAW,CAAC,EAAE,gBAAgB,CAAA;IAC9B;;OAEG;IACH,YAAY,CAAC,EAAE,gBAAgB,CAAA;IAC/B;;OAEG;IACH,UAAU,CAAC,EAAE,SAAS,CAAC,IAAI,GAAG,MAAM,GAAG,MAAM,CAAC,CAAA;CAC/C;AAiCD;;;;GAIG;AACH,wBAAgB,YAAY,CAC1B,OAAO,EAAE,KAAK,GACb,MAAM,CAAC,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC,CAAA;AAE7C;;;;;GAKG;AACH,wBAAgB,YAAY,CAC1B,OAAO,CAAC,EAAE,mBAAmB,GAC5B,MAAM,CAAC,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;AAEjE;;;;;GAKG;AACH,wBAAgB,YAAY,CAC1B,QAAQ,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAChC,OAAO,EAAE,KAAK,GACb,MAAM,CAAC,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,CAAC,CAAA;AAErE;;;;;GAKG;AACH,wBAAgB,YAAY,CAC1B,QAAQ,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAChC,OAAO,EAAE,mBAAmB,GAC3B,MAAM,CAAC,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA"}
|
package/dist/format/rich.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type { Format } from './types.js';
|
|
1
|
+
import type { FnWithParams, Format } from './types.js';
|
|
2
2
|
export type RichChunks<T> = (string | T)[];
|
|
3
3
|
export type RichTag<T> = (chunks: RichChunks<T>, index: number) => T;
|
|
4
4
|
export type RichTags<T> = Record<string, RichTag<T>>;
|
|
5
|
+
export type RichPreformat<P> = Format<string | undefined, FnWithParams<P>>;
|
|
5
6
|
/**
|
|
6
7
|
* Creates a rich-text formatter.
|
|
7
8
|
* @param tags - Tag handlers used to map rich-text tags to output chunks.
|
|
@@ -15,13 +16,31 @@ export declare function rich<T>(tags: RichTags<T>): Format<string | undefined, R
|
|
|
15
16
|
* @returns Formatter that returns mapped chunks.
|
|
16
17
|
*/
|
|
17
18
|
export declare function rich<T>(fallback: string, tags: RichTags<T>): Format<string | undefined, RichChunks<T>>;
|
|
19
|
+
/**
|
|
20
|
+
* Creates a rich-text formatter that formats each text piece before the tag handlers get it.
|
|
21
|
+
* Values filled in by the preformat are never parsed as tags.
|
|
22
|
+
* @param tags - Tag handlers used to map rich-text tags to output chunks.
|
|
23
|
+
* @param preformat - Formatter of each text piece between the tags, like `params`.
|
|
24
|
+
* @returns Formatter that returns a message function of the preformat parameters.
|
|
25
|
+
*/
|
|
26
|
+
export declare function rich<T, P>(tags: RichTags<T>, preformat: RichPreformat<P>): Format<string | undefined, (params: P) => RichChunks<T> | undefined>;
|
|
27
|
+
/**
|
|
28
|
+
* Creates a rich-text formatter with a fallback message that formats each text piece before the tag handlers get it.
|
|
29
|
+
* Values filled in by the preformat are never parsed as tags.
|
|
30
|
+
* @param fallback - Message used when the input is `undefined` or `null`.
|
|
31
|
+
* @param tags - Tag handlers used to map rich-text tags to output chunks.
|
|
32
|
+
* @param preformat - Formatter of each text piece between the tags, like `params`.
|
|
33
|
+
* @returns Formatter that returns a message function of the preformat parameters.
|
|
34
|
+
*/
|
|
35
|
+
export declare function rich<T, P>(fallback: string, tags: RichTags<T>, preformat: RichPreformat<P>): Format<string | undefined, (params: P) => RichChunks<T>>;
|
|
18
36
|
/**
|
|
19
37
|
* Creates a rich-text formatter.
|
|
20
38
|
* @param fallbackOrTags - Fallback message or tag handlers.
|
|
21
|
-
* @param
|
|
22
|
-
* @
|
|
39
|
+
* @param tagsOrPreformat - Tag handlers used when fallback is provided, or the preformat.
|
|
40
|
+
* @param maybePreformat - Formatter of each text piece used when fallback is provided.
|
|
41
|
+
* @returns Formatter that returns mapped chunks, a message function of them or `undefined`.
|
|
23
42
|
*/
|
|
24
|
-
export declare function rich<T>(fallbackOrTags: string | RichTags<T> | undefined,
|
|
43
|
+
export declare function rich<T, P>(fallbackOrTags: string | RichTags<T> | undefined, tagsOrPreformat?: RichTags<T> | RichPreformat<P>, maybePreformat?: RichPreformat<P>): Format<string | undefined, RichChunks<T> | ((params: P) => RichChunks<T> | undefined) | undefined>;
|
|
25
44
|
/**
|
|
26
45
|
* Maps rich-text tags in a string to chunks using provided tag handlers.
|
|
27
46
|
* Each handler call receives an unique index, usable as a key for framework nodes.
|
|
@@ -29,7 +48,8 @@ export declare function rich<T>(fallbackOrTags: string | RichTags<T> | undefined
|
|
|
29
48
|
* Unknown or malformed tags are ignored as markup and only their text content is kept.
|
|
30
49
|
* @param input - Rich-text input string.
|
|
31
50
|
* @param tags - Tag handlers used to map known tags.
|
|
51
|
+
* @param formatText - Formatter of each text piece between the tags, applied before the tag handlers get it.
|
|
32
52
|
* @returns Mapped chunks.
|
|
33
53
|
*/
|
|
34
|
-
export declare function mapTags<T>(input: string, tags: RichTags<T
|
|
54
|
+
export declare function mapTags<T>(input: string, tags: RichTags<T>, formatText?: (text: string) => string): RichChunks<T>;
|
|
35
55
|
//# sourceMappingURL=rich.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rich.d.ts","sourceRoot":"","sources":["../../src/format/rich.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"rich.d.ts","sourceRoot":"","sources":["../../src/format/rich.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,MAAM,EACP,MAAM,YAAY,CAAA;AAEnB,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI,CAA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GAAG,SAAS,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,CAAC,CAAA;AAE3D;;;;;;GAMG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,CAAC,EACvB,cAAc,EAAE,MAAM,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,SAAS,EAChD,eAAe,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,EAChD,cAAc,CAAC,EAAE,aAAa,CAAC,CAAC,CAAC,GAChC,MAAM,CAAC,MAAM,GAAG,SAAS,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,GAAG,SAAS,CAAC,CAAA;AAyCrG;;;;;;;;;GASG;AAEH,wBAAgB,OAAO,CAAC,CAAC,EACvB,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,EACjB,UAAU,GAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAe,GAC5C,UAAU,CAAC,CAAC,CAAC,CAwDf"}
|
package/dist/format/types.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export type FormatsInput<T extends Partial<Record<PropertyKey, AnyFormat>>> = Pa
|
|
|
11
11
|
[K in keyof T]: FormatInput<T[K]>;
|
|
12
12
|
}>;
|
|
13
13
|
export type FnWithParams<P> = (params: P) => string | undefined;
|
|
14
|
-
export type FnWithAnyParams =
|
|
14
|
+
export type FnWithAnyParams = (params: any, formats?: Record<string, unknown>) => string | undefined;
|
|
15
15
|
export interface IntlFormat<T> {
|
|
16
16
|
format(value: T, ...args: unknown[]): string;
|
|
17
17
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/format/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM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MAAM,GAAG,SAAS,CAAA;AAG/D,MAAM,MAAM,eAAe,GAAG,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,MAAM,GAAG,SAAS,CAAA;AAEpG,MAAM,WAAW,UAAU,CAAC,CAAC;IAC3B,MAAM,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,MAAM,CAAA;CAC7C;AAED,MAAM,WAAW,eAAe,CAAC,CAAC,CAAE,SAAQ,UAAU,CAAC,CAAC,CAAC;IACvD,WAAW,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,MAAM,CAAA;CACpC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type Accessor, type SignalishValue } from '@nano_kit/store';
|
|
2
|
+
import type { Format } from './types.js';
|
|
3
|
+
export type WrapValues<A> = A extends Accessor<infer V> ? V : A extends Record<string, unknown> ? {
|
|
4
|
+
[K in keyof A]: SignalishValue<A[K]>;
|
|
5
|
+
} : A;
|
|
6
|
+
export type WrapArgs<R> = [R] extends [(arg: infer A) => unknown] ? [message: R, values: WrapValues<A>, locale: string] : [message: R, locale: string];
|
|
7
|
+
export type Wrapped<R, O> = [R] extends [(arg: infer A) => unknown] ? (arg: A) => O : O;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a formatter that wraps a message with a function deciding what it renders.
|
|
10
|
+
* @param format - Formatter of the message to wrap.
|
|
11
|
+
* @param fn - Function that receives the wrapped message and the active locale and returns what the message renders. A message function is passed as is, followed by the values of its call read from signals and accessors, for `fn` to call with the values it chooses.
|
|
12
|
+
* @returns Formatter that returns what `fn` returns, or a message function returning it.
|
|
13
|
+
*/
|
|
14
|
+
export declare function wrap<I, R, O>(format: Format<I, R>, fn: (...args: WrapArgs<R>) => O): Format<I, Wrapped<R, O>>;
|
|
15
|
+
//# sourceMappingURL=wrap.d.ts.map
|
|
@@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AAC,CAAC,CAAC,CAAA"}
|
|
@@ -38,6 +38,12 @@ function memo(fn) {
|
|
|
38
38
|
return value;
|
|
39
39
|
};
|
|
40
40
|
}
|
|
41
|
+
function read(arg) {
|
|
42
|
+
if (arg?.constructor !== Object) return $get(arg);
|
|
43
|
+
const values = {};
|
|
44
|
+
for (const key in arg) values[key] = $get(arg[key]);
|
|
45
|
+
return values;
|
|
46
|
+
}
|
|
41
47
|
//#endregion
|
|
42
48
|
//#region src/format/intlformat.ts
|
|
43
49
|
function defaultFormat(fmt, value) {
|
|
@@ -85,8 +91,31 @@ function range(format, optionsOrFallback, maybeOptions) {
|
|
|
85
91
|
}
|
|
86
92
|
//#endregion
|
|
87
93
|
//#region src/format/relativetime.ts
|
|
88
|
-
|
|
89
|
-
|
|
94
|
+
const units = [
|
|
95
|
+
"year",
|
|
96
|
+
"month",
|
|
97
|
+
"week",
|
|
98
|
+
"day",
|
|
99
|
+
"hour",
|
|
100
|
+
"minute",
|
|
101
|
+
"second"
|
|
102
|
+
];
|
|
103
|
+
const lengths = [
|
|
104
|
+
31536e6,
|
|
105
|
+
2592e6,
|
|
106
|
+
6048e5,
|
|
107
|
+
864e5,
|
|
108
|
+
36e5,
|
|
109
|
+
6e4,
|
|
110
|
+
1e3
|
|
111
|
+
];
|
|
112
|
+
function relativeTimeFormat(fmt, value, { largestUnit = "year", smallestUnit = "second", relativeTo = Date.now() } = {}) {
|
|
113
|
+
const distance = new Date(value).getTime() - new Date($get(relativeTo)).getTime();
|
|
114
|
+
const last = units.indexOf(smallestUnit);
|
|
115
|
+
let index = units.indexOf(largestUnit);
|
|
116
|
+
while (index < last && Math.abs(distance) < lengths[index]) index++;
|
|
117
|
+
const count = Math.trunc(distance / lengths[index]);
|
|
118
|
+
return fmt.format(count, count || index < 4 ? units[index] : "second");
|
|
90
119
|
}
|
|
91
120
|
/* @__NO_SIDE_EFFECTS__ */
|
|
92
121
|
function relativetime(optionsOrFallback, maybeOptions) {
|
|
@@ -107,17 +136,19 @@ function list(optionsOrFallback, maybeOptions) {
|
|
|
107
136
|
//#endregion
|
|
108
137
|
//#region src/format/rich.ts
|
|
109
138
|
/* @__NO_SIDE_EFFECTS__ */
|
|
110
|
-
function rich(fallbackOrTags,
|
|
139
|
+
function rich(fallbackOrTags, tagsOrPreformat, maybePreformat) {
|
|
111
140
|
let fallback;
|
|
112
|
-
let tags;
|
|
113
|
-
|
|
141
|
+
let tags = fallbackOrTags;
|
|
142
|
+
let preformat = maybePreformat;
|
|
143
|
+
if (isFunction(tagsOrPreformat)) preformat = tagsOrPreformat;
|
|
144
|
+
else if (tagsOrPreformat) {
|
|
114
145
|
fallback = fallbackOrTags;
|
|
115
|
-
tags =
|
|
116
|
-
}
|
|
117
|
-
return (
|
|
146
|
+
tags = tagsOrPreformat;
|
|
147
|
+
}
|
|
148
|
+
return (ctx, input) => {
|
|
118
149
|
const text = input ?? fallback;
|
|
119
|
-
if (text === void 0)
|
|
120
|
-
return /* @__PURE__ */ mapTags(text, tags);
|
|
150
|
+
if (preformat) return (params) => text === void 0 ? text : /* @__PURE__ */ mapTags(text, tags, (piece) => preformat(ctx, piece)(params));
|
|
151
|
+
return text === void 0 ? text : /* @__PURE__ */ mapTags(text, tags);
|
|
121
152
|
};
|
|
122
153
|
}
|
|
123
154
|
const tagRegex = /<(\/?)([A-Za-z][\w.-]*)\s*(\/?)>/g;
|
|
@@ -128,10 +159,11 @@ const tagRegex = /<(\/?)([A-Za-z][\w.-]*)\s*(\/?)>/g;
|
|
|
128
159
|
* Unknown or malformed tags are ignored as markup and only their text content is kept.
|
|
129
160
|
* @param input - Rich-text input string.
|
|
130
161
|
* @param tags - Tag handlers used to map known tags.
|
|
162
|
+
* @param formatText - Formatter of each text piece between the tags, applied before the tag handlers get it.
|
|
131
163
|
* @returns Mapped chunks.
|
|
132
164
|
*/
|
|
133
165
|
/* @__NO_SIDE_EFFECTS__ */
|
|
134
|
-
function mapTags(input, tags) {
|
|
166
|
+
function mapTags(input, tags, formatText = String) {
|
|
135
167
|
const root = {
|
|
136
168
|
tag: "",
|
|
137
169
|
chunks: []
|
|
@@ -144,7 +176,7 @@ function mapTags(input, tags) {
|
|
|
144
176
|
while (match = tagRegex.exec(input)) {
|
|
145
177
|
const [, closing, tag, selfClosing] = match;
|
|
146
178
|
const current = stack[stack.length - 1];
|
|
147
|
-
if (match.index > lastIndex) current.chunks.push(input.slice(lastIndex, match.index));
|
|
179
|
+
if (match.index > lastIndex) current.chunks.push(formatText(input.slice(lastIndex, match.index)));
|
|
148
180
|
lastIndex = tagRegex.lastIndex;
|
|
149
181
|
if (tag in tags) {
|
|
150
182
|
if (closing) {
|
|
@@ -160,7 +192,7 @@ function mapTags(input, tags) {
|
|
|
160
192
|
}
|
|
161
193
|
tagRegex.lastIndex = lastIndex;
|
|
162
194
|
}
|
|
163
|
-
if (lastIndex < input.length) stack[stack.length - 1].chunks.push(input.slice(lastIndex));
|
|
195
|
+
if (lastIndex < input.length) stack[stack.length - 1].chunks.push(formatText(input.slice(lastIndex)));
|
|
164
196
|
for (let i = stack.length - 1; i > 0; i--) {
|
|
165
197
|
const frame = stack[i];
|
|
166
198
|
stack[i - 1].chunks.push(...frame.chunks);
|
|
@@ -170,23 +202,28 @@ function mapTags(input, tags) {
|
|
|
170
202
|
//#endregion
|
|
171
203
|
//#region src/format/markup.ts
|
|
172
204
|
/* @__NO_SIDE_EFFECTS__ */
|
|
173
|
-
function markup(fallbackOrTags,
|
|
205
|
+
function markup(fallbackOrTags, tagsOrPreformat, maybePreformat) {
|
|
174
206
|
let fallback;
|
|
175
|
-
let tags;
|
|
176
|
-
|
|
207
|
+
let tags = fallbackOrTags;
|
|
208
|
+
let preformat = maybePreformat;
|
|
209
|
+
if (isFunction(tagsOrPreformat)) preformat = tagsOrPreformat;
|
|
210
|
+
else if (tagsOrPreformat) {
|
|
177
211
|
fallback = fallbackOrTags;
|
|
178
|
-
tags =
|
|
179
|
-
}
|
|
212
|
+
tags = tagsOrPreformat;
|
|
213
|
+
}
|
|
180
214
|
const richTags = {};
|
|
181
215
|
for (const [tag, format] of Object.entries(tags)) richTags[tag] = (chunks) => format(chunks.join(""));
|
|
182
|
-
const richFormat = /* @__PURE__ */ rich(fallback, richTags);
|
|
183
|
-
return (ctx, input) =>
|
|
216
|
+
const richFormat = /* @__PURE__ */ rich(fallback, richTags, preformat);
|
|
217
|
+
return (ctx, input) => {
|
|
218
|
+
const chunks = richFormat(ctx, input);
|
|
219
|
+
return isFunction(chunks) ? (params) => chunks(params)?.join("") : chunks?.join("");
|
|
220
|
+
};
|
|
184
221
|
}
|
|
185
222
|
//#endregion
|
|
186
223
|
//#region src/format/format.ts
|
|
187
224
|
/* @__NO_SIDE_EFFECTS__ */
|
|
188
225
|
function format(type) {
|
|
189
|
-
return (ctx) => (value) => type(ctx, value);
|
|
226
|
+
return (ctx) => (value) => type(ctx, $get(value));
|
|
190
227
|
}
|
|
191
228
|
//#endregion
|
|
192
229
|
//#region src/format/params.ts
|
|
@@ -211,16 +248,37 @@ function params(params, preformat) {
|
|
|
211
248
|
}
|
|
212
249
|
return (ctx, input) => {
|
|
213
250
|
const preformatted = preformat ? preformat(ctx, input) : input;
|
|
214
|
-
if (isFunction(preformatted)) return (
|
|
215
|
-
const text = preformatted(params);
|
|
251
|
+
if (isFunction(preformatted)) return (values) => {
|
|
252
|
+
const text = preformatted(values, params);
|
|
216
253
|
if (text === void 0 || !replaceAll) return text;
|
|
217
|
-
return replaceAll(ctx, text,
|
|
254
|
+
return replaceAll(ctx, text, values);
|
|
218
255
|
};
|
|
219
256
|
if (preformatted === void 0 || !replaceAll) return () => preformatted;
|
|
220
257
|
return (params) => replaceAll(ctx, preformatted, params);
|
|
221
258
|
};
|
|
222
259
|
}
|
|
223
260
|
//#endregion
|
|
261
|
+
//#region src/format/wrap.ts
|
|
262
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
263
|
+
function wrap(format, fn) {
|
|
264
|
+
return (ctx, input) => {
|
|
265
|
+
const message = format(ctx, input);
|
|
266
|
+
const locale = ctx.$locale();
|
|
267
|
+
return isFunction(message) ? (arg) => fn(message, read(arg), locale) : fn(message, locale);
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
//#endregion
|
|
271
|
+
//#region src/format/choose.ts
|
|
272
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
273
|
+
function choose(formats, fn) {
|
|
274
|
+
return (ctx) => {
|
|
275
|
+
const bound = {};
|
|
276
|
+
const locale = ctx.$locale();
|
|
277
|
+
for (const key in formats) bound[key] = (value) => formats[key](ctx, value);
|
|
278
|
+
return (value) => fn(bound, read(value), locale);
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
//#endregion
|
|
224
282
|
//#region src/format/match.ts
|
|
225
283
|
/**
|
|
226
284
|
* Creates a reusable cases resolver for `match`.
|
|
@@ -269,10 +327,11 @@ function match(param, casesOrGetKey, maybeGetKey) {
|
|
|
269
327
|
resolvedCases = { ...resolvedCases };
|
|
270
328
|
cases(ctx, input, resolvedCases);
|
|
271
329
|
}
|
|
272
|
-
return (params) => {
|
|
330
|
+
return (params, formats) => {
|
|
273
331
|
const value = $get(typeof params === "object" ? params[param] : params);
|
|
274
332
|
const resolvedCase = isString ? input : resolvedCases[getKey(value, locale, resolvedCases)];
|
|
275
|
-
|
|
333
|
+
const message = isFunction(resolvedCase) ? resolvedCase(params, formats) : resolvedCase;
|
|
334
|
+
return formats && param in formats ? message : message?.replace(valuePattern, value);
|
|
276
335
|
};
|
|
277
336
|
};
|
|
278
337
|
}
|
|
@@ -320,6 +379,7 @@ var IntlContext = class {
|
|
|
320
379
|
#loader;
|
|
321
380
|
/**
|
|
322
381
|
* Active locale accessor.
|
|
382
|
+
* With a full-data loader, it keeps the previous locale until the translations of the next one load.
|
|
323
383
|
*/
|
|
324
384
|
$locale;
|
|
325
385
|
/**
|
|
@@ -336,12 +396,26 @@ var IntlContext = class {
|
|
|
336
396
|
* @param loader - Messages loader bound to the active locale.
|
|
337
397
|
*/
|
|
338
398
|
constructor($locale, loader) {
|
|
339
|
-
this.$locale = $locale;
|
|
340
|
-
this.#loader = loader;
|
|
341
399
|
if (isFunction(loader)) {
|
|
400
|
+
this.$locale = $locale;
|
|
342
401
|
this.$loading = signal(false);
|
|
343
402
|
this.$error = signal(void 0);
|
|
344
|
-
|
|
403
|
+
this.#loader = loader;
|
|
404
|
+
} else {
|
|
405
|
+
const [$data, $error, $loading] = loader;
|
|
406
|
+
const $shown = computed((prev) => {
|
|
407
|
+
const data = $data();
|
|
408
|
+
return !$loading() && data || !prev ? [$locale(), data] : prev;
|
|
409
|
+
});
|
|
410
|
+
this.$locale = computed(() => $shown()[0]);
|
|
411
|
+
this.$loading = $loading;
|
|
412
|
+
this.$error = $error;
|
|
413
|
+
this.#loader = [
|
|
414
|
+
computed(() => $shown()[1]),
|
|
415
|
+
$error,
|
|
416
|
+
$loading
|
|
417
|
+
];
|
|
418
|
+
}
|
|
345
419
|
}
|
|
346
420
|
/**
|
|
347
421
|
* Creates reactive messages for a single namespace.
|
|
@@ -367,7 +441,7 @@ var IntlContext = class {
|
|
|
367
441
|
if (entries.length > 0) {
|
|
368
442
|
result = { ...result };
|
|
369
443
|
for (const [key, format] of entries) {
|
|
370
|
-
if (data && !(key in data)) console.warn(`[nano_kit/intl] Missing message "${String(namespace)}.${key}" for locale "${untracked(this.$locale)}"`);
|
|
444
|
+
if (data && format.length > 1 && !(key in data)) console.warn(`[nano_kit/intl] Missing message "${String(namespace)}.${key}" for locale "${untracked(this.$locale)}"`);
|
|
371
445
|
result[key] = format(this, data?.[key]);
|
|
372
446
|
}
|
|
373
447
|
}
|
|
@@ -461,6 +535,6 @@ function direction($locale) {
|
|
|
461
535
|
return computed(() => /* @__PURE__ */ getDirection($locale()));
|
|
462
536
|
}
|
|
463
537
|
//#endregion
|
|
464
|
-
export { IntlContext, capitalize, cases, cases as forms, datetime, deflat, direction, duration, format, getDirection, intl, list, lowercase, mapTags, markup, match, number, other, params, plural, range, raw, relativetime, rich, rtlLocales, text, uppercase };
|
|
538
|
+
export { IntlContext, capitalize, cases, cases as forms, choose, datetime, deflat, direction, duration, format, getDirection, intl, list, lowercase, mapTags, markup, match, number, other, params, plural, range, raw, relativetime, rich, rtlLocales, text, uppercase, wrap };
|
|
465
539
|
|
|
466
540
|
//# sourceMappingURL=index.development.js.map
|