Skip to content

Commit

Permalink
Add onError argument
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Feb 4, 2022
1 parent 7199274 commit 91c1669
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ for the creation of `MessageFormat` instances.
The `ResolvedOptions` object contains the options
resolved during the construction of the `MessageFormat` instance.

The `msgPath` argument identifies the message from those available in the current resources.
If all added resources share the same `id` value,
the path may be given as a string or a string array.
The `scope` argument is used to lookup variable references used in the `Message`.

```ts
interface MessageFormatOptions {
localeMatcher?: 'best fit' | 'lookup';
Expand All @@ -96,9 +91,17 @@ interface Intl.MessageFormat {

addResources(...resources: Resource[]);

format(msgPath: MsgPath, scope?: Scope): string;
format(
msgPath: MsgPath,
scope?: Scope | null,
onError?: (error: Error) => void
): string;

getMessage(msgPath: MsgPath, scope?: Scope): ResolvedMessage | undefined;
getMessage(
msgPath: MsgPath,
scope?: Scope | null,
onError?: (error: Error) => void
): ResolvedMessage | undefined;

resolvedOptions(): ResolvedOptions;
}
Expand All @@ -107,6 +110,16 @@ interface Intl.MessageFormat {
For formatting a message, two methods are provided: `format()` and `getMessage()`.
The first of these will always return a simple string,
while the latter returns a `ResolvedMessage` object or `undefined` if the message was not found.
These methods have the following arguments:

- `msgPath` identifies the message from those available in the current resources.
If all added resources share the same `id` value,
the path may be given as a string or a string array.
- `scope` is used to lookup variable references used in the `Message`.
- `onError` argument defines an error handler that will be called if
message resolution or formatting fails.
If `onError` is not defined,
errors will be ignored and a fallback representation used for the corresponding message part.

`ResolvedMessage` is intended to provide a building block for the localization of messages
in contexts where its representation as a plain string would not be sufficient.
Expand Down

0 comments on commit 91c1669

Please sign in to comment.