|
1 | 1 | // Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
2 | 2 |
|
3 | | -import type { ServiceObject } from '../data/object.zod'; |
4 | | - |
5 | 3 | /** |
6 | | - * Translation Skeleton Generator |
| 4 | + * Translation Skeleton Protocol Constants |
7 | 5 | * |
8 | | - * Generates an AI-friendly JSON "fill-in-the-blank" template from an object |
9 | | - * definition. The output contains `__TRANSLATE__` placeholders for every |
10 | | - * translatable string, ensuring AI/human translators cannot accidentally |
11 | | - * add or omit fields. |
| 6 | + * Defines the placeholder convention used in AI-friendly translation |
| 7 | + * skeleton templates. Runtime implementations (skeleton generation, |
| 8 | + * validation) belong in implementation packages (CLI, service-i18n, etc.). |
12 | 9 | * |
13 | 10 | * @example |
14 | | - * ```typescript |
15 | | - * import { generateTranslationSkeleton } from '@objectstack/spec/system'; |
16 | | - * import { Task } from './objects/task.object'; |
17 | | - * |
18 | | - * const skeleton = generateTranslationSkeleton(Task); |
19 | | - * // → JSON string with __TRANSLATE__ placeholders for all 18 fields |
| 11 | + * ```json |
| 12 | + * { |
| 13 | + * "label": "__TRANSLATE__: \"Task\"", |
| 14 | + * "fields": { |
| 15 | + * "subject": { "label": "__TRANSLATE__: \"Subject\"" } |
| 16 | + * } |
| 17 | + * } |
20 | 18 | * ``` |
21 | 19 | */ |
22 | 20 |
|
23 | | -/** Placeholder prefix used in skeleton output */ |
| 21 | +/** Placeholder prefix used in translation skeleton output */ |
24 | 22 | export const TRANSLATE_PLACEHOLDER = '__TRANSLATE__'; |
25 | | - |
26 | | -/** |
27 | | - * Generates a translation skeleton JSON string from an object definition. |
28 | | - * |
29 | | - * The skeleton includes: |
30 | | - * - Object-level `label` and `pluralLabel` |
31 | | - * - Every field with a `label` placeholder |
32 | | - * - `help` placeholder for fields that have a `description` |
33 | | - * - `options` map for select/multiselect fields with all option values |
34 | | - * |
35 | | - * @param objectDef - A parsed ServiceObject definition |
36 | | - * @returns A formatted JSON string with `__TRANSLATE__` placeholders |
37 | | - */ |
38 | | -export function generateTranslationSkeleton(objectDef: ServiceObject): string { |
39 | | - const skeleton: Record<string, unknown> = { |
40 | | - label: `${TRANSLATE_PLACEHOLDER}: "${objectDef.label}"`, |
41 | | - }; |
42 | | - |
43 | | - if (objectDef.pluralLabel) { |
44 | | - skeleton.pluralLabel = `${TRANSLATE_PLACEHOLDER}: "${objectDef.pluralLabel}"`; |
45 | | - } |
46 | | - |
47 | | - const fieldsObj: Record<string, Record<string, unknown>> = {}; |
48 | | - |
49 | | - for (const [fieldName, fieldDef] of Object.entries(objectDef.fields)) { |
50 | | - const fieldEntry: Record<string, unknown> = { |
51 | | - label: `${TRANSLATE_PLACEHOLDER}: "${fieldDef.label ?? fieldName}"`, |
52 | | - }; |
53 | | - |
54 | | - // Add help placeholder for fields with a description |
55 | | - if (fieldDef.description) { |
56 | | - fieldEntry.help = `${TRANSLATE_PLACEHOLDER}: "${fieldDef.description}"`; |
57 | | - } |
58 | | - |
59 | | - // Add options map for select/multiselect fields |
60 | | - if (fieldDef.options && fieldDef.options.length > 0) { |
61 | | - const optionsMap: Record<string, string> = {}; |
62 | | - for (const opt of fieldDef.options) { |
63 | | - optionsMap[opt.value] = `${TRANSLATE_PLACEHOLDER}: "${opt.label}"`; |
64 | | - } |
65 | | - fieldEntry.options = optionsMap; |
66 | | - } |
67 | | - |
68 | | - fieldsObj[fieldName] = fieldEntry; |
69 | | - } |
70 | | - |
71 | | - skeleton.fields = fieldsObj; |
72 | | - |
73 | | - return JSON.stringify(skeleton, null, 2); |
74 | | -} |
0 commit comments