@@ -3,6 +3,95 @@ import { ProxyAgent } from 'proxy-agent';
33import { options } from './config' ;
44import pLimit from 'p-limit' ;
55
6+ export async function translateTitle ( title : string , targetLanguage : string ) {
7+ const client = new OpenAI ( {
8+ baseURL : options . baseUrl ,
9+ apiKey : options . apiKey ,
10+ organization : options . organization ,
11+ httpAgent : new ProxyAgent ( ) ,
12+ } ) ;
13+
14+ const titlePrompt = `You are translating ONLY a document title (from YAML front matter) to ${ targetLanguage } .
15+
16+ 🎯 YOUR ONLY JOB: Decide if this title should be translated, then either translate it or return it unchanged.
17+
18+ ✅ TRANSLATE these types of titles (conceptual/navigational):
19+ • "Getting Started" → translate
20+ • "Overview" → translate
21+ • "Prerequisites" → translate
22+ • "Advanced Topics" → translate
23+ • "Configuration" → translate
24+ • "Architecture" → translate
25+ • "Security" → translate
26+ • "Routing" → translate
27+ • "Data Binding" → translate
28+ • "Validation" → translate
29+ • "Project Setup" → translate
30+ • "Creating a Basic App" → translate
31+ • "Working With Data" → translate
32+ • "Error Handling" → translate
33+ • "Debugging" → translate
34+ • "Modernization Tutorial" → translate
35+ • "Building UI" → translate
36+ • "Component Basics" → translate
37+ • "Composite Components" → translate
38+ • Any title containing: Guide, Tutorial, Setup, Installation, How to, Working with, Introduction
39+
40+ 🚫 DO NOT TRANSLATE these types of titles (component/code names):
41+ • "Button" → return "Button" unchanged (it's a Java class)
42+ • "TextField" → return "TextField" unchanged (it's a Java class)
43+ • "AppLayout" → return "AppLayout" unchanged (it's a Java class)
44+ • "FlexLayout" → return "FlexLayout" unchanged (it's a Java class)
45+ • "CheckBox" → return "CheckBox" unchanged (it's a Java class)
46+ • "Dialog" → return "Dialog" unchanged (it's a Java class)
47+ • "Table" → return "Table" unchanged (it's a Java class)
48+ • "Toast" → return "Toast" unchanged (it's a Java class)
49+ • "<dwc-button>" → return "<dwc-button>" unchanged (it's a web component tag)
50+ • "<dwc-anything>" → return unchanged (all web component tags)
51+ • Any single-word or CamelCase title that looks like a Java class name
52+ • Any title wrapped in angle brackets < >
53+
54+ 📋 RULES:
55+ 1. Return ONLY the title text (translated or unchanged)
56+ 2. NO explanations, NO extra text, NO quotes
57+ 3. NO YAML formatting, NO "title:" prefix
58+ 4. DO NOT include sidebar_position or any other front matter fields
59+ 5. If it's a component name, return it EXACTLY as given
60+ 6. If it's a conceptual term, translate it naturally
61+ 7. When in doubt, look for these clues:
62+ - Multiple common English words (the, and, with, to) → probably translate
63+ - CamelCase single word → probably don't translate
64+ - Contains "Guide", "Tutorial", "Setup" → definitely translate
65+
66+ ⚠️ CRITICAL: You are translating ONLY the title value. The system handles all front matter fields (sidebar_position, sidebar_class_name, etc.). Return ONLY the translated title text.
67+
68+ EXAMPLES:
69+ Input: "Getting Started" → Output: "Comenzando" (for Spanish)
70+ Input: "Button" → Output: "Button"
71+ Input: "Advanced Topics" → Output: "Temas Avanzados" (for Spanish)
72+ Input: "AppLayout" → Output: "AppLayout"
73+ Input: "<dwc-button>" → Output: "<dwc-button>"
74+ Input: "Working With Data" → Output: "Trabajando con Datos" (for Spanish)
75+
76+ Return ONLY the title (translated or unchanged), nothing else.` ;
77+
78+ const chatCompletion = await client . chat . completions . create ( {
79+ messages : [
80+ {
81+ role : 'system' ,
82+ content : titlePrompt ,
83+ } ,
84+ { role : 'user' , content : title } ,
85+ ] ,
86+ model : options . model ,
87+ } ) ;
88+
89+ return {
90+ translatedText : chatCompletion . choices [ 0 ] . message . content ?. trim ( ) ?? title ,
91+ usage : chatCompletion . usage ,
92+ } ;
93+ }
94+
695export async function translate ( content : string , targetLanguage : string , isUIString : boolean = false ) {
796 const client = new OpenAI ( {
897 baseURL : options . baseUrl ,
@@ -11,12 +100,24 @@ export async function translate(content: string, targetLanguage: string, isUIStr
11100 httpAgent : new ProxyAgent ( ) ,
12101 } ) ;
13102
14- const systemPrompt = isUIString
15- ? `You are a UI translator. Translate the following short UI text to ${ targetLanguage } .
103+ const systemPrompt = isUIString
104+ ? `You are a UI translator. Translate the following short UI text to ${ targetLanguage } .
16105 Keep the translation concise and appropriate for UI elements like buttons, menu items, and labels.
17106 Do not translate technical terms or brand names like: webforJ, DWC, BASIS, startforJ, HueCraft, Blog, JavaDocs.
18107 Return ONLY the translated text, nothing else.`
19- : `You are translating technical documentation to ${ targetLanguage } .
108+ : `You are translating technical documentation to ${ targetLanguage } .
109+
110+ ⚠️ CRITICAL: FRONT MATTER HANDLING ⚠️
111+ • The document's YAML front matter (title, sidebar_position, sidebar_class_name, slug, etc.) has been REMOVED before being sent to you
112+ • You will ONLY receive the document body/content to translate
113+ • DO NOT add, create, or include ANY front matter in your response
114+ • DO NOT output --- markers or any YAML fields
115+ • DO NOT include title:, sidebar_position:, or any other front matter fields
116+ • Return ONLY the translated document body content
117+ • Front matter fields are handled separately by the system - DO NOT modify or include them
118+
119+ Example of what you receive: Just the markdown body without front matter
120+ Example of what you return: Just the translated markdown body without front matter
20121
21122⚠️ CRITICAL FAILURE PREVENTION ⚠️
22123If you translate ANY of the following, the build will FAIL:
0 commit comments