@havelaer/msgs 0.0.2 → 0.0.5
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/functions.d.ts +113 -2
- package/dist/functions.js +112 -2
- package/dist/index-DqWXabHs.d.ts +84 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +46 -0
- package/dist/react.d.ts +91 -1
- package/dist/react.js +79 -0
- package/package.json +15 -6
- package/dist/index-CrRmYrrh.d.ts +0 -22
package/dist/functions.d.ts
CHANGED
|
@@ -1,7 +1,118 @@
|
|
|
1
1
|
import { MessageFunction } from "messageformat/functions";
|
|
2
2
|
|
|
3
3
|
//#region src/functions/index.d.ts
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Currency formatting function for MessageFormat.
|
|
7
|
+
* Formats numbers as currency values.
|
|
8
|
+
*
|
|
9
|
+
* @beta
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* "Price: {$price :currency currency=USD}"
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
declare const currency: MessageFunction<any, any>;
|
|
17
|
+
/**
|
|
18
|
+
* Date formatting function for MessageFormat.
|
|
19
|
+
* Formats dates according to locale conventions.
|
|
20
|
+
*
|
|
21
|
+
* @beta
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* "Today is {$date :date dateStyle=medium}"
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare const date: MessageFunction<any, any>;
|
|
29
|
+
/**
|
|
30
|
+
* Date and time formatting function for MessageFormat.
|
|
31
|
+
* Formats both date and time components.
|
|
32
|
+
*
|
|
33
|
+
* @beta
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* "Event at {$datetime :datetime dateStyle=short timeStyle=short}"
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
declare const datetime: MessageFunction<any, any>;
|
|
41
|
+
/**
|
|
42
|
+
* Percentage formatting function for MessageFormat.
|
|
43
|
+
* Formats numbers as percentages.
|
|
44
|
+
*
|
|
45
|
+
* @beta
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* "Progress: {$progress :percent}"
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
declare const percent: MessageFunction<any, any>;
|
|
53
|
+
/**
|
|
54
|
+
* Number formatting function for MessageFormat.
|
|
55
|
+
* Formats numbers with locale-specific formatting.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* "Count: {$count :number}"
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
declare const number: MessageFunction<any, any>;
|
|
63
|
+
/**
|
|
64
|
+
* Time formatting function for MessageFormat.
|
|
65
|
+
* Formats time values according to locale conventions.
|
|
66
|
+
*
|
|
67
|
+
* @beta
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* "Time: {$time :time timeStyle=short}"
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
declare const time: MessageFunction<any, any>;
|
|
75
|
+
/**
|
|
76
|
+
* Unit formatting function for MessageFormat.
|
|
77
|
+
* Formats numbers with units (length, weight, etc.).
|
|
78
|
+
*
|
|
79
|
+
* @beta
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* "Distance: {$distance :unit unit=mile}"
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
declare const unit: MessageFunction<any, any>;
|
|
87
|
+
/**
|
|
88
|
+
* Integer formatting function for MessageFormat.
|
|
89
|
+
* Formats numbers as integers.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* "Items: {$count :integer}"
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
declare const integer: MessageFunction<any, any>;
|
|
97
|
+
/**
|
|
98
|
+
* Offset formatting function for MessageFormat.
|
|
99
|
+
* Formats timezone offsets.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* "Offset: {$offset :offset}"
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
declare const offset: MessageFunction<any, any>;
|
|
107
|
+
/**
|
|
108
|
+
* String formatting function for MessageFormat.
|
|
109
|
+
* Formats values as strings with optional transformations.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* "Name: {$name :string}"
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
declare const string: MessageFunction<any, any>;
|
|
6
117
|
//#endregion
|
|
7
118
|
export { currency, date, datetime, integer, number, offset, percent, string, time, unit };
|
package/dist/functions.js
CHANGED
|
@@ -1,8 +1,118 @@
|
|
|
1
1
|
import { DefaultFunctions, DraftFunctions } from "messageformat/functions";
|
|
2
2
|
|
|
3
3
|
//#region src/functions/index.ts
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Currency formatting function for MessageFormat.
|
|
6
|
+
* Formats numbers as currency values.
|
|
7
|
+
*
|
|
8
|
+
* @beta
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* "Price: {$price :currency currency=USD}"
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
const currency = DraftFunctions.currency;
|
|
16
|
+
/**
|
|
17
|
+
* Date formatting function for MessageFormat.
|
|
18
|
+
* Formats dates according to locale conventions.
|
|
19
|
+
*
|
|
20
|
+
* @beta
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* "Today is {$date :date dateStyle=medium}"
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
const date = DraftFunctions.date;
|
|
28
|
+
/**
|
|
29
|
+
* Date and time formatting function for MessageFormat.
|
|
30
|
+
* Formats both date and time components.
|
|
31
|
+
*
|
|
32
|
+
* @beta
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* "Event at {$datetime :datetime dateStyle=short timeStyle=short}"
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
const datetime = DraftFunctions.datetime;
|
|
40
|
+
/**
|
|
41
|
+
* Percentage formatting function for MessageFormat.
|
|
42
|
+
* Formats numbers as percentages.
|
|
43
|
+
*
|
|
44
|
+
* @beta
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* "Progress: {$progress :percent}"
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
const percent = DraftFunctions.percent;
|
|
52
|
+
/**
|
|
53
|
+
* Number formatting function for MessageFormat.
|
|
54
|
+
* Formats numbers with locale-specific formatting.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* "Count: {$count :number}"
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
const number = DefaultFunctions.number;
|
|
62
|
+
/**
|
|
63
|
+
* Time formatting function for MessageFormat.
|
|
64
|
+
* Formats time values according to locale conventions.
|
|
65
|
+
*
|
|
66
|
+
* @beta
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* "Time: {$time :time timeStyle=short}"
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
const time = DraftFunctions.time;
|
|
74
|
+
/**
|
|
75
|
+
* Unit formatting function for MessageFormat.
|
|
76
|
+
* Formats numbers with units (length, weight, etc.).
|
|
77
|
+
*
|
|
78
|
+
* @beta
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* "Distance: {$distance :unit unit=mile}"
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
const unit = DraftFunctions.unit;
|
|
86
|
+
/**
|
|
87
|
+
* Integer formatting function for MessageFormat.
|
|
88
|
+
* Formats numbers as integers.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```ts
|
|
92
|
+
* "Items: {$count :integer}"
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
const integer = DefaultFunctions.integer;
|
|
96
|
+
/**
|
|
97
|
+
* Offset formatting function for MessageFormat.
|
|
98
|
+
* Formats timezone offsets.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* "Offset: {$offset :offset}"
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
const offset = DefaultFunctions.offset;
|
|
106
|
+
/**
|
|
107
|
+
* String formatting function for MessageFormat.
|
|
108
|
+
* Formats values as strings with optional transformations.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* "Name: {$name :string}"
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
const string = DefaultFunctions.string;
|
|
6
116
|
|
|
7
117
|
//#endregion
|
|
8
118
|
export { currency, date, datetime, integer, number, offset, percent, string, time, unit };
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { MessageFormatOptions, MessagePart, Model } from "messageformat";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Represents a nested structure of messages for different locales.
|
|
7
|
+
* Can be either a nested object with string keys or a flat object with locale keys.
|
|
8
|
+
*
|
|
9
|
+
* @template TLocales - The supported locale strings
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const messages: Messages<"en-US" | "nl-NL"> = {
|
|
14
|
+
* greeting: {
|
|
15
|
+
* "en-US": "Hello {$name}",
|
|
16
|
+
* "nl-NL": "Hallo {$name}"
|
|
17
|
+
* }
|
|
18
|
+
* };
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
type Messages<TLocales extends string> = {
|
|
22
|
+
[key: string]: Messages<TLocales>;
|
|
23
|
+
} | { [P in TLocales]: string };
|
|
24
|
+
/**
|
|
25
|
+
* Configuration object returned by defineConfig containing all message processing methods.
|
|
26
|
+
*
|
|
27
|
+
* @template TLocales - The supported locale strings
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* const config = defineConfig({
|
|
32
|
+
* defaultLocale: "en-US",
|
|
33
|
+
* locales: { "en-US": {}, "nl-NL": {} }
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* // Parse messages
|
|
37
|
+
* const parsed = config.parse(messages);
|
|
38
|
+
*
|
|
39
|
+
* // Format a message
|
|
40
|
+
* const formatted = config.format("en-US", parsed.greeting, { name: "John" });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
type MsgsConfig<TLocales extends string> = {
|
|
44
|
+
/** Parse message strings into MessageFormat objects */
|
|
45
|
+
parse: <T extends string = TLocales, const U extends Messages<T> = Messages<T>>(messages: U) => U;
|
|
46
|
+
/** Format a message to a string */
|
|
47
|
+
format: (locale: TLocales, msgs: Record<TLocales, Model.Message | string>, args?: Record<string, unknown>) => string;
|
|
48
|
+
/** Format a message to an array of MessagePart objects */
|
|
49
|
+
formatToParts: (locale: TLocales, msgs: Record<TLocales, Model.Message | string>, args?: Record<string, unknown>) => MessagePart<any>[];
|
|
50
|
+
/** Resolve the best matching locale from user preferences */
|
|
51
|
+
resolveLocale: (userLocales: string[] | readonly string[]) => TLocales;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Creates a message configuration object for internationalization.
|
|
55
|
+
*
|
|
56
|
+
* @template TLocales - The supported locale strings
|
|
57
|
+
* @template TDefaultLocale - The default locale (must be one of TLocales)
|
|
58
|
+
*
|
|
59
|
+
* @param config - Configuration object
|
|
60
|
+
* @param config.defaultLocale - The default locale to use as fallback
|
|
61
|
+
* @param config.locales - Locale-specific MessageFormat options
|
|
62
|
+
* @param config.options - Global MessageFormat options applied to all locales
|
|
63
|
+
*
|
|
64
|
+
* @returns A MsgsConfig object with parsing, formatting, and locale resolution methods
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* const msgsConfig = defineConfig({
|
|
69
|
+
* defaultLocale: "en-US",
|
|
70
|
+
* locales: {
|
|
71
|
+
* "en-US": { currency: "USD" },
|
|
72
|
+
* "nl-NL": { currency: "EUR" }
|
|
73
|
+
* },
|
|
74
|
+
* options: { date: { dateStyle: "medium" } }
|
|
75
|
+
* });
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
declare function defineConfig<TLocales extends string, TDefaultLocale extends TLocales>(config: {
|
|
79
|
+
defaultLocale: TDefaultLocale;
|
|
80
|
+
locales: Record<TLocales, MessageFormatOptions<any, any>>;
|
|
81
|
+
options?: MessageFormatOptions<any, any>;
|
|
82
|
+
}): MsgsConfig<TLocales>;
|
|
83
|
+
//#endregion
|
|
84
|
+
export { Messages, MsgsConfig, defineConfig };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { Messages, MsgsConfig, defineConfig } from "./index-DqWXabHs.js";
|
|
2
|
+
export { Messages, MsgsConfig, defineConfig };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
import { MessageFormat, parseMessage } from "messageformat";
|
|
2
2
|
|
|
3
3
|
//#region src/resolveLocale.ts
|
|
4
|
+
/**
|
|
5
|
+
* Resolves the best matching locale from user preferences against supported locales.
|
|
6
|
+
* Uses Intl locale matching with fallback chains for better compatibility.
|
|
7
|
+
*
|
|
8
|
+
* @param userLocales - User's preferred locales (e.g. navigator.languages)
|
|
9
|
+
* @param supportedLocales - The locales your app supports (e.g. ["en", "nl-NL", "fr-FR"])
|
|
10
|
+
* @param localeMatcher - Intl matching strategy (used only for runtime support check)
|
|
11
|
+
* @returns The best matching locale string
|
|
12
|
+
*
|
|
13
|
+
* @throws {Error} When userLocales is not a non-empty array
|
|
14
|
+
* @throws {Error} When supportedLocales is not a non-empty array
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const locale = resolveLocale(
|
|
19
|
+
* navigator.languages,
|
|
20
|
+
* ["en-US", "nl-NL", "fr-FR"],
|
|
21
|
+
* "best fit"
|
|
22
|
+
* );
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
4
25
|
function resolveLocale(userLocales, supportedLocales, localeMatcher = "best fit") {
|
|
5
26
|
if (!Array.isArray(userLocales) || userLocales.length === 0) throw new Error("userLocales must be a non-empty array");
|
|
6
27
|
if (!Array.isArray(supportedLocales) || supportedLocales.length === 0) throw new Error("supportedLocales must be a non-empty array");
|
|
@@ -38,6 +59,31 @@ function walkAndParse(node, target) {
|
|
|
38
59
|
} else if (typeof value === "string") target[key] = parseMessage(value);
|
|
39
60
|
}
|
|
40
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Creates a message configuration object for internationalization.
|
|
64
|
+
*
|
|
65
|
+
* @template TLocales - The supported locale strings
|
|
66
|
+
* @template TDefaultLocale - The default locale (must be one of TLocales)
|
|
67
|
+
*
|
|
68
|
+
* @param config - Configuration object
|
|
69
|
+
* @param config.defaultLocale - The default locale to use as fallback
|
|
70
|
+
* @param config.locales - Locale-specific MessageFormat options
|
|
71
|
+
* @param config.options - Global MessageFormat options applied to all locales
|
|
72
|
+
*
|
|
73
|
+
* @returns A MsgsConfig object with parsing, formatting, and locale resolution methods
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* const msgsConfig = defineConfig({
|
|
78
|
+
* defaultLocale: "en-US",
|
|
79
|
+
* locales: {
|
|
80
|
+
* "en-US": { currency: "USD" },
|
|
81
|
+
* "nl-NL": { currency: "EUR" }
|
|
82
|
+
* },
|
|
83
|
+
* options: { date: { dateStyle: "medium" } }
|
|
84
|
+
* });
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
41
87
|
function defineConfig(config) {
|
|
42
88
|
function parse(messages) {
|
|
43
89
|
const parsed = {};
|
package/dist/react.d.ts
CHANGED
|
@@ -1,29 +1,119 @@
|
|
|
1
|
-
import { MsgsConfig } from "./index-
|
|
1
|
+
import { MsgsConfig } from "./index-DqWXabHs.js";
|
|
2
2
|
import * as react_jsx_runtime0 from "react/jsx-runtime";
|
|
3
3
|
import { ReactNode } from "react";
|
|
4
4
|
|
|
5
5
|
//#region src/react/index.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Valid argument values for message formatting.
|
|
8
|
+
*/
|
|
6
9
|
type ArgValue = string | number | boolean | null | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Translator object returned by useTranslator hook.
|
|
12
|
+
* Provides methods for formatting messages as strings or JSX.
|
|
13
|
+
*/
|
|
7
14
|
type Translator = {
|
|
15
|
+
/** Format a message to a string */
|
|
8
16
|
(msg: any, args?: Record<string, ArgValue>): string;
|
|
17
|
+
/** Format a message to JSX elements, used for messages with markup */
|
|
9
18
|
jsx(msg: any, args?: Record<string, ArgValue>): ReactNode;
|
|
19
|
+
/** Current locale string */
|
|
10
20
|
locale: string;
|
|
11
21
|
};
|
|
22
|
+
/**
|
|
23
|
+
* React hook for accessing the message translator.
|
|
24
|
+
* Must be used within a MsgsProvider context.
|
|
25
|
+
*
|
|
26
|
+
* @returns A translator object with format and jsx methods
|
|
27
|
+
*
|
|
28
|
+
* @throws {Error} When used outside of MsgsProvider context
|
|
29
|
+
* @throws {Error} When locale is not set by either MsgsProvider or LocaleProvider
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```tsx
|
|
33
|
+
* function MyComponent() {
|
|
34
|
+
* const t = useTranslator();
|
|
35
|
+
*
|
|
36
|
+
* return (
|
|
37
|
+
* <div>
|
|
38
|
+
* <h1>{t(msgs.greeting, { name: "John" })}</h1>
|
|
39
|
+
* <p>{t.jsx(msgs.description, { count: 5 })}</p>
|
|
40
|
+
* </div>
|
|
41
|
+
* );
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
12
45
|
declare function useTranslator(): Translator;
|
|
46
|
+
/**
|
|
47
|
+
* Props for the MsgsProvider component.
|
|
48
|
+
*
|
|
49
|
+
* @template TLocales - The supported locale strings
|
|
50
|
+
*/
|
|
13
51
|
interface MsgsProviderProps<TLocales extends string> {
|
|
52
|
+
/** The message configuration object */
|
|
14
53
|
config: MsgsConfig<TLocales>;
|
|
54
|
+
/** The current locale */
|
|
15
55
|
locale: TLocales;
|
|
56
|
+
/** Child components */
|
|
16
57
|
children: ReactNode;
|
|
17
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* React provider component that makes message configuration available to child components.
|
|
61
|
+
*
|
|
62
|
+
* @template TLocales - The supported locale strings
|
|
63
|
+
*
|
|
64
|
+
* @param props - Component props
|
|
65
|
+
* @param props.config - The message configuration object
|
|
66
|
+
* @param props.locale - The current locale
|
|
67
|
+
* @param props.children - Child components
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```tsx
|
|
71
|
+
* function App() {
|
|
72
|
+
* return (
|
|
73
|
+
* <MsgsProvider config={msgsConfig} locale="en-US">
|
|
74
|
+
* <MyComponent />
|
|
75
|
+
* </MsgsProvider>
|
|
76
|
+
* );
|
|
77
|
+
* }
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
18
80
|
declare function MsgsProvider<TLocales extends string>({
|
|
19
81
|
config,
|
|
20
82
|
locale,
|
|
21
83
|
children
|
|
22
84
|
}: MsgsProviderProps<TLocales>): react_jsx_runtime0.JSX.Element;
|
|
85
|
+
/**
|
|
86
|
+
* Props for the LocaleProvider component.
|
|
87
|
+
*
|
|
88
|
+
* @template TLocales - The supported locale strings
|
|
89
|
+
*/
|
|
23
90
|
interface LocaleProviderProps<TLocales extends string> {
|
|
91
|
+
/** The locale to set */
|
|
24
92
|
locale: TLocales;
|
|
93
|
+
/** Child components */
|
|
25
94
|
children: ReactNode;
|
|
26
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* React provider component that sets the locale for child components.
|
|
98
|
+
* Can be used to override the locale set by MsgsProvider.
|
|
99
|
+
*
|
|
100
|
+
* @template TLocales - The supported locale strings
|
|
101
|
+
*
|
|
102
|
+
* @param props - Component props
|
|
103
|
+
* @param props.locale - The locale to set
|
|
104
|
+
* @param props.children - Child components
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```tsx
|
|
108
|
+
* function LocalizedSection() {
|
|
109
|
+
* return (
|
|
110
|
+
* <LocaleProvider locale="nl-NL">
|
|
111
|
+
* <MyComponent />
|
|
112
|
+
* </LocaleProvider>
|
|
113
|
+
* );
|
|
114
|
+
* }
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
27
117
|
declare function LocaleProvider<TLocales extends string>({
|
|
28
118
|
locale,
|
|
29
119
|
children
|
package/dist/react.js
CHANGED
|
@@ -1324,6 +1324,20 @@ var import_jsx_runtime = /* @__PURE__ */ __toESM(require_jsx_runtime());
|
|
|
1324
1324
|
function createKey(part, i) {
|
|
1325
1325
|
return `${part.type}-${part.value}-${part.locale}-${part.kind}-${part.name}-${part.source}-${i}`;
|
|
1326
1326
|
}
|
|
1327
|
+
/**
|
|
1328
|
+
* Converts an array of MessagePart objects to React JSX elements.
|
|
1329
|
+
* Handles markup parts by converting them to React components and text parts to fragments.
|
|
1330
|
+
*
|
|
1331
|
+
* @param parts - Array of MessagePart objects to convert
|
|
1332
|
+
* @param args - Optional arguments that can be used to override markup components
|
|
1333
|
+
* @returns Array of React nodes
|
|
1334
|
+
*
|
|
1335
|
+
* @example
|
|
1336
|
+
* ```tsx
|
|
1337
|
+
* const parts = config.formatToParts(locale, message, args);
|
|
1338
|
+
* const jsxElements = partsToJSX(parts, { strong: 'b' });
|
|
1339
|
+
* ```
|
|
1340
|
+
*/
|
|
1327
1341
|
function partsToJSX(parts, args, start = 0, stop) {
|
|
1328
1342
|
const result = [];
|
|
1329
1343
|
let i = start;
|
|
@@ -1370,6 +1384,29 @@ function partsToJSX(parts, args, start = 0, stop) {
|
|
|
1370
1384
|
//#region src/react/index.tsx
|
|
1371
1385
|
const localeContext = (0, import_react.createContext)(null);
|
|
1372
1386
|
const configContext = (0, import_react.createContext)(null);
|
|
1387
|
+
/**
|
|
1388
|
+
* React hook for accessing the message translator.
|
|
1389
|
+
* Must be used within a MsgsProvider context.
|
|
1390
|
+
*
|
|
1391
|
+
* @returns A translator object with format and jsx methods
|
|
1392
|
+
*
|
|
1393
|
+
* @throws {Error} When used outside of MsgsProvider context
|
|
1394
|
+
* @throws {Error} When locale is not set by either MsgsProvider or LocaleProvider
|
|
1395
|
+
*
|
|
1396
|
+
* @example
|
|
1397
|
+
* ```tsx
|
|
1398
|
+
* function MyComponent() {
|
|
1399
|
+
* const t = useTranslator();
|
|
1400
|
+
*
|
|
1401
|
+
* return (
|
|
1402
|
+
* <div>
|
|
1403
|
+
* <h1>{t(msgs.greeting, { name: "John" })}</h1>
|
|
1404
|
+
* <p>{t.jsx(msgs.description, { count: 5 })}</p>
|
|
1405
|
+
* </div>
|
|
1406
|
+
* );
|
|
1407
|
+
* }
|
|
1408
|
+
* ```
|
|
1409
|
+
*/
|
|
1373
1410
|
function useTranslator() {
|
|
1374
1411
|
const config = (0, import_react.useContext)(configContext);
|
|
1375
1412
|
const locale = (0, import_react.useContext)(localeContext);
|
|
@@ -1385,6 +1422,27 @@ function useTranslator() {
|
|
|
1385
1422
|
translator.locale = locale;
|
|
1386
1423
|
return translator;
|
|
1387
1424
|
}
|
|
1425
|
+
/**
|
|
1426
|
+
* React provider component that makes message configuration available to child components.
|
|
1427
|
+
*
|
|
1428
|
+
* @template TLocales - The supported locale strings
|
|
1429
|
+
*
|
|
1430
|
+
* @param props - Component props
|
|
1431
|
+
* @param props.config - The message configuration object
|
|
1432
|
+
* @param props.locale - The current locale
|
|
1433
|
+
* @param props.children - Child components
|
|
1434
|
+
*
|
|
1435
|
+
* @example
|
|
1436
|
+
* ```tsx
|
|
1437
|
+
* function App() {
|
|
1438
|
+
* return (
|
|
1439
|
+
* <MsgsProvider config={msgsConfig} locale="en-US">
|
|
1440
|
+
* <MyComponent />
|
|
1441
|
+
* </MsgsProvider>
|
|
1442
|
+
* );
|
|
1443
|
+
* }
|
|
1444
|
+
* ```
|
|
1445
|
+
*/
|
|
1388
1446
|
function MsgsProvider({ config, locale, children }) {
|
|
1389
1447
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(configContext.Provider, {
|
|
1390
1448
|
value: config,
|
|
@@ -1394,6 +1452,27 @@ function MsgsProvider({ config, locale, children }) {
|
|
|
1394
1452
|
})
|
|
1395
1453
|
});
|
|
1396
1454
|
}
|
|
1455
|
+
/**
|
|
1456
|
+
* React provider component that sets the locale for child components.
|
|
1457
|
+
* Can be used to override the locale set by MsgsProvider.
|
|
1458
|
+
*
|
|
1459
|
+
* @template TLocales - The supported locale strings
|
|
1460
|
+
*
|
|
1461
|
+
* @param props - Component props
|
|
1462
|
+
* @param props.locale - The locale to set
|
|
1463
|
+
* @param props.children - Child components
|
|
1464
|
+
*
|
|
1465
|
+
* @example
|
|
1466
|
+
* ```tsx
|
|
1467
|
+
* function LocalizedSection() {
|
|
1468
|
+
* return (
|
|
1469
|
+
* <LocaleProvider locale="nl-NL">
|
|
1470
|
+
* <MyComponent />
|
|
1471
|
+
* </LocaleProvider>
|
|
1472
|
+
* );
|
|
1473
|
+
* }
|
|
1474
|
+
* ```
|
|
1475
|
+
*/
|
|
1397
1476
|
function LocaleProvider({ locale, children }) {
|
|
1398
1477
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(localeContext.Provider, {
|
|
1399
1478
|
value: locale,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@havelaer/msgs",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -8,20 +8,29 @@
|
|
|
8
8
|
"import": "./dist/index.js"
|
|
9
9
|
},
|
|
10
10
|
"./react": {
|
|
11
|
-
"types": "./dist/react
|
|
12
|
-
"import": "./dist/react
|
|
11
|
+
"types": "./dist/react.d.ts",
|
|
12
|
+
"import": "./dist/react.js"
|
|
13
13
|
},
|
|
14
14
|
"./functions": {
|
|
15
|
-
"types": "./dist/functions
|
|
16
|
-
"import": "./dist/functions
|
|
15
|
+
"types": "./dist/functions.d.ts",
|
|
16
|
+
"import": "./dist/functions.js"
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
19
|
"publishConfig": {
|
|
20
20
|
"access": "public"
|
|
21
21
|
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"messageformat",
|
|
24
|
+
"MessageFormat2",
|
|
25
|
+
"MF2",
|
|
26
|
+
"l10n",
|
|
27
|
+
"localization",
|
|
28
|
+
"react",
|
|
29
|
+
"typescript"
|
|
30
|
+
],
|
|
22
31
|
"scripts": {
|
|
23
32
|
"check": "biome check .",
|
|
24
|
-
"prepublishOnly": "npm run check
|
|
33
|
+
"prepublishOnly": "npm run check && npm run build && npm version patch -m 'chore: publishing version %s'",
|
|
25
34
|
"build": "tsdown"
|
|
26
35
|
},
|
|
27
36
|
"files": [
|
package/dist/index-CrRmYrrh.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { MessageFormatOptions, MessagePart, Model } from "messageformat";
|
|
2
|
-
|
|
3
|
-
//#region src/index.d.ts
|
|
4
|
-
type Messages<TLocales extends string> = {
|
|
5
|
-
[key: string]: Messages<TLocales>;
|
|
6
|
-
} | { [P in TLocales]: string };
|
|
7
|
-
type Config = {
|
|
8
|
-
messageFormatOptions?: MessageFormatOptions<any, any>;
|
|
9
|
-
};
|
|
10
|
-
type MsgsConfig<TLocales extends string> = {
|
|
11
|
-
parse: <T extends string = TLocales, const U extends Messages<T> = Messages<T>>(messages: U) => U;
|
|
12
|
-
format: (locale: TLocales, msgs: Record<TLocales, Model.Message | string>, args?: Record<string, unknown>) => string;
|
|
13
|
-
formatToParts: (locale: TLocales, msgs: Record<TLocales, Model.Message | string>, args?: Record<string, unknown>) => MessagePart<any>[];
|
|
14
|
-
resolveLocale: (userLocales: string[] | readonly string[]) => TLocales;
|
|
15
|
-
};
|
|
16
|
-
declare function defineConfig<TLocales extends string, TDefaultLocale extends TLocales>(config: {
|
|
17
|
-
defaultLocale: TDefaultLocale;
|
|
18
|
-
locales: Record<TLocales, MessageFormatOptions<any, any>>;
|
|
19
|
-
options?: MessageFormatOptions<any, any>;
|
|
20
|
-
}): MsgsConfig<TLocales>;
|
|
21
|
-
//#endregion
|
|
22
|
-
export { Config, Messages, MsgsConfig, defineConfig };
|