@havelaer/msgs 0.0.12 → 0.0.14
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/index.d.ts +87 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.js +1 -2
- package/package.json +14 -14
- package/dist/index-CFo6Y2QT.d.ts +0 -88
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,88 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MessageFormatOptions, MessagePart, Model } from "messageformat";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Valid argument values for message formatting.
|
|
7
|
+
*/
|
|
8
|
+
type ArgValue<T = never> = string | number | boolean | T;
|
|
9
|
+
/**
|
|
10
|
+
* Represents a nested structure of messages for different locales.
|
|
11
|
+
* Can be either a nested object with string keys or a flat object with locale keys.
|
|
12
|
+
*
|
|
13
|
+
* @template TLocales - The supported locale strings
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const messages: Messages<"en-US" | "nl-NL"> = {
|
|
18
|
+
* greeting: {
|
|
19
|
+
* "en-US": "Hello {$name}",
|
|
20
|
+
* "nl-NL": "Hallo {$name}"
|
|
21
|
+
* }
|
|
22
|
+
* };
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
type Messages<TLocales extends string> = {
|
|
26
|
+
[key: string]: Messages<TLocales>;
|
|
27
|
+
} | { [P in TLocales]: string };
|
|
28
|
+
/**
|
|
29
|
+
* Configuration object returned by defineConfig containing all message processing methods.
|
|
30
|
+
*
|
|
31
|
+
* @template TLocales - The supported locale strings
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const config = defineConfig({
|
|
36
|
+
* defaultLocale: "en-US",
|
|
37
|
+
* locales: { "en-US": {}, "nl-NL": {} }
|
|
38
|
+
* });
|
|
39
|
+
*
|
|
40
|
+
* // Parse messages
|
|
41
|
+
* const parsed = config.parse(messages);
|
|
42
|
+
*
|
|
43
|
+
* // Format a message
|
|
44
|
+
* const formatted = config.format("en-US", parsed.greeting, { name: "John" });
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
type Formatter<TLocales extends string> = {
|
|
48
|
+
/** Parse message strings into MessageFormat objects */
|
|
49
|
+
parse: <T extends string = TLocales, const U extends Messages<T> = Messages<T>>(messages: U) => U;
|
|
50
|
+
/** Format a message to a string */
|
|
51
|
+
format: (locale: TLocales, msgs: Record<TLocales, Model.Message | string>, args?: Record<string, unknown>) => string;
|
|
52
|
+
/** Format a message to an array of MessagePart objects */
|
|
53
|
+
formatToParts: (locale: TLocales, msgs: Record<TLocales, Model.Message | string>, args?: Record<string, unknown>) => MessagePart<any>[];
|
|
54
|
+
/** Resolve the best matching locale from user preferences */
|
|
55
|
+
resolveLocale: (userLocales: string[] | readonly string[]) => TLocales;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Creates a message formatter object for internationalization.
|
|
59
|
+
*
|
|
60
|
+
* @template TLocales - The supported locale strings
|
|
61
|
+
* @template TDefaultLocale - The default locale (must be one of TLocales)
|
|
62
|
+
*
|
|
63
|
+
* @param config - Configuration object
|
|
64
|
+
* @param config.defaultLocale - The default locale to use as fallback
|
|
65
|
+
* @param config.locales - Locale-specific MessageFormat options
|
|
66
|
+
* @param config.options - Global MessageFormat options applied to all locales
|
|
67
|
+
*
|
|
68
|
+
* @returns A Formatter object with parsing, formatting, and locale resolution methods
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* const formatter = createFormatter({
|
|
73
|
+
* defaultLocale: "en-US",
|
|
74
|
+
* locales: {
|
|
75
|
+
* "en-US": { --locale specific MessageFormatOptions-- },
|
|
76
|
+
* "nl-NL": { --locale specific MessageFormatOptions-- }
|
|
77
|
+
* },
|
|
78
|
+
* options: { --global MessageFormatOptions-- }
|
|
79
|
+
* });
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
declare function createFormatter<TLocales extends string, TDefaultLocale extends TLocales>(config: {
|
|
83
|
+
defaultLocale: TDefaultLocale;
|
|
84
|
+
locales: Record<TLocales, MessageFormatOptions<any, any>>;
|
|
85
|
+
options?: MessageFormatOptions<any, any>;
|
|
86
|
+
}): Formatter<TLocales>;
|
|
87
|
+
//#endregion
|
|
2
88
|
export { ArgValue, Formatter, Messages, createFormatter };
|
package/dist/react.d.ts
CHANGED
package/dist/react.js
CHANGED
|
@@ -137,8 +137,7 @@ function useTranslator() {
|
|
|
137
137
|
return config.format(locale, msg, args);
|
|
138
138
|
};
|
|
139
139
|
translator.jsx = function translateJsx(msg, args) {
|
|
140
|
-
|
|
141
|
-
return /* @__PURE__ */ jsx(Fragment$1, { children: partsToJSX(parts, args) });
|
|
140
|
+
return /* @__PURE__ */ jsx(Fragment$1, { children: partsToJSX(config.formatToParts(locale, msg, args), args) });
|
|
142
141
|
};
|
|
143
142
|
translator.locale = locale;
|
|
144
143
|
return translator;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@havelaer/msgs",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -46,25 +46,25 @@
|
|
|
46
46
|
"react-dom": ">=18.0.0"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@biomejs/biome": "^2.
|
|
50
|
-
"@testing-library/jest-dom": "^6.
|
|
49
|
+
"@biomejs/biome": "^2.3.8",
|
|
50
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
51
51
|
"@testing-library/react": "^16.3.0",
|
|
52
|
-
"@types/node": "^
|
|
53
|
-
"@types/react": "^19.
|
|
54
|
-
"@types/react-dom": "^19.
|
|
55
|
-
"@vitest/coverage-v8": "^
|
|
56
|
-
"happy-dom": "^
|
|
57
|
-
"react": "^19.
|
|
58
|
-
"react-dom": "^19.
|
|
59
|
-
"tsdown": "^0.
|
|
60
|
-
"typescript": "~5.9.
|
|
61
|
-
"vitest": "^
|
|
52
|
+
"@types/node": "^25.0.2",
|
|
53
|
+
"@types/react": "^19.2.7",
|
|
54
|
+
"@types/react-dom": "^19.2.3",
|
|
55
|
+
"@vitest/coverage-v8": "^4.0.14",
|
|
56
|
+
"happy-dom": "^20.0.10",
|
|
57
|
+
"react": "^19.2.0",
|
|
58
|
+
"react-dom": "^19.2.0",
|
|
59
|
+
"tsdown": "^0.18.2",
|
|
60
|
+
"typescript": "~5.9.3",
|
|
61
|
+
"vitest": "^4.0.14"
|
|
62
62
|
},
|
|
63
63
|
"workspaces": [
|
|
64
64
|
".",
|
|
65
65
|
"playground"
|
|
66
66
|
],
|
|
67
67
|
"dependencies": {
|
|
68
|
-
"messageformat": "4.0.0
|
|
68
|
+
"messageformat": "4.0.0"
|
|
69
69
|
}
|
|
70
70
|
}
|
package/dist/index-CFo6Y2QT.d.ts
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { MessageFormatOptions, MessagePart, Model } from "messageformat";
|
|
2
|
-
|
|
3
|
-
//#region src/index.d.ts
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Valid argument values for message formatting.
|
|
7
|
-
*/
|
|
8
|
-
type ArgValue<T = never> = string | number | boolean | T;
|
|
9
|
-
/**
|
|
10
|
-
* Represents a nested structure of messages for different locales.
|
|
11
|
-
* Can be either a nested object with string keys or a flat object with locale keys.
|
|
12
|
-
*
|
|
13
|
-
* @template TLocales - The supported locale strings
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```ts
|
|
17
|
-
* const messages: Messages<"en-US" | "nl-NL"> = {
|
|
18
|
-
* greeting: {
|
|
19
|
-
* "en-US": "Hello {$name}",
|
|
20
|
-
* "nl-NL": "Hallo {$name}"
|
|
21
|
-
* }
|
|
22
|
-
* };
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
type Messages<TLocales extends string> = {
|
|
26
|
-
[key: string]: Messages<TLocales>;
|
|
27
|
-
} | { [P in TLocales]: string };
|
|
28
|
-
/**
|
|
29
|
-
* Configuration object returned by defineConfig containing all message processing methods.
|
|
30
|
-
*
|
|
31
|
-
* @template TLocales - The supported locale strings
|
|
32
|
-
*
|
|
33
|
-
* @example
|
|
34
|
-
* ```ts
|
|
35
|
-
* const config = defineConfig({
|
|
36
|
-
* defaultLocale: "en-US",
|
|
37
|
-
* locales: { "en-US": {}, "nl-NL": {} }
|
|
38
|
-
* });
|
|
39
|
-
*
|
|
40
|
-
* // Parse messages
|
|
41
|
-
* const parsed = config.parse(messages);
|
|
42
|
-
*
|
|
43
|
-
* // Format a message
|
|
44
|
-
* const formatted = config.format("en-US", parsed.greeting, { name: "John" });
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
type Formatter<TLocales extends string> = {
|
|
48
|
-
/** Parse message strings into MessageFormat objects */
|
|
49
|
-
parse: <T extends string = TLocales, const U extends Messages<T> = Messages<T>>(messages: U) => U;
|
|
50
|
-
/** Format a message to a string */
|
|
51
|
-
format: (locale: TLocales, msgs: Record<TLocales, Model.Message | string>, args?: Record<string, unknown>) => string;
|
|
52
|
-
/** Format a message to an array of MessagePart objects */
|
|
53
|
-
formatToParts: (locale: TLocales, msgs: Record<TLocales, Model.Message | string>, args?: Record<string, unknown>) => MessagePart<any>[];
|
|
54
|
-
/** Resolve the best matching locale from user preferences */
|
|
55
|
-
resolveLocale: (userLocales: string[] | readonly string[]) => TLocales;
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* Creates a message formatter object for internationalization.
|
|
59
|
-
*
|
|
60
|
-
* @template TLocales - The supported locale strings
|
|
61
|
-
* @template TDefaultLocale - The default locale (must be one of TLocales)
|
|
62
|
-
*
|
|
63
|
-
* @param config - Configuration object
|
|
64
|
-
* @param config.defaultLocale - The default locale to use as fallback
|
|
65
|
-
* @param config.locales - Locale-specific MessageFormat options
|
|
66
|
-
* @param config.options - Global MessageFormat options applied to all locales
|
|
67
|
-
*
|
|
68
|
-
* @returns A Formatter object with parsing, formatting, and locale resolution methods
|
|
69
|
-
*
|
|
70
|
-
* @example
|
|
71
|
-
* ```ts
|
|
72
|
-
* const formatter = createFormatter({
|
|
73
|
-
* defaultLocale: "en-US",
|
|
74
|
-
* locales: {
|
|
75
|
-
* "en-US": { --locale specific MessageFormatOptions-- },
|
|
76
|
-
* "nl-NL": { --locale specific MessageFormatOptions-- }
|
|
77
|
-
* },
|
|
78
|
-
* options: { --global MessageFormatOptions-- }
|
|
79
|
-
* });
|
|
80
|
-
* ```
|
|
81
|
-
*/
|
|
82
|
-
declare function createFormatter<TLocales extends string, TDefaultLocale extends TLocales>(config: {
|
|
83
|
-
defaultLocale: TDefaultLocale;
|
|
84
|
-
locales: Record<TLocales, MessageFormatOptions<any, any>>;
|
|
85
|
-
options?: MessageFormatOptions<any, any>;
|
|
86
|
-
}): Formatter<TLocales>;
|
|
87
|
-
//#endregion
|
|
88
|
-
export { ArgValue, Formatter, Messages, createFormatter };
|