@babelize/engine 0.3.5 → 0.3.8
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/config.d.ts +14 -58
- package/dist/index.d.ts +2 -0
- package/dist/index.js +11 -1
- package/dist/local/hosted-provider.js +1 -0
- package/dist/local/translate-core.d.ts +13 -2
- package/dist/local/translate-core.js +27 -9
- package/dist/local/translate-messages.d.ts +5 -0
- package/dist/local/translate-messages.js +1 -1
- package/dist/modules/finalization/nextjs-finalize.d.ts +19 -2
- package/dist/modules/finalization/nextjs-finalize.js +46 -4
- package/dist/modules/injection/inject.js +46 -1
- package/dist/modules/locale-builder/adopted.d.ts +29 -2
- package/dist/modules/locale-builder/adopted.js +185 -16
- package/dist/modules/locale-builder/locales.js +11 -2
- package/dist/modules/locale-builder/message-modules.d.ts +40 -0
- package/dist/modules/locale-builder/message-modules.js +263 -0
- package/dist/modules/lockfile/schema.d.ts +5 -157
- package/dist/modules/translator/client/byok.provider.js +8 -2
- package/dist/modules/translator/formality.d.ts +40 -0
- package/dist/modules/translator/formality.js +313 -0
- package/dist/modules/translator/translate.d.ts +18 -0
- package/dist/modules/translator/translate.js +78 -11
- package/dist/modules/workspace/existing-setup.d.ts +3 -1
- package/dist/modules/workspace/existing-setup.js +32 -15
- package/dist/orchestrator.js +51 -7
- package/dist/providers/default-translation-provider.js +2 -2
- package/dist/providers/types.d.ts +5 -0
- package/dist/qa/check-project.d.ts +7 -0
- package/dist/qa/check-project.js +12 -2
- package/dist/qa/locale-files.d.ts +8 -5
- package/dist/qa/locale-files.js +37 -9
- package/dist/qa/locale-qa.d.ts +10 -1
- package/dist/qa/locale-qa.js +9 -0
- package/dist/qa/review.js +1 -1
- package/dist/runtime/types.d.ts +21 -1
- package/dist/types.d.ts +2 -0
- package/dist/utils/translator-caller.d.ts +19 -0
- package/dist/utils/translator-caller.js +55 -21
- package/package.json +3 -3
|
@@ -1,190 +1,38 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
export declare const lockfileEntrySchema: z.ZodObject<{
|
|
3
|
-
/** SHA-256 hex digest of the source string */
|
|
4
3
|
sourceHash: z.ZodString;
|
|
5
|
-
/** ISO-8601 timestamp of last translation */
|
|
6
4
|
translatedAt: z.ZodString;
|
|
7
|
-
/** Translated values keyed by locale code */
|
|
8
5
|
languages: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
9
|
-
/** Metadata per locale */
|
|
10
6
|
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
11
7
|
fromMemory: z.ZodOptional<z.ZodBoolean>;
|
|
12
8
|
sourceHash: z.ZodOptional<z.ZodString>;
|
|
13
|
-
/** This key's translation for the locale is known to be in TM; skip backfill. */
|
|
14
9
|
inTm: z.ZodOptional<z.ZodBoolean>;
|
|
15
|
-
/** Pseudo-localized (`--provider pseudo`), not a translation: any real run re-translates it. */
|
|
16
10
|
pseudo: z.ZodOptional<z.ZodBoolean>;
|
|
17
|
-
/**
|
|
18
|
-
* Fingerprint of the glossary rules that applied to this key's source
|
|
19
|
-
* for the locale (see glossaryFingerprint). Absent: none applied.
|
|
20
|
-
* A run whose rules give a different fingerprint re-translates it.
|
|
21
|
-
*/
|
|
22
11
|
glossaryHash: z.ZodOptional<z.ZodString>;
|
|
23
|
-
/**
|
|
24
|
-
* The translation came from the project's own files (e.g. a .po
|
|
25
|
-
* catalogue), not from Babelize: glossary edits don't overwrite it.
|
|
26
|
-
*/
|
|
27
12
|
external: z.ZodOptional<z.ZodBoolean>;
|
|
28
|
-
/** Edited in the dashboard (a translation override), not translated by Babelize. */
|
|
29
13
|
manual: z.ZodOptional<z.ZodBoolean>;
|
|
30
|
-
},
|
|
31
|
-
|
|
32
|
-
fromMemory?: boolean | undefined;
|
|
33
|
-
inTm?: boolean | undefined;
|
|
34
|
-
pseudo?: boolean | undefined;
|
|
35
|
-
glossaryHash?: string | undefined;
|
|
36
|
-
external?: boolean | undefined;
|
|
37
|
-
manual?: boolean | undefined;
|
|
38
|
-
}, {
|
|
39
|
-
sourceHash?: string | undefined;
|
|
40
|
-
fromMemory?: boolean | undefined;
|
|
41
|
-
inTm?: boolean | undefined;
|
|
42
|
-
pseudo?: boolean | undefined;
|
|
43
|
-
glossaryHash?: string | undefined;
|
|
44
|
-
external?: boolean | undefined;
|
|
45
|
-
manual?: boolean | undefined;
|
|
46
|
-
}>>>;
|
|
47
|
-
}, "strip", z.ZodTypeAny, {
|
|
48
|
-
sourceHash: string;
|
|
49
|
-
translatedAt: string;
|
|
50
|
-
languages: Record<string, string>;
|
|
51
|
-
metadata?: Record<string, {
|
|
52
|
-
sourceHash?: string | undefined;
|
|
53
|
-
fromMemory?: boolean | undefined;
|
|
54
|
-
inTm?: boolean | undefined;
|
|
55
|
-
pseudo?: boolean | undefined;
|
|
56
|
-
glossaryHash?: string | undefined;
|
|
57
|
-
external?: boolean | undefined;
|
|
58
|
-
manual?: boolean | undefined;
|
|
59
|
-
}> | undefined;
|
|
60
|
-
}, {
|
|
61
|
-
sourceHash: string;
|
|
62
|
-
translatedAt: string;
|
|
63
|
-
languages: Record<string, string>;
|
|
64
|
-
metadata?: Record<string, {
|
|
65
|
-
sourceHash?: string | undefined;
|
|
66
|
-
fromMemory?: boolean | undefined;
|
|
67
|
-
inTm?: boolean | undefined;
|
|
68
|
-
pseudo?: boolean | undefined;
|
|
69
|
-
glossaryHash?: string | undefined;
|
|
70
|
-
external?: boolean | undefined;
|
|
71
|
-
manual?: boolean | undefined;
|
|
72
|
-
}> | undefined;
|
|
73
|
-
}>;
|
|
14
|
+
}, z.core.$strip>>>;
|
|
15
|
+
}, z.core.$strip>;
|
|
74
16
|
export type LockfileEntry = z.infer<typeof lockfileEntrySchema>;
|
|
75
17
|
export declare const lockfileDataSchema: z.ZodObject<{
|
|
76
18
|
version: z.ZodDefault<z.ZodString>;
|
|
77
19
|
projectId: z.ZodString;
|
|
78
20
|
updatedAt: z.ZodString;
|
|
79
21
|
keys: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
80
|
-
/** SHA-256 hex digest of the source string */
|
|
81
22
|
sourceHash: z.ZodString;
|
|
82
|
-
/** ISO-8601 timestamp of last translation */
|
|
83
23
|
translatedAt: z.ZodString;
|
|
84
|
-
/** Translated values keyed by locale code */
|
|
85
24
|
languages: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
86
|
-
/** Metadata per locale */
|
|
87
25
|
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
88
26
|
fromMemory: z.ZodOptional<z.ZodBoolean>;
|
|
89
27
|
sourceHash: z.ZodOptional<z.ZodString>;
|
|
90
|
-
/** This key's translation for the locale is known to be in TM; skip backfill. */
|
|
91
28
|
inTm: z.ZodOptional<z.ZodBoolean>;
|
|
92
|
-
/** Pseudo-localized (`--provider pseudo`), not a translation: any real run re-translates it. */
|
|
93
29
|
pseudo: z.ZodOptional<z.ZodBoolean>;
|
|
94
|
-
/**
|
|
95
|
-
* Fingerprint of the glossary rules that applied to this key's source
|
|
96
|
-
* for the locale (see glossaryFingerprint). Absent: none applied.
|
|
97
|
-
* A run whose rules give a different fingerprint re-translates it.
|
|
98
|
-
*/
|
|
99
30
|
glossaryHash: z.ZodOptional<z.ZodString>;
|
|
100
|
-
/**
|
|
101
|
-
* The translation came from the project's own files (e.g. a .po
|
|
102
|
-
* catalogue), not from Babelize: glossary edits don't overwrite it.
|
|
103
|
-
*/
|
|
104
31
|
external: z.ZodOptional<z.ZodBoolean>;
|
|
105
|
-
/** Edited in the dashboard (a translation override), not translated by Babelize. */
|
|
106
32
|
manual: z.ZodOptional<z.ZodBoolean>;
|
|
107
|
-
},
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
inTm?: boolean | undefined;
|
|
111
|
-
pseudo?: boolean | undefined;
|
|
112
|
-
glossaryHash?: string | undefined;
|
|
113
|
-
external?: boolean | undefined;
|
|
114
|
-
manual?: boolean | undefined;
|
|
115
|
-
}, {
|
|
116
|
-
sourceHash?: string | undefined;
|
|
117
|
-
fromMemory?: boolean | undefined;
|
|
118
|
-
inTm?: boolean | undefined;
|
|
119
|
-
pseudo?: boolean | undefined;
|
|
120
|
-
glossaryHash?: string | undefined;
|
|
121
|
-
external?: boolean | undefined;
|
|
122
|
-
manual?: boolean | undefined;
|
|
123
|
-
}>>>;
|
|
124
|
-
}, "strip", z.ZodTypeAny, {
|
|
125
|
-
sourceHash: string;
|
|
126
|
-
translatedAt: string;
|
|
127
|
-
languages: Record<string, string>;
|
|
128
|
-
metadata?: Record<string, {
|
|
129
|
-
sourceHash?: string | undefined;
|
|
130
|
-
fromMemory?: boolean | undefined;
|
|
131
|
-
inTm?: boolean | undefined;
|
|
132
|
-
pseudo?: boolean | undefined;
|
|
133
|
-
glossaryHash?: string | undefined;
|
|
134
|
-
external?: boolean | undefined;
|
|
135
|
-
manual?: boolean | undefined;
|
|
136
|
-
}> | undefined;
|
|
137
|
-
}, {
|
|
138
|
-
sourceHash: string;
|
|
139
|
-
translatedAt: string;
|
|
140
|
-
languages: Record<string, string>;
|
|
141
|
-
metadata?: Record<string, {
|
|
142
|
-
sourceHash?: string | undefined;
|
|
143
|
-
fromMemory?: boolean | undefined;
|
|
144
|
-
inTm?: boolean | undefined;
|
|
145
|
-
pseudo?: boolean | undefined;
|
|
146
|
-
glossaryHash?: string | undefined;
|
|
147
|
-
external?: boolean | undefined;
|
|
148
|
-
manual?: boolean | undefined;
|
|
149
|
-
}> | undefined;
|
|
150
|
-
}>>;
|
|
151
|
-
}, "strip", z.ZodTypeAny, {
|
|
152
|
-
keys: Record<string, {
|
|
153
|
-
sourceHash: string;
|
|
154
|
-
translatedAt: string;
|
|
155
|
-
languages: Record<string, string>;
|
|
156
|
-
metadata?: Record<string, {
|
|
157
|
-
sourceHash?: string | undefined;
|
|
158
|
-
fromMemory?: boolean | undefined;
|
|
159
|
-
inTm?: boolean | undefined;
|
|
160
|
-
pseudo?: boolean | undefined;
|
|
161
|
-
glossaryHash?: string | undefined;
|
|
162
|
-
external?: boolean | undefined;
|
|
163
|
-
manual?: boolean | undefined;
|
|
164
|
-
}> | undefined;
|
|
165
|
-
}>;
|
|
166
|
-
projectId: string;
|
|
167
|
-
version: string;
|
|
168
|
-
updatedAt: string;
|
|
169
|
-
}, {
|
|
170
|
-
keys: Record<string, {
|
|
171
|
-
sourceHash: string;
|
|
172
|
-
translatedAt: string;
|
|
173
|
-
languages: Record<string, string>;
|
|
174
|
-
metadata?: Record<string, {
|
|
175
|
-
sourceHash?: string | undefined;
|
|
176
|
-
fromMemory?: boolean | undefined;
|
|
177
|
-
inTm?: boolean | undefined;
|
|
178
|
-
pseudo?: boolean | undefined;
|
|
179
|
-
glossaryHash?: string | undefined;
|
|
180
|
-
external?: boolean | undefined;
|
|
181
|
-
manual?: boolean | undefined;
|
|
182
|
-
}> | undefined;
|
|
183
|
-
}>;
|
|
184
|
-
projectId: string;
|
|
185
|
-
updatedAt: string;
|
|
186
|
-
version?: string | undefined;
|
|
187
|
-
}>;
|
|
33
|
+
}, z.core.$strip>>>;
|
|
34
|
+
}, z.core.$strip>>;
|
|
35
|
+
}, z.core.$strip>;
|
|
188
36
|
export type LockfileData = z.infer<typeof lockfileDataSchema>;
|
|
189
37
|
export interface PartitionedKey {
|
|
190
38
|
key: string;
|
|
@@ -17,6 +17,7 @@ exports.parseTranslations = parseTranslations;
|
|
|
17
17
|
const types_1 = require("@babelize/types");
|
|
18
18
|
const retry_1 = require("./retry");
|
|
19
19
|
const language_codes_1 = require("../language-codes");
|
|
20
|
+
const formality_1 = require("../formality");
|
|
20
21
|
const icu_message_1 = require("../../../utils/icu-message");
|
|
21
22
|
// Rough character to token ratio
|
|
22
23
|
const CHAR_TO_TOKEN_RATIO = 4;
|
|
@@ -241,6 +242,7 @@ function buildSystemPrompt(req, entries) {
|
|
|
241
242
|
const targetCode = req.targetLocale || safeFromFlores(req.targetLang);
|
|
242
243
|
const source = languageName(safeFromFlores(req.sourceLang));
|
|
243
244
|
const target = languageName(targetCode);
|
|
245
|
+
const address = (0, formality_1.formalityInstruction)(targetCode, req.formality, target);
|
|
244
246
|
const rules = [
|
|
245
247
|
`You are a professional software localizer. Translate the user-interface strings of a web application from ${source} into ${target}.`,
|
|
246
248
|
'',
|
|
@@ -251,7 +253,9 @@ function buildSystemPrompt(req, entries) {
|
|
|
251
253
|
'Rules:',
|
|
252
254
|
'1. Tokens such as [#VAR_0#] stand for variables, markup tags, links or brand names. Copy every token exactly once and unchanged; move it if the target grammar needs a different word order. Never translate, add, merge or drop tokens.',
|
|
253
255
|
'2. Write natural, idiomatic UI text as a native speaker would — not a word-for-word rendering. Keep it concise: buttons, labels and menu items should stay short.',
|
|
254
|
-
|
|
256
|
+
req.formality && address
|
|
257
|
+
? `3. Use consistent terminology across all entries. ${address}`
|
|
258
|
+
: `3. Use one consistent formality level and terminology across all entries, matching what is conventional for modern software in the target language.${address ? ` ${address}` : ''}`,
|
|
255
259
|
"4. Follow the target language's punctuation, capitalization and spacing conventions. Keep any leading or trailing whitespace.",
|
|
256
260
|
'5. If a text is a proper name, a code identifier, or is already correct in the target language (e.g. "OK", "Email" where that is the norm), return it unchanged.',
|
|
257
261
|
'6. Never explain, never add quotes or notes, never leave text in the source language unless rule 5 applies.',
|
|
@@ -270,7 +274,9 @@ function buildSystemPrompt(req, entries) {
|
|
|
270
274
|
rules.push(`- "${g.term}" → "${g.translation}"`);
|
|
271
275
|
}
|
|
272
276
|
if (req.brandVoice) {
|
|
273
|
-
rules.push('',
|
|
277
|
+
rules.push('', req.formality && address
|
|
278
|
+
? 'Brand voice — follow this tone and style; it takes priority over rule 3, except for the form of address, which is fixed:'
|
|
279
|
+
: 'Brand voice — follow this tone and style; it takes priority over rule 3:', req.brandVoice);
|
|
274
280
|
}
|
|
275
281
|
return rules.join('\n');
|
|
276
282
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formal vs informal address per target language (#371): the forms the
|
|
3
|
+
* translator is told to use, and the marker words that tell which form a
|
|
4
|
+
* translated string uses (detection from an existing catalog, QA for mixed
|
|
5
|
+
* forms). Languages without a T–V distinction have no entry and get no
|
|
6
|
+
* instruction.
|
|
7
|
+
*/
|
|
8
|
+
export type FormalityLevel = 'formal' | 'informal';
|
|
9
|
+
export type FormalitySetting = FormalityLevel | 'auto';
|
|
10
|
+
/** True when the language distinguishes formal and informal address. */
|
|
11
|
+
export declare function hasFormalitySplit(locale: string): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* The formality configured for `locale`: the exact locale, else its base
|
|
14
|
+
* language (`de` covers `de-AT`). `auto` and unlisted locales: undefined.
|
|
15
|
+
*/
|
|
16
|
+
export declare function formalityFor(settings: Record<string, FormalitySetting> | null | undefined, locale: string): FormalityLevel | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* The prompt rule for the form of address. With a level set, it names the
|
|
19
|
+
* concrete forms to use and to avoid; on `auto` it still forbids mixing them.
|
|
20
|
+
* Undefined for languages without the distinction (and `auto`): the generic
|
|
21
|
+
* consistency rule applies.
|
|
22
|
+
*/
|
|
23
|
+
export declare function formalityInstruction(locale: string, level: FormalityLevel | undefined, languageName: string): string | undefined;
|
|
24
|
+
/** Which form a single string uses: undefined when it addresses no one (or both forms appear). */
|
|
25
|
+
export declare function formalityOf(text: string, locale: string): FormalityLevel | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* The form an existing catalog uses, when it clearly uses one: at least
|
|
28
|
+
* three strings address the user and 80% of them use the same form.
|
|
29
|
+
*/
|
|
30
|
+
export declare function detectFormality(values: Iterable<string>, locale: string): FormalityLevel | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Strings of one locale that break its form of address: with `expected`, the
|
|
33
|
+
* ones using the other form; without it, the minority form when both appear
|
|
34
|
+
* (all of them on a tie). Key → message.
|
|
35
|
+
*/
|
|
36
|
+
export declare function formalityIssues(values: Record<string, string>, locale: string, expected?: FormalityLevel): Record<string, string>;
|
|
37
|
+
/** Parse `de=formal,it=informal` (CLI flag values); throws on a malformed item. */
|
|
38
|
+
export declare function parseFormalityList(items: string[]): Record<string, FormalitySetting>;
|
|
39
|
+
/** Keep valid `locale → formal|informal|auto` entries of untrusted input (config files, DB rows). */
|
|
40
|
+
export declare function sanitizeFormality(raw: unknown): Record<string, FormalitySetting> | undefined;
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Formal vs informal address per target language (#371): the forms the
|
|
4
|
+
* translator is told to use, and the marker words that tell which form a
|
|
5
|
+
* translated string uses (detection from an existing catalog, QA for mixed
|
|
6
|
+
* forms). Languages without a T–V distinction have no entry and get no
|
|
7
|
+
* instruction.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.hasFormalitySplit = hasFormalitySplit;
|
|
11
|
+
exports.formalityFor = formalityFor;
|
|
12
|
+
exports.formalityInstruction = formalityInstruction;
|
|
13
|
+
exports.formalityOf = formalityOf;
|
|
14
|
+
exports.detectFormality = detectFormality;
|
|
15
|
+
exports.formalityIssues = formalityIssues;
|
|
16
|
+
exports.parseFormalityList = parseFormalityList;
|
|
17
|
+
exports.sanitizeFormality = sanitizeFormality;
|
|
18
|
+
/** Whole words: \b is ASCII-only, so letters are matched with \p{L}. */
|
|
19
|
+
const words = (list, flags = 'iu') => new RegExp(`(?<!\\p{L})(?:${list})(?!\\p{L})`, flags);
|
|
20
|
+
const SPECS = {
|
|
21
|
+
de: {
|
|
22
|
+
formal: '"Sie" (Ihr, Ihnen; imperatives like "Klicken Sie")',
|
|
23
|
+
informal: '"du" (dein, dich, dir; imperatives like "Klicke")',
|
|
24
|
+
// "Sie" at the very start may be "she/they"; capitalized mid-sentence it is the polite "you".
|
|
25
|
+
formalMarkers: /(?<=\p{L}[,;:]?\s)Sie(?!\p{L})|(?<!\p{L})(?:Ihnen|Ihr|Ihre|Ihrem|Ihren|Ihrer|Ihres)(?!\p{L})/u,
|
|
26
|
+
informalMarkers: words('du|dich|dir|dein|deine|deinem|deinen|deiner|deines'),
|
|
27
|
+
},
|
|
28
|
+
fr: {
|
|
29
|
+
formal: '"vous" (votre, vos; imperatives in -ez)',
|
|
30
|
+
informal: '"tu" (te, toi, ton, ta, tes; imperatives without -ez)',
|
|
31
|
+
formalMarkers: words('vous|votre|vos'),
|
|
32
|
+
informalMarkers: words("tu|toi|ton|ta|tes|te|t(?=['’]\\p{L})"),
|
|
33
|
+
},
|
|
34
|
+
es: {
|
|
35
|
+
formal: '"usted" (su, le; verbs in the third person, e.g. "Introduzca")',
|
|
36
|
+
informal: '"tú" (tu, te, ti; verbs in the second person, e.g. "Introduce")',
|
|
37
|
+
formalMarkers: words('usted|ustedes'),
|
|
38
|
+
informalMarkers: words('tú|tu|tus|te|ti|contigo'),
|
|
39
|
+
},
|
|
40
|
+
it: {
|
|
41
|
+
formal: '"Lei" (Suo, Sua, La; verbs in the third person, e.g. "Inserisca")',
|
|
42
|
+
informal: '"tu" (tuo, tua, ti; verbs in the second person, e.g. "Inserisci")',
|
|
43
|
+
formalMarkers: words('Lei|Suo|Sua|Suoi|Sue', 'u'),
|
|
44
|
+
informalMarkers: words('tu|tuo|tua|tuoi|tue|ti'),
|
|
45
|
+
},
|
|
46
|
+
pt: {
|
|
47
|
+
formal: '"o senhor" / "a senhora" (verbs in the third person)',
|
|
48
|
+
informal: '"você" in Brazilian Portuguese, "tu" in European Portuguese',
|
|
49
|
+
formalMarkers: words('o senhor|a senhora|os senhores|as senhoras'),
|
|
50
|
+
informalMarkers: words('você|vocês|tu|teu|tua|teus|tuas|contigo'),
|
|
51
|
+
},
|
|
52
|
+
nl: {
|
|
53
|
+
formal: '"u" (uw)',
|
|
54
|
+
informal: '"je" / "jij" (jouw, jou)',
|
|
55
|
+
formalMarkers: words('u|uw'),
|
|
56
|
+
informalMarkers: words('je|jij|jou|jouw'),
|
|
57
|
+
},
|
|
58
|
+
pl: {
|
|
59
|
+
formal: '"Pan" / "Pani" / "Państwo" (verbs in the third person)',
|
|
60
|
+
informal: '"ty" (twój; verbs in the second person singular)',
|
|
61
|
+
formalMarkers: words('Pan|Pani|Państwo|Pana|Panu|Panią|Państwa|Państwu', 'u'),
|
|
62
|
+
informalMarkers: words('ty|twój|twoja|twoje|twoich|twoim|ciebie|cię|tobie'),
|
|
63
|
+
},
|
|
64
|
+
tr: {
|
|
65
|
+
formal: '"siz" (sizin; imperatives like "Giriş yapın")',
|
|
66
|
+
informal: '"sen" (senin; imperatives like "Giriş yap")',
|
|
67
|
+
formalMarkers: words('siz|sizin|size|sizi|sizinle'),
|
|
68
|
+
informalMarkers: words('sen|senin|sana|seni|seninle'),
|
|
69
|
+
},
|
|
70
|
+
ru: {
|
|
71
|
+
formal: '"вы" (ваш, вам, вас)',
|
|
72
|
+
informal: '"ты" (твой, тебе, тебя)',
|
|
73
|
+
formalMarkers: words('вы|вас|вам|вами|ваш|ваша|ваше|ваши|вашего|вашей|вашим|ваших|вашу'),
|
|
74
|
+
informalMarkers: words('ты|тебя|тебе|тобой|твой|твоя|твоё|твое|твои|твоего|твоей|твоим|твоих|твою'),
|
|
75
|
+
},
|
|
76
|
+
uk: {
|
|
77
|
+
formal: '"ви" (ваш, вам, вас)',
|
|
78
|
+
informal: '"ти" (твій, тобі, тебе)',
|
|
79
|
+
formalMarkers: words('ви|вас|вам|вами|ваш|ваша|ваше|ваші|вашого|вашої|вашим|ваших|вашу'),
|
|
80
|
+
informalMarkers: words('ти|тебе|тобі|тобою|твій|твоя|твоє|твої|твого|твоєї|твоїм|твоїх|твою'),
|
|
81
|
+
},
|
|
82
|
+
bg: {
|
|
83
|
+
formal: '"Вие" (Ваш)',
|
|
84
|
+
informal: '"ти" (твой)',
|
|
85
|
+
formalMarkers: words('вие|вас|вам|ваш|ваша|ваше|ваши'),
|
|
86
|
+
informalMarkers: words('ти|теб|тебе|твой|твоя|твое|твои'),
|
|
87
|
+
},
|
|
88
|
+
cs: {
|
|
89
|
+
formal: '"vy" (váš; verbs in the second person plural)',
|
|
90
|
+
informal: '"ty" (tvůj; verbs in the second person singular)',
|
|
91
|
+
formalMarkers: words('vy|vás|vám|vámi|váš|vaše|vašeho|vaší|vašich|vašim'),
|
|
92
|
+
informalMarkers: words('ty|tebe|tě|tobě|ti|tvůj|tvoje|tvá|tvého|tvých|tvým'),
|
|
93
|
+
},
|
|
94
|
+
sk: {
|
|
95
|
+
formal: '"vy" (váš; verbs in the second person plural)',
|
|
96
|
+
informal: '"ty" (tvoj; verbs in the second person singular)',
|
|
97
|
+
formalMarkers: words('vy|vás|vám|vami|váš|vaša|vaše|vášho|vašej|vašich'),
|
|
98
|
+
informalMarkers: words('ty|teba|ťa|tebe|ti|tvoj|tvoja|tvoje|tvojho|tvojej|tvojich'),
|
|
99
|
+
},
|
|
100
|
+
hu: {
|
|
101
|
+
formal: '"Ön" / "Önök" (verbs in the third person)',
|
|
102
|
+
informal: '"te" (verbs in the second person)',
|
|
103
|
+
formalMarkers: words('Ön|Önök|Önt|Önnek|Önnel|Öntől|Önök', 'u'),
|
|
104
|
+
informalMarkers: words('te|téged|neked|veled|tőled'),
|
|
105
|
+
},
|
|
106
|
+
ro: {
|
|
107
|
+
formal: '"dumneavoastră" (verbs in the second person plural)',
|
|
108
|
+
informal: '"tu" (verbs in the second person singular)',
|
|
109
|
+
formalMarkers: words('dumneavoastră|dvs'),
|
|
110
|
+
informalMarkers: words('tu|tău|ta|tale|tine|ție|ţie'),
|
|
111
|
+
},
|
|
112
|
+
ca: {
|
|
113
|
+
formal: '"vostè" (verbs in the third person)',
|
|
114
|
+
informal: '"tu" (verbs in the second person)',
|
|
115
|
+
formalMarkers: words('vostè|vostès'),
|
|
116
|
+
informalMarkers: words('tu|teu|teva|teus|teves'),
|
|
117
|
+
},
|
|
118
|
+
el: {
|
|
119
|
+
formal: '"εσείς" (σας; verbs in the second person plural)',
|
|
120
|
+
informal: '"εσύ" (σου; verbs in the second person singular)',
|
|
121
|
+
formalMarkers: words('εσείς|σας'),
|
|
122
|
+
informalMarkers: words('εσύ|σου'),
|
|
123
|
+
},
|
|
124
|
+
sv: {
|
|
125
|
+
formal: '"ni" (er, ert) — rare in modern Swedish software, which uses "du"',
|
|
126
|
+
informal: '"du" (din, dig)',
|
|
127
|
+
informalMarkers: words('du|dig|din|ditt|dina'),
|
|
128
|
+
},
|
|
129
|
+
da: {
|
|
130
|
+
formal: '"De" (Dem, Deres) — rare in modern Danish software, which uses "du"',
|
|
131
|
+
informal: '"du" (din, dig)',
|
|
132
|
+
formalMarkers: words('Dem|Deres', 'u'),
|
|
133
|
+
informalMarkers: words('du|dig|din|dit|dine'),
|
|
134
|
+
},
|
|
135
|
+
nb: {
|
|
136
|
+
formal: '"De" (Dem, Deres) — rare in modern Norwegian software, which uses "du"',
|
|
137
|
+
informal: '"du" (din, deg)',
|
|
138
|
+
formalMarkers: words('Dem|Deres', 'u'),
|
|
139
|
+
informalMarkers: words('du|deg|din|ditt|dine'),
|
|
140
|
+
},
|
|
141
|
+
fi: {
|
|
142
|
+
formal: '"te" (Teidän; verbs in the second person plural) — rare in modern Finnish software, which uses "sinä"',
|
|
143
|
+
informal: '"sinä" (sinun; verbs in the second person singular)',
|
|
144
|
+
},
|
|
145
|
+
hi: {
|
|
146
|
+
formal: '"आप" (आपका, आपके)',
|
|
147
|
+
informal: '"तुम" (तुम्हारा, तुम्हें)',
|
|
148
|
+
formalMarkers: /आप/u,
|
|
149
|
+
informalMarkers: /तुम|तू(?!\p{M})/u,
|
|
150
|
+
},
|
|
151
|
+
id: {
|
|
152
|
+
formal: '"Anda"',
|
|
153
|
+
informal: '"kamu" / "kau"',
|
|
154
|
+
formalMarkers: words('Anda', 'u'),
|
|
155
|
+
informalMarkers: words('kamu|kau|mu'),
|
|
156
|
+
},
|
|
157
|
+
ms: {
|
|
158
|
+
formal: '"Anda"',
|
|
159
|
+
informal: '"kamu" / "awak"',
|
|
160
|
+
formalMarkers: words('Anda', 'u'),
|
|
161
|
+
informalMarkers: words('kamu|awak'),
|
|
162
|
+
},
|
|
163
|
+
zh: {
|
|
164
|
+
formal: '"您"',
|
|
165
|
+
informal: '"你"',
|
|
166
|
+
formalMarkers: /您/u,
|
|
167
|
+
informalMarkers: /你/u,
|
|
168
|
+
},
|
|
169
|
+
ja: {
|
|
170
|
+
formal: 'polite keigo (です/ます forms, ください)',
|
|
171
|
+
informal: 'plain casual forms (だ/る endings, no です/ます)',
|
|
172
|
+
},
|
|
173
|
+
ko: {
|
|
174
|
+
formal: 'formal polite speech (합니다/습니다 or 해요 endings)',
|
|
175
|
+
informal: 'casual speech (반말, 해/야 endings)',
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
function baseLanguage(locale) {
|
|
179
|
+
const base = locale.toLowerCase().split(/[-_]/)[0];
|
|
180
|
+
return base === 'no' || base === 'nn' ? 'nb' : base;
|
|
181
|
+
}
|
|
182
|
+
function specFor(locale) {
|
|
183
|
+
return SPECS[baseLanguage(locale)];
|
|
184
|
+
}
|
|
185
|
+
/** True when the language distinguishes formal and informal address. */
|
|
186
|
+
function hasFormalitySplit(locale) {
|
|
187
|
+
return !!specFor(locale);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* The formality configured for `locale`: the exact locale, else its base
|
|
191
|
+
* language (`de` covers `de-AT`). `auto` and unlisted locales: undefined.
|
|
192
|
+
*/
|
|
193
|
+
function formalityFor(settings, locale) {
|
|
194
|
+
if (!settings)
|
|
195
|
+
return undefined;
|
|
196
|
+
const lower = Object.fromEntries(Object.entries(settings).map(([k, v]) => [k.toLowerCase().replace(/_/g, '-'), v]));
|
|
197
|
+
const exact = lower[locale.toLowerCase().replace(/_/g, '-')];
|
|
198
|
+
const value = exact ?? lower[locale.toLowerCase().split(/[-_]/)[0]];
|
|
199
|
+
return value === 'formal' || value === 'informal' ? value : undefined;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* The prompt rule for the form of address. With a level set, it names the
|
|
203
|
+
* concrete forms to use and to avoid; on `auto` it still forbids mixing them.
|
|
204
|
+
* Undefined for languages without the distinction (and `auto`): the generic
|
|
205
|
+
* consistency rule applies.
|
|
206
|
+
*/
|
|
207
|
+
function formalityInstruction(locale, level, languageName) {
|
|
208
|
+
const spec = specFor(locale);
|
|
209
|
+
if (!spec)
|
|
210
|
+
return undefined;
|
|
211
|
+
if (!level) {
|
|
212
|
+
return `Address the user in one form throughout: either the formal ${spec.formal} or the informal ${spec.informal}, whichever is conventional for modern software in ${languageName} — never mix them.`;
|
|
213
|
+
}
|
|
214
|
+
const [use, avoid] = level === 'formal' ? [spec.formal, spec.informal] : [spec.informal, spec.formal];
|
|
215
|
+
return `Address the user ${level === 'formal' ? 'formally' : 'informally'} in every entry: use ${use}. Never use ${avoid}, even where a context or a previous translation does.`;
|
|
216
|
+
}
|
|
217
|
+
/** Which form a single string uses: undefined when it addresses no one (or both forms appear). */
|
|
218
|
+
function formalityOf(text, locale) {
|
|
219
|
+
const spec = specFor(locale);
|
|
220
|
+
if (!spec?.formalMarkers && !spec?.informalMarkers)
|
|
221
|
+
return undefined;
|
|
222
|
+
// Placeholders ({name}, {{name}}, tokens) and tags aren't words of the message.
|
|
223
|
+
const prose = text.replace(/\{\{?\s*[\w.-]+\s*\}?\}|<[^<>]*>|\[#[^\]]*#\]/g, ' ');
|
|
224
|
+
const formal = !!spec.formalMarkers?.test(prose);
|
|
225
|
+
const informal = !!spec.informalMarkers?.test(prose);
|
|
226
|
+
if (formal === informal)
|
|
227
|
+
return undefined;
|
|
228
|
+
return formal ? 'formal' : 'informal';
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* The form an existing catalog uses, when it clearly uses one: at least
|
|
232
|
+
* three strings address the user and 80% of them use the same form.
|
|
233
|
+
*/
|
|
234
|
+
function detectFormality(values, locale) {
|
|
235
|
+
let formal = 0;
|
|
236
|
+
let informal = 0;
|
|
237
|
+
for (const v of values) {
|
|
238
|
+
const form = formalityOf(v, locale);
|
|
239
|
+
if (form === 'formal')
|
|
240
|
+
formal++;
|
|
241
|
+
else if (form === 'informal')
|
|
242
|
+
informal++;
|
|
243
|
+
}
|
|
244
|
+
const total = formal + informal;
|
|
245
|
+
if (total < 3)
|
|
246
|
+
return undefined;
|
|
247
|
+
if (formal / total >= 0.8)
|
|
248
|
+
return 'formal';
|
|
249
|
+
if (informal / total >= 0.8)
|
|
250
|
+
return 'informal';
|
|
251
|
+
return undefined;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Strings of one locale that break its form of address: with `expected`, the
|
|
255
|
+
* ones using the other form; without it, the minority form when both appear
|
|
256
|
+
* (all of them on a tie). Key → message.
|
|
257
|
+
*/
|
|
258
|
+
function formalityIssues(values, locale, expected) {
|
|
259
|
+
const formOf = {};
|
|
260
|
+
let formal = 0;
|
|
261
|
+
let informal = 0;
|
|
262
|
+
for (const [key, value] of Object.entries(values)) {
|
|
263
|
+
const form = formalityOf(value, locale);
|
|
264
|
+
if (!form)
|
|
265
|
+
continue;
|
|
266
|
+
formOf[key] = form;
|
|
267
|
+
if (form === 'formal')
|
|
268
|
+
formal++;
|
|
269
|
+
else
|
|
270
|
+
informal++;
|
|
271
|
+
}
|
|
272
|
+
const out = {};
|
|
273
|
+
if (expected) {
|
|
274
|
+
for (const [key, form] of Object.entries(formOf)) {
|
|
275
|
+
if (form !== expected)
|
|
276
|
+
out[key] = `Uses ${form} address, but ${locale} is set to ${expected}`;
|
|
277
|
+
}
|
|
278
|
+
return out;
|
|
279
|
+
}
|
|
280
|
+
if (formal === 0 || informal === 0)
|
|
281
|
+
return out;
|
|
282
|
+
const flag = formal === informal ? ['formal', 'informal'] : [formal < informal ? 'formal' : 'informal'];
|
|
283
|
+
for (const [key, form] of Object.entries(formOf)) {
|
|
284
|
+
if (!flag.includes(form))
|
|
285
|
+
continue;
|
|
286
|
+
out[key] = `Mixes forms of address: uses ${form} address while ${formal === informal ? 'as many' : 'most'} strings in ${locale} use ${form === 'formal' ? 'informal' : 'formal'} (${formal} formal, ${informal} informal)`;
|
|
287
|
+
}
|
|
288
|
+
return out;
|
|
289
|
+
}
|
|
290
|
+
/** Parse `de=formal,it=informal` (CLI flag values); throws on a malformed item. */
|
|
291
|
+
function parseFormalityList(items) {
|
|
292
|
+
const out = {};
|
|
293
|
+
for (const item of items.flatMap((i) => i.split(',')).map((i) => i.trim()).filter(Boolean)) {
|
|
294
|
+
const m = /^([A-Za-z]{2,3}(?:[-_][A-Za-z0-9]{2,8})*)\s*[=:]\s*(formal|informal|auto)$/i.exec(item);
|
|
295
|
+
if (!m)
|
|
296
|
+
throw new Error(`Invalid formality "${item}": use <locale>=formal|informal|auto, e.g. de=formal`);
|
|
297
|
+
out[m[1]] = m[2].toLowerCase();
|
|
298
|
+
}
|
|
299
|
+
return out;
|
|
300
|
+
}
|
|
301
|
+
/** Keep valid `locale → formal|informal|auto` entries of untrusted input (config files, DB rows). */
|
|
302
|
+
function sanitizeFormality(raw) {
|
|
303
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
304
|
+
return undefined;
|
|
305
|
+
const out = {};
|
|
306
|
+
for (const [k, v] of Object.entries(raw)) {
|
|
307
|
+
if (!/^[A-Za-z]{2,3}(?:[-_][A-Za-z0-9]{2,8})*$/.test(k))
|
|
308
|
+
continue;
|
|
309
|
+
if (v === 'formal' || v === 'informal' || v === 'auto')
|
|
310
|
+
out[k] = v;
|
|
311
|
+
}
|
|
312
|
+
return Object.keys(out).length > 0 ? out : undefined;
|
|
313
|
+
}
|
|
@@ -2,6 +2,7 @@ import type { LocaleFile, WorkspaceInfo } from '../../runtime/types';
|
|
|
2
2
|
import type { ByokProvider } from '@babelize/types';
|
|
3
3
|
import { type GlossaryRules } from '../glossary';
|
|
4
4
|
import type { EngineProviders, ProjectRulesStore, TranslationFailure, TranslationCache } from '../../providers/types';
|
|
5
|
+
import { type FormalityLevel, type FormalitySetting } from './formality';
|
|
5
6
|
/** QA note on keys the backup (NLLB) translator produced. */
|
|
6
7
|
export declare const FALLBACK_QA_WARNING = "translated with the backup translator; will be retried with the AI translator next run";
|
|
7
8
|
export interface TranslateOptions {
|
|
@@ -43,9 +44,26 @@ export interface TranslateOptions {
|
|
|
43
44
|
export interface LoadedTranslationRules {
|
|
44
45
|
glossary: GlossaryRules;
|
|
45
46
|
brandVoice: string | null;
|
|
47
|
+
/** Configured form of address per locale (or base language); unlisted is auto. */
|
|
48
|
+
formality?: Record<string, FormalitySetting> | null;
|
|
49
|
+
/**
|
|
50
|
+
* Form of address found in a locale's existing translations (an adopted
|
|
51
|
+
* setup), used where the configured one is auto. Guides new translations
|
|
52
|
+
* only: unlike a configured one it doesn't re-translate stored keys.
|
|
53
|
+
*/
|
|
54
|
+
detectedFormality?: Record<string, FormalityLevel>;
|
|
46
55
|
/** False if the glossary or brand voice failed to load. */
|
|
47
56
|
complete: boolean;
|
|
48
57
|
}
|
|
58
|
+
/** The form of address translations into `locale` use: configured, else detected, else auto (undefined). */
|
|
59
|
+
export declare function effectiveFormality(rules: Pick<LoadedTranslationRules, 'formality' | 'detectedFormality'>, locale: string): FormalityLevel | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* The lockfile fingerprint of the project rules a translation was made
|
|
62
|
+
* under: the glossary's, plus the configured form of address. A key is
|
|
63
|
+
* re-translated when it changes. Auto adds nothing, so projects without a
|
|
64
|
+
* formality setting keep their fingerprints.
|
|
65
|
+
*/
|
|
66
|
+
export declare function rulesFingerprint(source: string, locale: string, rules: Pick<LoadedTranslationRules, 'glossary' | 'formality'>): string;
|
|
49
67
|
/**
|
|
50
68
|
* Load a project's glossary and brand voice. Never throws: a failure means
|
|
51
69
|
* translating without that rule, as before. The two lookups are independent,
|