@babelize/engine 0.3.15 → 0.3.16

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.
@@ -0,0 +1,15 @@
1
+ import type { GlossaryRules } from './rules';
2
+ /**
3
+ * The app's own name, kept as written in every language (NLLB turned
4
+ * "Tasklane" into "pour les enfants"). Read from the project, never guessed
5
+ * from the strings:
6
+ *
7
+ * - an explicit app name (Expo `app.json` `expo.name`, a web manifest
8
+ * `name`/`short_name`) counts on its own;
9
+ * - the start of the page title ("Tasklane: the to-do list…", `metadata.title`
10
+ * or `<title>`) counts only when `package.json`'s name agrees, so a title
11
+ * like "Dashboard | Acme" can't put "Dashboard" on the never-translate list.
12
+ */
13
+ export declare function detectBrandNames(workspacePath: string): string[];
14
+ /** `glossary` with the brand names added to its never-translate terms (unless the glossary already rules on them). */
15
+ export declare function withBrandNames(glossary: GlossaryRules, brandNames: readonly string[]): GlossaryRules;
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.detectBrandNames = detectBrandNames;
37
+ exports.withBrandNames = withBrandNames;
38
+ const fs = __importStar(require("fs"));
39
+ const path = __importStar(require("path"));
40
+ /**
41
+ * The app's own name, kept as written in every language (NLLB turned
42
+ * "Tasklane" into "pour les enfants"). Read from the project, never guessed
43
+ * from the strings:
44
+ *
45
+ * - an explicit app name (Expo `app.json` `expo.name`, a web manifest
46
+ * `name`/`short_name`) counts on its own;
47
+ * - the start of the page title ("Tasklane: the to-do list…", `metadata.title`
48
+ * or `<title>`) counts only when `package.json`'s name agrees, so a title
49
+ * like "Dashboard | Acme" can't put "Dashboard" on the never-translate list.
50
+ */
51
+ function detectBrandNames(workspacePath) {
52
+ const names = new Set();
53
+ for (const name of explicitAppNames(workspacePath))
54
+ names.add(name);
55
+ const packageName = readJson(path.join(workspacePath, 'package.json'))?.name;
56
+ const packageWords = typeof packageName === 'string' ? nameWords(packageName.replace(/^@[^/]+\//, '')) : [];
57
+ const title = pageTitle(workspacePath);
58
+ const titleName = title ? titlePrefix(title) : null;
59
+ if (titleName && packageWords.length > 0 && packageAgrees(titleName, packageWords))
60
+ names.add(titleName);
61
+ return [...names];
62
+ }
63
+ /** `glossary` with the brand names added to its never-translate terms (unless the glossary already rules on them). */
64
+ function withBrandNames(glossary, brandNames) {
65
+ const ruled = new Set([...glossary.neverTranslate, ...glossary.alwaysTranslateAs.map((e) => e.term)].map((t) => t.toLowerCase()));
66
+ const added = brandNames.filter((name) => !ruled.has(name.toLowerCase()));
67
+ return added.length === 0 ? glossary : { ...glossary, neverTranslate: [...glossary.neverTranslate, ...added] };
68
+ }
69
+ const MAX_BRAND_LENGTH = 40;
70
+ function explicitAppNames(root) {
71
+ const found = [];
72
+ const expo = readJson(path.join(root, 'app.json'))?.expo;
73
+ if (expo && typeof expo === 'object')
74
+ found.push(expo.name);
75
+ for (const file of ['public/manifest.json', 'public/site.webmanifest', 'public/manifest.webmanifest', 'app/manifest.json']) {
76
+ const manifest = readJson(path.join(root, file));
77
+ if (manifest)
78
+ found.push(manifest.short_name, manifest.name);
79
+ }
80
+ return found.filter(isBrandLike).map((name) => name.trim());
81
+ }
82
+ /** The page title: Next `metadata.title` (string or `default`) in the root layout, else an HTML `<title>`. */
83
+ function pageTitle(root) {
84
+ for (const dir of ['app', 'src/app']) {
85
+ for (const ext of ['tsx', 'ts', 'jsx', 'js']) {
86
+ const source = readText(path.join(root, dir, `layout.${ext}`));
87
+ const match = source?.match(/\btitle\s*:\s*(?:\{[^}]*?\bdefault\s*:\s*)?(["'`])([^"'`\n]+)\1/);
88
+ if (match)
89
+ return match[2];
90
+ }
91
+ }
92
+ for (const file of ['index.html', 'public/index.html']) {
93
+ const match = readText(path.join(root, file))?.match(/<title>([^<]+)<\/title>/i);
94
+ if (match)
95
+ return match[1];
96
+ }
97
+ return null;
98
+ }
99
+ /** "Tasklane: the to-do list" → "Tasklane"; a title with no separator is the name only if it's short. */
100
+ function titlePrefix(title) {
101
+ const parts = title.split(/\s*(?::|\||—|–|·|\s-\s)\s*/);
102
+ const head = parts[0].trim();
103
+ if (parts.length === 1 && head.split(/\s+/).length > 2)
104
+ return null;
105
+ return isBrandLike(head) ? head : null;
106
+ }
107
+ /** "tasklane-sample" agrees with "Tasklane"; "my-web-app" agrees with nothing. */
108
+ function packageAgrees(name, packageWords) {
109
+ const compact = name.toLowerCase().replace(/[^\p{L}\p{N}]/gu, '');
110
+ if (compact.length < 3)
111
+ return false;
112
+ const words = packageWords.filter((w) => !GENERIC_PACKAGE_WORDS.has(w));
113
+ return words.includes(compact) || words.join('') === compact;
114
+ }
115
+ const GENERIC_PACKAGE_WORDS = new Set([
116
+ 'app', 'apps', 'web', 'www', 'site', 'website', 'frontend', 'client', 'ui', 'sample', 'example', 'demo',
117
+ 'starter', 'template', 'my', 'next', 'nextjs', 'react', 'vue', 'vite', 'landing', 'page', 'dashboard',
118
+ ]);
119
+ function nameWords(name) {
120
+ return name.toLowerCase().split(/[^\p{L}\p{N}]+/u).filter(Boolean);
121
+ }
122
+ function isBrandLike(value) {
123
+ if (typeof value !== 'string')
124
+ return false;
125
+ const v = value.trim();
126
+ return v.length >= 2 && v.length <= MAX_BRAND_LENGTH && /\p{L}/u.test(v) && v.split(/\s+/).length <= 3;
127
+ }
128
+ function readText(file) {
129
+ try {
130
+ return fs.readFileSync(file, 'utf8');
131
+ }
132
+ catch {
133
+ return null;
134
+ }
135
+ }
136
+ function readJson(file) {
137
+ const text = readText(file);
138
+ if (!text)
139
+ return null;
140
+ try {
141
+ const value = JSON.parse(text);
142
+ return value && typeof value === 'object' ? value : null;
143
+ }
144
+ catch {
145
+ return null;
146
+ }
147
+ }
@@ -19,6 +19,7 @@ const concurrent_map_1 = require("../../utils/concurrent-map");
19
19
  const endonyms_1 = require("../../utils/endonyms");
20
20
  const language_codes_1 = require("./language-codes");
21
21
  const glossary_1 = require("../glossary");
22
+ const brand_1 = require("../glossary/brand");
22
23
  const check_project_1 = require("../../qa/check-project");
23
24
  const gemini_provider_1 = require("./client/gemini.provider");
24
25
  const memory_matcher_1 = require("./memory/memory-matcher");
@@ -288,7 +289,11 @@ async function translateLocales(workspace, sourceLocale, targetLocales, forceTra
288
289
  ?? (projectId
289
290
  ? await loadProjectTranslationRules(projectId, providers.rules)
290
291
  : await loadWorkspaceRules(workspace.path));
291
- const glossary = rules.glossary;
292
+ // The app's own name joins the never-translate terms for this run.
293
+ const brandNames = (0, brand_1.detectBrandNames)(workspace.path);
294
+ if (brandNames.length > 0)
295
+ logger_1.engineLogger.info('Keeping the app name as written', { brandNames });
296
+ const glossary = (0, brand_1.withBrandNames)(rules.glossary, brandNames);
292
297
  const neverTranslateTerms = glossary.neverTranslate;
293
298
  const brandVoice = rules.brandVoice;
294
299
  const rulesComplete = rules.complete;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@babelize/engine",
3
- "version": "0.3.15",
3
+ "version": "0.3.16",
4
4
  "description": "Babelize translation pipeline (extract, keygen, translate, inject). Infrastructure is injected through provider/store interfaces.",
5
5
  "keywords": [
6
6
  "babelize",