@atscript/ui 0.1.169 → 0.1.170

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.
Files changed (45) hide show
  1. package/dist/form/create-form-def.cjs +8 -2
  2. package/dist/form/create-form-def.mjs +8 -2
  3. package/dist/form/labels.cjs +4 -4
  4. package/dist/form/labels.d.cts +3 -2
  5. package/dist/form/labels.d.mts +3 -2
  6. package/dist/form/labels.mjs +4 -4
  7. package/dist/form/types.d.cts +12 -0
  8. package/dist/form/types.d.mts +12 -0
  9. package/dist/form/validate.cjs +18 -4
  10. package/dist/form/validate.d.cts +11 -1
  11. package/dist/form/validate.d.mts +11 -1
  12. package/dist/form/validate.mjs +18 -5
  13. package/dist/i18n/model-texts.cjs +50 -0
  14. package/dist/i18n/model-texts.d.cts +42 -0
  15. package/dist/i18n/model-texts.d.mts +42 -0
  16. package/dist/i18n/model-texts.mjs +46 -0
  17. package/dist/i18n/ui-texts.d.cts +5 -1
  18. package/dist/i18n/ui-texts.d.mts +5 -1
  19. package/dist/index.cjs +7 -0
  20. package/dist/index.d.cts +3 -2
  21. package/dist/index.d.mts +3 -2
  22. package/dist/index.mjs +3 -2
  23. package/dist/nav/model-routes.cjs +9 -3
  24. package/dist/nav/model-routes.d.cts +8 -1
  25. package/dist/nav/model-routes.d.mts +8 -1
  26. package/dist/nav/model-routes.mjs +9 -3
  27. package/dist/shared/field-resolver.cjs +3 -2
  28. package/dist/shared/field-resolver.d.cts +6 -0
  29. package/dist/shared/field-resolver.d.mts +6 -0
  30. package/dist/shared/field-resolver.mjs +3 -2
  31. package/dist/shared/meta-cache.cjs +3 -3
  32. package/dist/shared/meta-cache.d.cts +1 -1
  33. package/dist/shared/meta-cache.d.mts +1 -1
  34. package/dist/shared/meta-cache.mjs +3 -3
  35. package/dist/table/create-table-def.cjs +2 -1
  36. package/dist/table/create-table-def.mjs +2 -1
  37. package/dist/value-help/extract-literals.cjs +8 -5
  38. package/dist/value-help/extract-literals.d.cts +5 -2
  39. package/dist/value-help/extract-literals.d.mts +5 -2
  40. package/dist/value-help/extract-literals.mjs +8 -5
  41. package/dist/value-help/resolve-options.cjs +17 -8
  42. package/dist/value-help/resolve-options.d.cts +7 -3
  43. package/dist/value-help/resolve-options.d.mts +7 -3
  44. package/dist/value-help/resolve-options.mjs +17 -8
  45. package/package.json +10 -10
@@ -1,4 +1,5 @@
1
1
  const require_annotation_keys = require("../shared/annotation-keys.cjs");
2
+ const require_model_texts = require("../i18n/model-texts.cjs");
2
3
  const require_field_resolver = require("../shared/field-resolver.cjs");
3
4
  const require_messages = require("../messages.cjs");
4
5
  const require_extract_literals = require("../value-help/extract-literals.cjs");
@@ -60,7 +61,9 @@ function unnamed(field) {
60
61
  * becomes a nested object field, since inlining it would never end.
61
62
  */
62
63
  function createFormDef(type, opts) {
63
- return buildFormDef(type, opts?.mode === "edit", opts ?? {});
64
+ const def = buildFormDef(type, opts?.mode === "edit", opts ?? {});
65
+ if (require_model_texts.isServedType(type)) def.served = true;
66
+ return def;
64
67
  }
65
68
  /**
66
69
  * The def of an object (or a leaf) type. `root`: the caller's options, for the
@@ -399,7 +402,10 @@ function createVariant(def, edit) {
399
402
  const label = require_field_resolver.getFieldMeta(def, require_annotation_keys.META_LABEL);
400
403
  const hasComponent = require_field_resolver.getFieldMeta(def, require_annotation_keys.UI_FORM_COMPONENT) !== void 0;
401
404
  return {
402
- ...label === void 0 ? typeLabel("object") : { label },
405
+ ...label === void 0 ? typeLabel("object") : {
406
+ label,
407
+ labelText: label
408
+ },
403
409
  type: def,
404
410
  def: buildFormDef(def, edit),
405
411
  itemField: hasComponent ? createFieldDef("", def, edit) : void 0
@@ -1,4 +1,5 @@
1
1
  import { META_DESCRIPTION, META_LABEL, UI_FORM_COMPONENT, UI_FORM_PUSH_DOWN, UI_TYPE } from "../shared/annotation-keys.mjs";
2
+ import { isServedType } from "../i18n/model-texts.mjs";
2
3
  import { getFieldMeta, hasComputedAnnotations } from "../shared/field-resolver.mjs";
3
4
  import { AS_UI_MESSAGES } from "../messages.mjs";
4
5
  import { isEmptyMember, isPureLiteralUnion } from "../value-help/extract-literals.mjs";
@@ -60,7 +61,9 @@ function unnamed(field) {
60
61
  * becomes a nested object field, since inlining it would never end.
61
62
  */
62
63
  function createFormDef(type, opts) {
63
- return buildFormDef(type, opts?.mode === "edit", opts ?? {});
64
+ const def = buildFormDef(type, opts?.mode === "edit", opts ?? {});
65
+ if (isServedType(type)) def.served = true;
66
+ return def;
64
67
  }
65
68
  /**
66
69
  * The def of an object (or a leaf) type. `root`: the caller's options, for the
@@ -399,7 +402,10 @@ function createVariant(def, edit) {
399
402
  const label = getFieldMeta(def, META_LABEL);
400
403
  const hasComponent = getFieldMeta(def, UI_FORM_COMPONENT) !== void 0;
401
404
  return {
402
- ...label === void 0 ? typeLabel("object") : { label },
405
+ ...label === void 0 ? typeLabel("object") : {
406
+ label,
407
+ labelText: label
408
+ },
403
409
  type: def,
404
410
  def: buildFormDef(def, edit),
405
411
  itemField: hasComponent ? createFieldDef("", def, edit) : void 0
@@ -1,13 +1,13 @@
1
1
  require("../shared/annotation-keys.cjs");
2
- const require_field_resolver = require("../shared/field-resolver.cjs");
2
+ const require_model_texts = require("../i18n/model-texts.cjs");
3
3
  const require_ui_texts = require("../i18n/ui-texts.cjs");
4
4
  //#region src/form/labels.ts
5
5
  /**
6
6
  * Singular label for an array field (used by AsArray for "Add <singular>"):
7
- * `@ui.form.label.singular`, else `t("UI_ITEM")` ("item").
7
+ * `@ui.form.label.singular` (translated with `text`), else `t("UI_ITEM")` ("item").
8
8
  */
9
- function resolveSingularLabel(meta, t = require_ui_texts.getUiTexts().t) {
10
- return meta && require_field_resolver.getFieldMeta(meta, "ui.form.label.singular") || t("UI_ITEM");
9
+ function resolveSingularLabel(meta, t = require_ui_texts.getUiTexts().t, text) {
10
+ return meta && require_model_texts.fieldText(meta, "ui.form.label.singular", text) || t("UI_ITEM");
11
11
  }
12
12
  //#endregion
13
13
  exports.resolveSingularLabel = resolveSingularLabel;
@@ -1,11 +1,12 @@
1
+ import { UserTextT } from "../i18n/model-texts.cjs";
1
2
  import { UiT } from "../i18n/ui-texts.cjs";
2
3
  import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
3
4
 
4
5
  //#region src/form/labels.d.ts
5
6
  /**
6
7
  * Singular label for an array field (used by AsArray for "Add <singular>"):
7
- * `@ui.form.label.singular`, else `t("UI_ITEM")` ("item").
8
+ * `@ui.form.label.singular` (translated with `text`), else `t("UI_ITEM")` ("item").
8
9
  */
9
- declare function resolveSingularLabel(meta: TAtscriptAnnotatedType | undefined, t?: UiT): string;
10
+ declare function resolveSingularLabel(meta: TAtscriptAnnotatedType | undefined, t?: UiT, text?: UserTextT): string;
10
11
  //#endregion
11
12
  export { resolveSingularLabel };
@@ -1,11 +1,12 @@
1
+ import { UserTextT } from "../i18n/model-texts.mjs";
1
2
  import { UiT } from "../i18n/ui-texts.mjs";
2
3
  import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
3
4
 
4
5
  //#region src/form/labels.d.ts
5
6
  /**
6
7
  * Singular label for an array field (used by AsArray for "Add <singular>"):
7
- * `@ui.form.label.singular`, else `t("UI_ITEM")` ("item").
8
+ * `@ui.form.label.singular` (translated with `text`), else `t("UI_ITEM")` ("item").
8
9
  */
9
- declare function resolveSingularLabel(meta: TAtscriptAnnotatedType | undefined, t?: UiT): string;
10
+ declare function resolveSingularLabel(meta: TAtscriptAnnotatedType | undefined, t?: UiT, text?: UserTextT): string;
10
11
  //#endregion
11
12
  export { resolveSingularLabel };
@@ -1,13 +1,13 @@
1
1
  import "../shared/annotation-keys.mjs";
2
- import { getFieldMeta } from "../shared/field-resolver.mjs";
2
+ import { fieldText } from "../i18n/model-texts.mjs";
3
3
  import { getUiTexts } from "../i18n/ui-texts.mjs";
4
4
  //#region src/form/labels.ts
5
5
  /**
6
6
  * Singular label for an array field (used by AsArray for "Add <singular>"):
7
- * `@ui.form.label.singular`, else `t("UI_ITEM")` ("item").
7
+ * `@ui.form.label.singular` (translated with `text`), else `t("UI_ITEM")` ("item").
8
8
  */
9
- function resolveSingularLabel(meta, t = getUiTexts().t) {
10
- return meta && getFieldMeta(meta, "ui.form.label.singular") || t("UI_ITEM");
9
+ function resolveSingularLabel(meta, t = getUiTexts().t, text) {
10
+ return meta && fieldText(meta, "ui.form.label.singular", text) || t("UI_ITEM");
11
11
  }
12
12
  //#endregion
13
13
  export { resolveSingularLabel };
@@ -93,11 +93,23 @@ interface FormDef {
93
93
  mainFields: FormFieldDef[];
94
94
  pushDownFields: FormFieldDef[];
95
95
  flatMap: Map<string, TAtscriptAnnotatedType>;
96
+ /**
97
+ * Set when `type` was served already translated (`deserializeServedType`): a
98
+ * renderer then skips translating the type's texts with the app's `t`.
99
+ * Present on the form's own def only.
100
+ */
101
+ served?: true;
96
102
  }
97
103
  /** One branch of a union type — used by union fields and union array items. */
98
104
  interface FormUnionVariant {
99
105
  /** Display label — from @meta.label or auto-generated (e.g. "1. String") */
100
106
  label: string;
107
+ /**
108
+ * The `@meta.label` text `label` ends with. Translate it at render time
109
+ * (`userText`) — a definition is built once for every locale and for texts
110
+ * a server already translated.
111
+ */
112
+ labelText?: string;
101
113
  /**
102
114
  * The UI text code of an auto-generated label's type name
103
115
  * (`UI_FORM_TYPE_STRING`); `label` ends with its English text. Render the
@@ -93,11 +93,23 @@ interface FormDef {
93
93
  mainFields: FormFieldDef[];
94
94
  pushDownFields: FormFieldDef[];
95
95
  flatMap: Map<string, TAtscriptAnnotatedType>;
96
+ /**
97
+ * Set when `type` was served already translated (`deserializeServedType`): a
98
+ * renderer then skips translating the type's texts with the app's `t`.
99
+ * Present on the form's own def only.
100
+ */
101
+ served?: true;
96
102
  }
97
103
  /** One branch of a union type — used by union fields and union array items. */
98
104
  interface FormUnionVariant {
99
105
  /** Display label — from @meta.label or auto-generated (e.g. "1. String") */
100
106
  label: string;
107
+ /**
108
+ * The `@meta.label` text `label` ends with. Translate it at render time
109
+ * (`userText`) — a definition is built once for every locale and for texts
110
+ * a server already translated.
111
+ */
112
+ labelText?: string;
101
113
  /**
102
114
  * The UI text code of an auto-generated label's type name
103
115
  * (`UI_FORM_TYPE_STRING`); `label` ends with its English text. Render the
@@ -7,11 +7,22 @@ let _atscript_typescript_utils = require("@atscript/typescript/utils");
7
7
  let _atscript_db_validator = require("@atscript/db/validator");
8
8
  //#region src/form/validate.ts
9
9
  /**
10
+ * The code of the error a failing `@ui.form.validate` rule reports: its message is
11
+ * worded with the app's `t` already (the rule's own `t`), so it is never looked up again.
12
+ */
13
+ const UI_FORM_VALIDATE_ERROR_CODE = "UI_FORM_VALIDATE";
14
+ /**
10
15
  * `error`'s message in the language of `t` (looked up by code or text, see
11
16
  * {@link TFormValidatorCallOptions.t}); the English message when `t` has none.
12
17
  * Its `details` are dropped first: only the message is read.
18
+ *
19
+ * A message that is final text is returned as it is: a rule's (`UI_FORM_VALIDATE`: worded
20
+ * already), and, when `served` (the type's schema was translated by the server), every
21
+ * message of the type's own annotations (`@expect.*` / `@meta.required` messages) — looking
22
+ * a translated text up as an id would translate it twice. Codes are always looked up.
13
23
  */
14
- function messageIn(error, t) {
24
+ function messageIn(error, t, served) {
25
+ if (error.code === "UI_FORM_VALIDATE" || served && error.custom) return error.message;
15
26
  return (0, _atscript_typescript_utils.localizeErrors)([{
16
27
  ...error,
17
28
  details: void 0
@@ -111,6 +122,7 @@ function getFormValidator(def, opts) {
111
122
  ]
112
123
  };
113
124
  const validator = new _atscript_typescript_utils.Validator(def.type, options);
125
+ const served = def.served === true;
114
126
  const validate = (callOpts) => {
115
127
  if (validator.validate(callOpts.data, true, {
116
128
  data: callOpts.data,
@@ -120,7 +132,7 @@ function getFormValidator(def, opts) {
120
132
  const t = callOpts.t ?? require_ui_texts.getUiTexts().t;
121
133
  return require_error_utils.validatorErrorsToMap(validator.errors.map((e) => ({
122
134
  path: e.path,
123
- message: messageIn(e, t)
135
+ message: messageIn(e, t, served)
124
136
  })));
125
137
  };
126
138
  return Object.assign(validate, { normalize: (data) => (0, _atscript_typescript_utils.normalizeForType)(def.type, data, options) });
@@ -171,15 +183,17 @@ function createFieldValidator(prop, opts) {
171
183
  const judged = skipNormalize ? value : (0, _atscript_typescript_utils.normalizeForType)(prop, value, options);
172
184
  if (cached.validate(judged, true, externalCtx)) return true;
173
185
  const t = externalCtx?.t ?? opts?.t ?? require_ui_texts.getUiTexts().t;
186
+ const served = opts?.served?.();
174
187
  if (opts?.rootOnly) {
175
188
  const rootError = cached.errors?.find((e) => e.path === "");
176
- return rootError ? messageIn(rootError, t) : true;
189
+ return rootError ? messageIn(rootError, t, served) : true;
177
190
  }
178
191
  const error = cached.errors?.[0];
179
- return error && messageIn(error, t) || t("UI_INVALID_VALUE");
192
+ return error && messageIn(error, t, served) || t("UI_INVALID_VALUE");
180
193
  };
181
194
  }
182
195
  //#endregion
196
+ exports.UI_FORM_VALIDATE_ERROR_CODE = UI_FORM_VALIDATE_ERROR_CODE;
183
197
  exports.createFieldValidator = createFieldValidator;
184
198
  exports.getDefaultValidatorPlugins = getDefaultValidatorPlugins;
185
199
  exports.getFormValidator = getFormValidator;
@@ -27,6 +27,11 @@ interface FormValidator {
27
27
  */
28
28
  normalize<T>(data: T): T;
29
29
  }
30
+ /**
31
+ * The code of the error a failing `@ui.form.validate` rule reports: its message is
32
+ * worded with the app's `t` already (the rule's own `t`), so it is never looked up again.
33
+ */
34
+ declare const UI_FORM_VALIDATE_ERROR_CODE = "UI_FORM_VALIDATE";
30
35
  /** Replace the default validator plugins applied to every form/field validator. */
31
36
  declare function setDefaultValidatorPlugins(plugins: TValidatorPlugin[]): void;
32
37
  /** Get the currently registered default validator plugins. */
@@ -53,6 +58,11 @@ interface TFieldValidatorOptions {
53
58
  rootOnly?: boolean;
54
59
  /** Words the fallback error (`UI_INVALID_VALUE`); the active UI texts when omitted. */
55
60
  t?: UiT;
61
+ /**
62
+ * Whether the prop's schema was served translated (read per validation): the messages of its
63
+ * own annotations are then not looked up as ids again. Codes are still translated.
64
+ */
65
+ served?: () => boolean;
56
66
  }
57
67
  /**
58
68
  * Whether a type kind is a container that carries its own children:
@@ -78,4 +88,4 @@ declare function createFieldValidator(prop: TAtscriptAnnotatedType, opts?: TFiel
78
88
  t?: UiT;
79
89
  }) => true | string;
80
90
  //#endregion
81
- export { FormValidator, TFieldValidatorOptions, TFormValidatorCallOptions, createFieldValidator, getDefaultValidatorPlugins, getFormValidator, isContainerKind, setDefaultValidatorPlugins };
91
+ export { FormValidator, TFieldValidatorOptions, TFormValidatorCallOptions, UI_FORM_VALIDATE_ERROR_CODE, createFieldValidator, getDefaultValidatorPlugins, getFormValidator, isContainerKind, setDefaultValidatorPlugins };
@@ -27,6 +27,11 @@ interface FormValidator {
27
27
  */
28
28
  normalize<T>(data: T): T;
29
29
  }
30
+ /**
31
+ * The code of the error a failing `@ui.form.validate` rule reports: its message is
32
+ * worded with the app's `t` already (the rule's own `t`), so it is never looked up again.
33
+ */
34
+ declare const UI_FORM_VALIDATE_ERROR_CODE = "UI_FORM_VALIDATE";
30
35
  /** Replace the default validator plugins applied to every form/field validator. */
31
36
  declare function setDefaultValidatorPlugins(plugins: TValidatorPlugin[]): void;
32
37
  /** Get the currently registered default validator plugins. */
@@ -53,6 +58,11 @@ interface TFieldValidatorOptions {
53
58
  rootOnly?: boolean;
54
59
  /** Words the fallback error (`UI_INVALID_VALUE`); the active UI texts when omitted. */
55
60
  t?: UiT;
61
+ /**
62
+ * Whether the prop's schema was served translated (read per validation): the messages of its
63
+ * own annotations are then not looked up as ids again. Codes are still translated.
64
+ */
65
+ served?: () => boolean;
56
66
  }
57
67
  /**
58
68
  * Whether a type kind is a container that carries its own children:
@@ -78,4 +88,4 @@ declare function createFieldValidator(prop: TAtscriptAnnotatedType, opts?: TFiel
78
88
  t?: UiT;
79
89
  }) => true | string;
80
90
  //#endregion
81
- export { FormValidator, TFieldValidatorOptions, TFormValidatorCallOptions, createFieldValidator, getDefaultValidatorPlugins, getFormValidator, isContainerKind, setDefaultValidatorPlugins };
91
+ export { FormValidator, TFieldValidatorOptions, TFormValidatorCallOptions, UI_FORM_VALIDATE_ERROR_CODE, createFieldValidator, getDefaultValidatorPlugins, getFormValidator, isContainerKind, setDefaultValidatorPlugins };
@@ -7,11 +7,22 @@ import { Validator, localizeErrors, normalizeForType } from "@atscript/typescrip
7
7
  import { createDbValidatorPlugin } from "@atscript/db/validator";
8
8
  //#region src/form/validate.ts
9
9
  /**
10
+ * The code of the error a failing `@ui.form.validate` rule reports: its message is
11
+ * worded with the app's `t` already (the rule's own `t`), so it is never looked up again.
12
+ */
13
+ const UI_FORM_VALIDATE_ERROR_CODE = "UI_FORM_VALIDATE";
14
+ /**
10
15
  * `error`'s message in the language of `t` (looked up by code or text, see
11
16
  * {@link TFormValidatorCallOptions.t}); the English message when `t` has none.
12
17
  * Its `details` are dropped first: only the message is read.
18
+ *
19
+ * A message that is final text is returned as it is: a rule's (`UI_FORM_VALIDATE`: worded
20
+ * already), and, when `served` (the type's schema was translated by the server), every
21
+ * message of the type's own annotations (`@expect.*` / `@meta.required` messages) — looking
22
+ * a translated text up as an id would translate it twice. Codes are always looked up.
13
23
  */
14
- function messageIn(error, t) {
24
+ function messageIn(error, t, served) {
25
+ if (error.code === "UI_FORM_VALIDATE" || served && error.custom) return error.message;
15
26
  return localizeErrors([{
16
27
  ...error,
17
28
  details: void 0
@@ -111,6 +122,7 @@ function getFormValidator(def, opts) {
111
122
  ]
112
123
  };
113
124
  const validator = new Validator(def.type, options);
125
+ const served = def.served === true;
114
126
  const validate = (callOpts) => {
115
127
  if (validator.validate(callOpts.data, true, {
116
128
  data: callOpts.data,
@@ -120,7 +132,7 @@ function getFormValidator(def, opts) {
120
132
  const t = callOpts.t ?? getUiTexts().t;
121
133
  return validatorErrorsToMap(validator.errors.map((e) => ({
122
134
  path: e.path,
123
- message: messageIn(e, t)
135
+ message: messageIn(e, t, served)
124
136
  })));
125
137
  };
126
138
  return Object.assign(validate, { normalize: (data) => normalizeForType(def.type, data, options) });
@@ -171,13 +183,14 @@ function createFieldValidator(prop, opts) {
171
183
  const judged = skipNormalize ? value : normalizeForType(prop, value, options);
172
184
  if (cached.validate(judged, true, externalCtx)) return true;
173
185
  const t = externalCtx?.t ?? opts?.t ?? getUiTexts().t;
186
+ const served = opts?.served?.();
174
187
  if (opts?.rootOnly) {
175
188
  const rootError = cached.errors?.find((e) => e.path === "");
176
- return rootError ? messageIn(rootError, t) : true;
189
+ return rootError ? messageIn(rootError, t, served) : true;
177
190
  }
178
191
  const error = cached.errors?.[0];
179
- return error && messageIn(error, t) || t("UI_INVALID_VALUE");
192
+ return error && messageIn(error, t, served) || t("UI_INVALID_VALUE");
180
193
  };
181
194
  }
182
195
  //#endregion
183
- export { createFieldValidator, getDefaultValidatorPlugins, getFormValidator, isContainerKind, setDefaultValidatorPlugins };
196
+ export { UI_FORM_VALIDATE_ERROR_CODE, createFieldValidator, getDefaultValidatorPlugins, getFormValidator, isContainerKind, setDefaultValidatorPlugins };
@@ -0,0 +1,50 @@
1
+ let _atscript_typescript_utils = require("@atscript/typescript/utils");
2
+ //#region src/i18n/model-texts.ts
3
+ /**
4
+ * The value of metadata key `key` in the app's language, by the rules the
5
+ * server translates a served type by (`translateUserText`): a text key's
6
+ * string — each item of a list, the named fields of a value object — is
7
+ * `t(text)`; any other key is returned as it is. Never changes `value`; `value`
8
+ * itself when `t` is omitted (or no function) or nothing is translated.
9
+ */
10
+ function userText(key, value, t) {
11
+ return typeof t !== "function" || value === void 0 ? value : (0, _atscript_typescript_utils.translateUserText)(key, value, t);
12
+ }
13
+ function fieldText(prop, key, t) {
14
+ return userText(key, prop.metadata.get(key), t);
15
+ }
16
+ /** The roots of the types a server served already translated (the types themselves stay untouched). */
17
+ const served = /* @__PURE__ */ new WeakSet();
18
+ /**
19
+ * Marks `type` — a root deserialized from a schema the server served — as
20
+ * translated by the server: a form built from it does not translate its texts
21
+ * again. A translated text that equals another source text would be
22
+ * mistranslated otherwise. For a served schema you deserialize yourself: prefer
23
+ * {@link deserializeServedType}, which marks by the schema's `$i18n`.
24
+ */
25
+ function markServedType(type) {
26
+ served.add(type);
27
+ return type;
28
+ }
29
+ /** Whether `type` was marked served ({@link markServedType}). */
30
+ function isServedType(type) {
31
+ return served.has(type);
32
+ }
33
+ /**
34
+ * `deserializeAnnotatedType` for a schema the server served: a schema carrying
35
+ * `$i18n` — even an empty `{}` — was translated by the server (its texts, its
36
+ * message codes and expression literals), so the result is marked served. A
37
+ * schema without `$i18n` is untranslated source: the form translates it with
38
+ * the app's `t`. (A server older than the release that added this mark can
39
+ * leave out an empty `$i18n` after translating: upgrade server and client together.)
40
+ */
41
+ function deserializeServedType(json) {
42
+ const type = (0, _atscript_typescript_utils.deserializeAnnotatedType)(json);
43
+ return json.$i18n === void 0 ? type : markServedType(type);
44
+ }
45
+ //#endregion
46
+ exports.deserializeServedType = deserializeServedType;
47
+ exports.fieldText = fieldText;
48
+ exports.isServedType = isServedType;
49
+ exports.markServedType = markServedType;
50
+ exports.userText = userText;
@@ -0,0 +1,42 @@
1
+ import { TAtscriptAnnotatedType, TSerializedAnnotatedType } from "@atscript/typescript/utils";
2
+
3
+ //#region src/i18n/model-texts.d.ts
4
+ /**
5
+ * Translates one text of the model (a label, a hint, …): the translation, or
6
+ * `undefined` to keep the text. A {@link UiT} is one (`provideAsLocale`'s
7
+ * `t`): the model's texts are translated with the same keys as the server's —
8
+ * the source text is the key.
9
+ */
10
+ type UserTextT = (text: string) => string | undefined;
11
+ /**
12
+ * The value of metadata key `key` in the app's language, by the rules the
13
+ * server translates a served type by (`translateUserText`): a text key's
14
+ * string — each item of a list, the named fields of a value object — is
15
+ * `t(text)`; any other key is returned as it is. Never changes `value`; `value`
16
+ * itself when `t` is omitted (or no function) or nothing is translated.
17
+ */
18
+ declare function userText<T>(key: string, value: T, t?: UserTextT): T;
19
+ /** The static metadata value `key` of `prop` in the app's language (see {@link userText}). */
20
+ declare function fieldText<K extends keyof AtscriptMetadata>(prop: TAtscriptAnnotatedType, key: K, t?: UserTextT): AtscriptMetadata[K] | undefined;
21
+ declare function fieldText(prop: TAtscriptAnnotatedType, key: string, t?: UserTextT): unknown;
22
+ /**
23
+ * Marks `type` — a root deserialized from a schema the server served — as
24
+ * translated by the server: a form built from it does not translate its texts
25
+ * again. A translated text that equals another source text would be
26
+ * mistranslated otherwise. For a served schema you deserialize yourself: prefer
27
+ * {@link deserializeServedType}, which marks by the schema's `$i18n`.
28
+ */
29
+ declare function markServedType<T extends TAtscriptAnnotatedType>(type: T): T;
30
+ /** Whether `type` was marked served ({@link markServedType}). */
31
+ declare function isServedType(type: TAtscriptAnnotatedType): boolean;
32
+ /**
33
+ * `deserializeAnnotatedType` for a schema the server served: a schema carrying
34
+ * `$i18n` — even an empty `{}` — was translated by the server (its texts, its
35
+ * message codes and expression literals), so the result is marked served. A
36
+ * schema without `$i18n` is untranslated source: the form translates it with
37
+ * the app's `t`. (A server older than the release that added this mark can
38
+ * leave out an empty `$i18n` after translating: upgrade server and client together.)
39
+ */
40
+ declare function deserializeServedType(json: TSerializedAnnotatedType): TAtscriptAnnotatedType;
41
+ //#endregion
42
+ export { UserTextT, deserializeServedType, fieldText, isServedType, markServedType, userText };
@@ -0,0 +1,42 @@
1
+ import { TAtscriptAnnotatedType, TSerializedAnnotatedType } from "@atscript/typescript/utils";
2
+
3
+ //#region src/i18n/model-texts.d.ts
4
+ /**
5
+ * Translates one text of the model (a label, a hint, …): the translation, or
6
+ * `undefined` to keep the text. A {@link UiT} is one (`provideAsLocale`'s
7
+ * `t`): the model's texts are translated with the same keys as the server's —
8
+ * the source text is the key.
9
+ */
10
+ type UserTextT = (text: string) => string | undefined;
11
+ /**
12
+ * The value of metadata key `key` in the app's language, by the rules the
13
+ * server translates a served type by (`translateUserText`): a text key's
14
+ * string — each item of a list, the named fields of a value object — is
15
+ * `t(text)`; any other key is returned as it is. Never changes `value`; `value`
16
+ * itself when `t` is omitted (or no function) or nothing is translated.
17
+ */
18
+ declare function userText<T>(key: string, value: T, t?: UserTextT): T;
19
+ /** The static metadata value `key` of `prop` in the app's language (see {@link userText}). */
20
+ declare function fieldText<K extends keyof AtscriptMetadata>(prop: TAtscriptAnnotatedType, key: K, t?: UserTextT): AtscriptMetadata[K] | undefined;
21
+ declare function fieldText(prop: TAtscriptAnnotatedType, key: string, t?: UserTextT): unknown;
22
+ /**
23
+ * Marks `type` — a root deserialized from a schema the server served — as
24
+ * translated by the server: a form built from it does not translate its texts
25
+ * again. A translated text that equals another source text would be
26
+ * mistranslated otherwise. For a served schema you deserialize yourself: prefer
27
+ * {@link deserializeServedType}, which marks by the schema's `$i18n`.
28
+ */
29
+ declare function markServedType<T extends TAtscriptAnnotatedType>(type: T): T;
30
+ /** Whether `type` was marked served ({@link markServedType}). */
31
+ declare function isServedType(type: TAtscriptAnnotatedType): boolean;
32
+ /**
33
+ * `deserializeAnnotatedType` for a schema the server served: a schema carrying
34
+ * `$i18n` — even an empty `{}` — was translated by the server (its texts, its
35
+ * message codes and expression literals), so the result is marked served. A
36
+ * schema without `$i18n` is untranslated source: the form translates it with
37
+ * the app's `t`. (A server older than the release that added this mark can
38
+ * leave out an empty `$i18n` after translating: upgrade server and client together.)
39
+ */
40
+ declare function deserializeServedType(json: TSerializedAnnotatedType): TAtscriptAnnotatedType;
41
+ //#endregion
42
+ export { UserTextT, deserializeServedType, fieldText, isServedType, markServedType, userText };
@@ -0,0 +1,46 @@
1
+ import { deserializeAnnotatedType, translateUserText } from "@atscript/typescript/utils";
2
+ //#region src/i18n/model-texts.ts
3
+ /**
4
+ * The value of metadata key `key` in the app's language, by the rules the
5
+ * server translates a served type by (`translateUserText`): a text key's
6
+ * string — each item of a list, the named fields of a value object — is
7
+ * `t(text)`; any other key is returned as it is. Never changes `value`; `value`
8
+ * itself when `t` is omitted (or no function) or nothing is translated.
9
+ */
10
+ function userText(key, value, t) {
11
+ return typeof t !== "function" || value === void 0 ? value : translateUserText(key, value, t);
12
+ }
13
+ function fieldText(prop, key, t) {
14
+ return userText(key, prop.metadata.get(key), t);
15
+ }
16
+ /** The roots of the types a server served already translated (the types themselves stay untouched). */
17
+ const served = /* @__PURE__ */ new WeakSet();
18
+ /**
19
+ * Marks `type` — a root deserialized from a schema the server served — as
20
+ * translated by the server: a form built from it does not translate its texts
21
+ * again. A translated text that equals another source text would be
22
+ * mistranslated otherwise. For a served schema you deserialize yourself: prefer
23
+ * {@link deserializeServedType}, which marks by the schema's `$i18n`.
24
+ */
25
+ function markServedType(type) {
26
+ served.add(type);
27
+ return type;
28
+ }
29
+ /** Whether `type` was marked served ({@link markServedType}). */
30
+ function isServedType(type) {
31
+ return served.has(type);
32
+ }
33
+ /**
34
+ * `deserializeAnnotatedType` for a schema the server served: a schema carrying
35
+ * `$i18n` — even an empty `{}` — was translated by the server (its texts, its
36
+ * message codes and expression literals), so the result is marked served. A
37
+ * schema without `$i18n` is untranslated source: the form translates it with
38
+ * the app's `t`. (A server older than the release that added this mark can
39
+ * leave out an empty `$i18n` after translating: upgrade server and client together.)
40
+ */
41
+ function deserializeServedType(json) {
42
+ const type = deserializeAnnotatedType(json);
43
+ return json.$i18n === void 0 ? type : markServedType(type);
44
+ }
45
+ //#endregion
46
+ export { deserializeServedType, fieldText, isServedType, markServedType, userText };
@@ -5,7 +5,11 @@ type UiTextParams = Record<string, unknown>;
5
5
  * An app's own translator for the built-in UI texts: return the final text
6
6
  * (params already filled in), or `undefined` to fall back to the texts the
7
7
  * server served for the locale, then to English. `id` is a code of
8
- * `AS_UI_MESSAGES` (`UI_CANCEL`, …) or a literal text.
8
+ * `AS_UI_MESSAGES` (`UI_CANCEL`, …) or a literal text — since 0.1.170 also
9
+ * the texts of the model a form renders from a type compiled into the client
10
+ * (labels, descriptions, hints, placeholders, prefixes, option labels, titles,
11
+ * nav labels, …): the source text is the id, as in the catalogue the server
12
+ * translates served schemas with.
9
13
  */
10
14
  type UiTranslate = (id: string, params: UiTextParams | undefined, locale: string | undefined) => string | undefined;
11
15
  /** `t(id, params?)` — a UI text in the current locale. */
@@ -5,7 +5,11 @@ type UiTextParams = Record<string, unknown>;
5
5
  * An app's own translator for the built-in UI texts: return the final text
6
6
  * (params already filled in), or `undefined` to fall back to the texts the
7
7
  * server served for the locale, then to English. `id` is a code of
8
- * `AS_UI_MESSAGES` (`UI_CANCEL`, …) or a literal text.
8
+ * `AS_UI_MESSAGES` (`UI_CANCEL`, …) or a literal text — since 0.1.170 also
9
+ * the texts of the model a form renders from a type compiled into the client
10
+ * (labels, descriptions, hints, placeholders, prefixes, option labels, titles,
11
+ * nav labels, …): the source text is the id, as in the catalogue the server
12
+ * translates served schemas with.
9
13
  */
10
14
  type UiTranslate = (id: string, params: UiTextParams | undefined, locale: string | undefined) => string | undefined;
11
15
  /** `t(id, params?)` — a UI text in the current locale. */
package/dist/index.cjs CHANGED
@@ -1,6 +1,7 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_annotation_keys = require("./shared/annotation-keys.cjs");
3
3
  const require_types = require("./form/types.cjs");
4
+ const require_model_texts = require("./i18n/model-texts.cjs");
4
5
  const require_field_resolver = require("./shared/field-resolver.cjs");
5
6
  const require_ui_texts = require("./i18n/ui-texts.cjs");
6
7
  const require_extract_literals = require("./value-help/extract-literals.cjs");
@@ -101,6 +102,7 @@ exports.UI_FORM_SUFFIX_ICON = require_annotation_keys.UI_FORM_SUFFIX_ICON;
101
102
  exports.UI_FORM_SUFFIX_REF = require_annotation_keys.UI_FORM_SUFFIX_REF;
102
103
  exports.UI_FORM_TYPE = require_annotation_keys.UI_FORM_TYPE;
103
104
  exports.UI_FORM_VALIDATE = require_annotation_keys.UI_FORM_VALIDATE;
105
+ exports.UI_FORM_VALIDATE_ERROR_CODE = require_validate.UI_FORM_VALIDATE_ERROR_CODE;
104
106
  exports.UI_NAV_GROUP = require_annotation_keys.UI_NAV_GROUP;
105
107
  exports.UI_NAV_HIDDEN = require_annotation_keys.UI_NAV_HIDDEN;
106
108
  exports.UI_NAV_ORDER = require_annotation_keys.UI_NAV_ORDER;
@@ -141,6 +143,7 @@ exports.deepClone = require_clone.deepClone;
141
143
  exports.deepEqual = require_diff.deepEqual;
142
144
  exports.defaultResolver = require_field_resolver.defaultResolver;
143
145
  exports.deleteByPath = require_path_utils.deleteByPath;
146
+ exports.deserializeServedType = require_model_texts.deserializeServedType;
144
147
  exports.detectUnionVariant = require_path_utils.detectUnionVariant;
145
148
  exports.enforceScale = require_decimal_format.enforceScale;
146
149
  exports.escapeRegex = require_str.escapeRegex;
@@ -149,6 +152,7 @@ exports.extractLiteralOptions = require_extract_literals.extractLiteralOptions;
149
152
  exports.extractMeasurement = require_measurement.extractMeasurement;
150
153
  exports.extractValueHelp = require_extract_ref.extractValueHelp;
151
154
  exports.fetchDistinctValues = require_distinct_client.fetchDistinctValues;
155
+ exports.fieldText = require_model_texts.fieldText;
152
156
  exports.formatDecimalForDisplay = require_decimal_format.formatDecimalForDisplay;
153
157
  exports.getByPath = require_path_utils.getByPath;
154
158
  exports.getColumn = require_column_resolver.getColumn;
@@ -179,11 +183,13 @@ exports.isObjectField = require_types.isObjectField;
179
183
  exports.isPathDirty = require_dirty.isPathDirty;
180
184
  exports.isPickerDeclined = require_distinct_client.isPickerDeclined;
181
185
  exports.isPureLiteralUnion = require_extract_literals.isPureLiteralUnion;
186
+ exports.isServedType = require_model_texts.isServedType;
182
187
  exports.isTupleField = require_types.isTupleField;
183
188
  exports.isUnionField = require_types.isUnionField;
184
189
  exports.iteratePathAncestors = require_error_utils.iteratePathAncestors;
185
190
  exports.joinDecimalString = require_decimal_format.joinDecimalString;
186
191
  exports.joinPath = require_path_utils.joinPath;
192
+ exports.markServedType = require_model_texts.markServedType;
187
193
  exports.mergeErrorMaps = require_error_utils.mergeErrorMaps;
188
194
  exports.navigateHrefFor = require_navigate_href.navigateHrefFor;
189
195
  exports.omitPaths = require_error_utils.omitPaths;
@@ -222,5 +228,6 @@ exports.setValueHelpCacheTtl = require_value_help_client.setValueHelpCacheTtl;
222
228
  exports.splitDecimalString = require_decimal_format.splitDecimalString;
223
229
  exports.str = require_str.str;
224
230
  exports.unionVariantChanged = require_union_detect.unionVariantChanged;
231
+ exports.userText = require_model_texts.userText;
225
232
  exports.validatorErrorsToMap = require_error_utils.validatorErrorsToMap;
226
233
  exports.valueHelpDictPaths = require_dict_paths.valueHelpDictPaths;