@dws-std/i18n 1.1.0 → 1.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/README.md CHANGED
@@ -56,7 +56,7 @@ const unauthorized = entry({
56
56
  }
57
57
  });
58
58
 
59
- // This will produce a plain ResolvedMessage when used in a message catalog
59
+ // This will produce a plain LocalizedMessage when used in a message catalog
60
60
  const welcome = entry<{ name: string }>({
61
61
  translations: {
62
62
  en: 'Welcome, {{name}}!',
@@ -124,7 +124,7 @@ const msg = DNS_MESSAGES.recordCreated();
124
124
 
125
125
  ### Resolving to a specific locale
126
126
 
127
- `resolveMessage` takes a `LocalizedHttpException` or a `ResolvedMessage` and returns the interpolated string for the locale you want.
127
+ `resolveMessage` takes a `LocalizedHttpException` or a `LocalizedMessage` and returns the interpolated string for the locale you want.
128
128
 
129
129
  ```ts
130
130
  import { resolveMessage } from '@dws-std/i18n';
@@ -16,8 +16,8 @@ export interface LocalizedHttpExceptionOptions<TCause = unknown> extends HttpExc
16
16
  /**
17
17
  * HTTP exception that carries translated messages.
18
18
  *
19
- * The `message` property is automatically resolved to the default locale.
20
- * Use {@link resolveMessage} to get a translation in a different locale.
19
+ * The `message` property contains the raw template for the default locale.
20
+ * Use {@link resolveMessage} to get the interpolated string for any locale.
21
21
  *
22
22
  * @template TCause - Type of the underlying cause.
23
23
  */
package/dist/index.d.ts CHANGED
@@ -2,11 +2,11 @@ export { entry } from './entry';
2
2
  export { defineExceptionCatalog } from './exception/define-exception-catalog';
3
3
  export type { DefineExceptionCatalogOptions, ExceptionCatalog } from './exception/define-exception-catalog';
4
4
  export { LocalizedHttpException } from './exception/localized-http-exception';
5
- export type { LocalizedHttpExceptionOptions as LocalizedHttpExceptionInit } from './exception/localized-http-exception';
5
+ export type { LocalizedHttpExceptionOptions } from './exception/localized-http-exception';
6
6
  export type { ExceptionEntry } from './exception/type/exception-entry';
7
7
  export { defineMessageCatalog } from './message/define-message-catalog';
8
8
  export type { DefineMessageCatalogOptions, MessageCatalog } from './message/define-message-catalog';
9
+ export type { LocalizedMessage } from './message/type/localized-message';
9
10
  export type { MessageEntry } from './message/type/message-entry';
10
- export type { ResolvedMessage } from './message/type/resolved-message';
11
11
  export { resolveMessage } from './resolve-message';
12
12
  export type { Translations } from './type/translations';
package/dist/index.js CHANGED
@@ -6,21 +6,12 @@ function entry(definition) {
6
6
  // src/exception/localized-http-exception.ts
7
7
  import { HttpException } from "@dws-std/error";
8
8
 
9
- // src/resolve-message.ts
10
- var _interpolate = (template, params) => template.replace(/\{\{(\w+)\}\}/g, (_, key) => params[key] ?? `{{${key}}}`);
11
- var resolveMessage = (target, locale) => target.params ? _interpolate(target.translations[locale ?? target.defaultLocale] ?? "", target.params) : target.translations[locale ?? target.defaultLocale] ?? "";
12
-
13
- // src/exception/localized-http-exception.ts
14
9
  class LocalizedHttpException extends HttpException {
15
10
  translations;
16
11
  params;
17
12
  defaultLocale;
18
13
  constructor(key, init) {
19
- super(resolveMessage({
20
- translations: init.translations,
21
- params: init.params,
22
- defaultLocale: init.defaultLocale
23
- }), {
14
+ super(init.translations[init.defaultLocale] ?? "", {
24
15
  cause: init.cause,
25
16
  status: init.status,
26
17
  key
@@ -59,6 +50,9 @@ var defineMessageCatalog = (options) => {
59
50
  }
60
51
  return catalog;
61
52
  };
53
+ // src/resolve-message.ts
54
+ var _interpolate = (template, params) => template.replace(/\{\{(\w+)\}\}/g, (_, key) => params[key] ?? `{{${key}}}`);
55
+ var resolveMessage = (target, locale) => target.params ? _interpolate(target.translations[locale ?? target.defaultLocale] ?? "", target.params) : target.translations[locale ?? target.defaultLocale] ?? "";
62
56
  export {
63
57
  resolveMessage,
64
58
  entry,
@@ -1,7 +1,7 @@
1
1
  import type { MessageEntry } from './type/message-entry';
2
- import type { ResolvedMessage } from './type/resolved-message';
2
+ import type { LocalizedMessage } from './type/localized-message';
3
3
  export type MessageCatalog<TDefs extends Record<string, MessageEntry<any>>> = {
4
- readonly [K in keyof TDefs]: TDefs[K] extends MessageEntry<infer P> ? [P] extends [Record<string, never>] ? () => ResolvedMessage : (params: P) => ResolvedMessage : never;
4
+ readonly [K in keyof TDefs]: TDefs[K] extends MessageEntry<infer P> ? [P] extends [Record<string, never>] ? () => LocalizedMessage : (params: P) => LocalizedMessage : never;
5
5
  };
6
6
  /**
7
7
  * Configuration for {@link defineMessageCatalog}.
@@ -18,7 +18,7 @@ export interface DefineMessageCatalogOptions<TDefs extends Record<string, Messag
18
18
  * Builds a typed message catalog from a set of {@link MessageEntry} definitions.
19
19
  *
20
20
  * Each key in `definitions` becomes a factory function that creates
21
- * a {@link ResolvedMessage} pre-filled with the right translations and default locale.
21
+ * a {@link LocalizedMessage} pre-filled with the right translations and default locale.
22
22
  *
23
23
  * @param options - Default locale and message definitions.
24
24
  *
@@ -1,11 +1,11 @@
1
1
  import type { Translations } from '../../type/translations';
2
2
  /**
3
- * Fully resolved message ready to be rendered.
3
+ * Localized message carrying translations and interpolation parameters.
4
4
  *
5
5
  * Returned by the factory functions generated by `defineMessageCatalog`.
6
6
  * Pass it to {@link resolveMessage} to get the final string for a given locale.
7
7
  */
8
- export interface ResolvedMessage {
8
+ export interface LocalizedMessage {
9
9
  /** All available translations keyed by locale. */
10
10
  readonly translations: Translations;
11
11
  /** Parameter values to interpolate into `{{placeholder}}` tokens. */
@@ -1,7 +1,7 @@
1
1
  import type { LocalizedHttpException } from './exception/localized-http-exception';
2
- import type { ResolvedMessage } from './message/type/resolved-message';
2
+ import type { LocalizedMessage } from './message/type/localized-message';
3
3
  /**
4
- * Turns a {@link ResolvedMessage} or {@link LocalizedHttpException} into
4
+ * Turns a {@link LocalizedMessage} or {@link LocalizedHttpException} into
5
5
  * a plain string for the requested locale.
6
6
  *
7
7
  * `{{placeholder}}` tokens are replaced with matching values from `target.params`.
@@ -13,4 +13,4 @@ import type { ResolvedMessage } from './message/type/resolved-message';
13
13
  *
14
14
  * @returns Translated string with placeholders interpolated.
15
15
  */
16
- export declare const resolveMessage: (target: LocalizedHttpException | ResolvedMessage, locale?: string) => string;
16
+ export declare const resolveMessage: (target: LocalizedHttpException | LocalizedMessage, locale?: string) => string;
package/package.json CHANGED
@@ -1,17 +1,16 @@
1
1
  {
2
2
  "name": "@dws-std/i18n",
3
- "version": "1.1.0",
4
- "description": "Type-safe i18n for TypeScript define localized exception and message catalogs with compile-time validated parameters.",
3
+ "version": "1.2.1",
4
+ "description": "Type-safe i18n for TypeScript, define localized exception and message catalogs with compile-time validated parameters.",
5
5
  "keywords": [
6
6
  "bun",
7
7
  "dws",
8
- "exception",
9
- "http-error",
10
8
  "i18n",
11
9
  "internationalization",
12
- "l10n",
13
10
  "localization",
14
- "translation"
11
+ "open-source",
12
+ "translation",
13
+ "typescript"
15
14
  ],
16
15
  "license": "MIT",
17
16
  "author": "Dominus Web Services (DWS)",
@@ -41,16 +40,16 @@
41
40
  "test": "bun test --pass-with-no-tests --coverage"
42
41
  },
43
42
  "dependencies": {
44
- "@dws-std/error": "^2.1.0"
43
+ "@dws-std/error": "^2.1.1"
45
44
  },
46
45
  "devDependencies": {
47
- "@types/bun": "^1.3.10",
48
- "oxfmt": "0.41.0",
49
- "oxlint": "1.56.0",
50
- "oxlint-tsgolint": "0.17.0",
51
- "typescript": "^5.9.3"
46
+ "@types/bun": "^1.3.11",
47
+ "oxfmt": "0.44.0",
48
+ "oxlint": "1.59.0",
49
+ "oxlint-tsgolint": "0.20.0",
50
+ "typescript": "^6.0.2"
52
51
  },
53
52
  "peerDependencies": {
54
- "@dws-std/error": "^2.1.0"
53
+ "@dws-std/error": "^2.1.1"
55
54
  }
56
55
  }