Skip to content

Commit 4b7326e

Browse files
committed
feat: update openai prompt
1 parent c8dc1e6 commit 4b7326e

File tree

4 files changed

+161
-43
lines changed

4 files changed

+161
-43
lines changed

apps/docs/translation.config.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export default {
22
langs: {
33
'zh-hans': {
4+
locale: 'zh-hans',
45
name: 'Simplified Chinese',
56
// 翻译规则和指南
67
guide: `
@@ -14,6 +15,7 @@ export default {
1415
terms: {},
1516
},
1617
'zh-hant': {
18+
locale: 'zh-hant',
1719
name: 'Traditional Chinese',
1820
// 翻譯規則和指南
1921
guide: `
@@ -27,6 +29,7 @@ export default {
2729
terms: {},
2830
},
2931
ja: {
32+
locale: 'ja',
3033
name: 'Japanese',
3134
guide: `
3235
- For technical terms that should not be fully translated, use the format: "日本語訳 (English term)"
@@ -37,6 +40,7 @@ export default {
3740
terms: {},
3841
},
3942
es: {
43+
locale: 'es',
4044
name: 'Spanish',
4145
guide: `
4246
- For technical terms that should not be fully translated, use the format: "Traducción en español (English term)"
@@ -47,6 +51,7 @@ export default {
4751
terms: {},
4852
},
4953
de: {
54+
locale: 'de',
5055
name: 'German',
5156
guide: `
5257
- For technical terms that should not be fully translated, use the format: "Deutsche Übersetzung (English term)"
@@ -57,6 +62,7 @@ export default {
5762
terms: {},
5863
},
5964
fr: {
65+
locale: 'fr',
6066
name: 'French',
6167
guide: `
6268
- For technical terms that should not be fully translated, use the format: "Traduction française (English term)"
@@ -67,6 +73,7 @@ export default {
6773
terms: {},
6874
},
6975
ru: {
76+
locale: 'ru',
7077
name: 'Russian',
7178
guide: `
7279
- For technical terms that should not be fully translated, use the format: "Русский перевод (English term)"
@@ -77,6 +84,7 @@ export default {
7784
terms: {},
7885
},
7986
ar: {
87+
locale: 'ar',
8088
name: 'Arabic',
8189
guide: `
8290
- For technical terms that should not be fully translated, use the format: "الترجمة العربية (English term)"

packages/translate/src/openai.ts

Lines changed: 142 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { type Usage, addUsage } from './usage';
1111

1212
interface LangConfig {
1313
name: string;
14+
locale: string;
1415
}
1516

1617
interface TranslateDocumentParams {
@@ -52,87 +53,189 @@ async function translateChunk(
5253
const textLength = chunk.length;
5354
const systemPrompt = `You are a professional technical translator from English to ${langConfig.name} specializing in software documentation. You are particularly skilled at translating React, web development, and programming terminology, keeping the translations consistent and readable.`;
5455

56+
// Generate language-specific examples
57+
const getLanguageSpecificExamples = (
58+
locale: string,
59+
): { correct: string[]; incorrect: string[] } => {
60+
const examples: Record<string, { correct: string[]; incorrect: string[] }> =
61+
{
62+
'zh-hans': {
63+
correct: [
64+
'title: 静态资源(\\`public\\` 目录)',
65+
'description: API \\`getStaticProps\\` 参考文档。了解如何使用 \\`getStaticProps\\`。',
66+
'description: 关于 \\`<Link>\\` 组件的 API 参考文档。',
67+
'description: React \\`useState\\` 钩子的使用示例',
68+
],
69+
incorrect: [
70+
'title: \\`public\\` 目录中的静态资源',
71+
'description: \\`getStaticProps\\` API 参考文档。了解如何使用 \\`getStaticProps\\`。',
72+
'description: \\`<Link>\\` 组件的 API 参考文档。',
73+
'description: \\`useState\\` 钩子的使用示例',
74+
],
75+
},
76+
'zh-hant': {
77+
correct: [
78+
'title: 靜態資源(\\`public\\` 目錄)',
79+
'description: API \\`getStaticProps\\` 參考文檔。了解如何使用 \\`getStaticProps\\`。',
80+
'description: 關於 \\`<Link>\\` 組件的 API 參考文檔。',
81+
'description: React \\`useState\\` 鉤子的使用示例',
82+
],
83+
incorrect: [
84+
'title: \\`public\\` 目錄中的靜態資源',
85+
'description: \\`getStaticProps\\` API 參考文檔。了解如何使用 \\`getStaticProps\\`。',
86+
'description: \\`<Link>\\` 組件的 API 參考文檔。',
87+
'description: \\`useState\\` 鉤子的使用示例',
88+
],
89+
},
90+
ja: {
91+
correct: [
92+
'title: 静的アセット(\\`public\\` ディレクトリ)',
93+
'description: API \\`getStaticProps\\` のリファレンス。\\`getStaticProps\\` の使用方法を学びます。',
94+
'description: React \\`<Link>\\` コンポーネントについての API リファレンス。',
95+
'description: React \\`useState\\` フックの使用例',
96+
],
97+
incorrect: [
98+
'title: \\`public\\` ディレクトリの静的アセット',
99+
'description: \\`getStaticProps\\` API のリファレンス。\\`getStaticProps\\` の使用方法を学びます。',
100+
'description: \\`<Link>\\` コンポーネントの API リファレンス。',
101+
'description: \\`useState\\` フックの使用例',
102+
],
103+
},
104+
ru: {
105+
correct: [
106+
'description: Справочник API \\`getStaticProps\\`. Узнайте, как использовать \\`getStaticProps\\`.',
107+
'description: Справочник API для компонента \\`<Link>\\`.',
108+
'title: Статические ресурсы (директория \\`public\\`)',
109+
'description: Примеры использования хука \\`useState\\` в React',
110+
],
111+
incorrect: [
112+
'title: \\`public\\` директория статических ресурсов',
113+
'description: \\`getStaticProps\\` справочник API. Узнайте, как использовать \\`getStaticProps\\`.',
114+
'description: \\`<Link>\\` справочник API компонента.',
115+
'description: \\`useState\\` примеры использования хука',
116+
],
117+
},
118+
de: {
119+
correct: [
120+
'title: Statische Assets (\\`public\\`-Verzeichnis)',
121+
'description: API-Referenz für \\`getStaticProps\\`. Erfahren Sie, wie Sie \\`getStaticProps\\` verwenden.',
122+
'description: API-Referenz für die \\`<Link>\\`-Komponente.',
123+
'description: React \\`useState\\`-Hook Verwendungsbeispiele',
124+
],
125+
incorrect: [
126+
'title: \\`public\\`-Verzeichnis statische Assets',
127+
'description: \\`getStaticProps\\` API-Referenz. Erfahren Sie, wie Sie \\`getStaticProps\\` verwenden.',
128+
'description: \\`<Link>\\`-Komponente API-Referenz.',
129+
'description: \\`useState\\`-Hook Verwendungsbeispiele',
130+
],
131+
},
132+
fr: {
133+
correct: [
134+
'title: Ressources statiques (répertoire \\`public\\`)',
135+
'description: Référence API pour \\`getStaticProps\\`. Apprenez à utiliser \\`getStaticProps\\`.',
136+
'description: Référence API pour le composant \\`<Link>\\`.',
137+
"description: Exemples d'utilisation du hook \\`useState\\` de React",
138+
],
139+
incorrect: [
140+
'title: \\`public\\` répertoire des ressources statiques',
141+
'description: \\`getStaticProps\\` référence API. Apprenez à utiliser \\`getStaticProps\\`.',
142+
'description: \\`<Link>\\` référence API du composant.',
143+
"description: \\`useState\\` exemples d'utilisation du hook",
144+
],
145+
},
146+
es: {
147+
correct: [
148+
'title: Recursos estáticos (directorio \\`public\\`)',
149+
'description: Referencia de API para \\`getStaticProps\\`. Aprende a usar \\`getStaticProps\\`.',
150+
'description: Referencia de API para el componente \\`<Link>\\`.',
151+
'description: Ejemplos de uso del hook \\`useState\\` de React',
152+
],
153+
incorrect: [
154+
'title: \\`public\\` directorio de recursos estáticos',
155+
'description: \\`getStaticProps\\` referencia de API. Aprende a usar \\`getStaticProps\\`.',
156+
'description: \\`<Link>\\` referencia de API del componente.',
157+
'description: \\`useState\\` ejemplos de uso del hook',
158+
],
159+
},
160+
ar: {
161+
correct: [
162+
'title: الموارد الثابتة (دليل \\`public\\`)',
163+
'description: مرجع API لـ \\`getStaticProps\\`. تعلم كيفية استخدام \\`getStaticProps\\`.',
164+
'description: مرجع API لمكون \\`<Link>\\`.',
165+
'description: أمثلة استخدام خطاف \\`useState\\` في React',
166+
],
167+
incorrect: [
168+
'title: \\`public\\` دليل الموارد الثابتة',
169+
'description: \\`getStaticProps\\` مرجع API. تعلم كيفية استخدام \\`getStaticProps\\`.',
170+
'description: \\`<Link>\\` مرجع API للمكون.',
171+
'description: \\`useState\\` أمثلة استخدام الخطاف',
172+
],
173+
},
174+
};
175+
176+
return examples[locale] || examples['zh-hans'];
177+
};
178+
179+
const langExamples = getLanguageSpecificExamples(langConfig.locale);
180+
55181
const frontmatterRules = `⚠️ CRITICAL MDX FRONTMATTER RULES ⚠️
56182
• NEVER start a frontmatter value with inline code (text between \`backticks\`)
57183
• This applies to ALL inline code including \`<Component>\` tags, \`functions\`, variables, etc.
58184
• In frontmatter (sections between --- marks), ALWAYS rearrange sentences so inline code appears AFTER some text
59-
• If a frontmatter value would start with inline code after translation, ALWAYS rewrite the sentence so that some descriptive text comes before the inline code. For example, instead of \`title: \`public\` 目錄中的靜態資源\`, use \`title: 靜態資源(\`public\` 目錄)\` or \`title: 關於 public 目錄的靜態資源\`.
185+
• If a frontmatter value would start with inline code after translation, ALWAYS rewrite the sentence so that some descriptive text comes before the inline code. For example, instead of ${langExamples.incorrect[0]}, use ${langExamples.correct[0]}.
60186
• This rule applies to ALL frontmatter keys (title, description, etc.), not just description.
61187
• ALWAYS preserve both the opening and closing frontmatter delimiters (---) - never omit the closing delimiter
62188
• Maintain the exact structure of frontmatter - beginning with ---, followed by key-value pairs, and ending with ---
63189
• These rules are ABSOLUTELY REQUIRED for proper rendering of the documentation
64190
65191
Examples of MDX Frontmatter Translation:
66192
67-
# Example 1: Function names
193+
# Example 1: Never start frontmatter values with inline code
68194
✓ CORRECT (Always do this):
69195
Original:
196+
title: \`public\` directory static assets
70197
description: API reference for \`getStaticProps\`. Learn how to use \`getStaticProps\`.
71-
Translation:
72-
description: API \`getStaticProps\` 参考文档。了解如何使用 \`getStaticProps\`。
73-
74-
✗ INCORRECT (Never do this):
75-
Original:
76-
description: API reference for \`getStaticProps\`. Learn how to use \`getStaticProps\`.
77-
Translation:
78-
description: \`getStaticProps\` API 参考文档。了解如何使用 \`getStaticProps\`。
79-
80-
# Example 2: HTML/Component tags
81-
✓ CORRECT (Always do this):
82-
Original:
83198
description: API reference for the \`<Link>\` component.
199+
description: \`useState\` hook usage examples
84200
Translation:
85-
description: 关于 \`<Link>\` 组件的 API 参考文档。
201+
${langExamples.correct.map((ex) => ` ${ex}`).join('\n')}
86202
87203
✗ INCORRECT (Never do this):
88204
Original:
205+
title: \`public\` directory static assets
206+
description: API reference for \`getStaticProps\`. Learn how to use \`getStaticProps\`.
89207
description: API reference for the \`<Link>\` component.
208+
description: \`useState\` hook usage examples
90209
Translation:
91-
description: \`<Link>\` 组件的 API 参考文档。
210+
${langExamples.incorrect.map((ex) => ` ${ex}`).join('\n')}
92211
93-
# Example 3: Frontmatter Structure
212+
# Example 2: Frontmatter Structure - always include closing delimiter
94213
✓ CORRECT (Always do this):
95214
Original:
96215
---
97-
title: Link
98-
description: API reference for the \`<Link>\` component.
216+
title: \`public\` directory static assets
99217
---
100218
Translation:
101219
---
102-
title: Link
103-
description: 关于 \`<Link>\` 组件的 API 参考文档。
220+
${langExamples.correct[0]}
104221
---
105222
106223
✗ INCORRECT (Never do this):
107224
Original:
108225
---
109-
title: Link
110-
description: API reference for the \`<Link>\` component.
226+
title: \`public\` directory static assets
111227
---
112228
Translation:
113229
---
114-
title: Link
115-
description: 关于 \`<Link>\` 组件的 API 参考文档。
230+
${langExamples.correct[0]}
116231
(missing closing delimiter)
117232
118-
# Example 4: Do NOT start with inline code in any frontmatter key
119-
✓ CORRECT (Always do this):
120-
Original:
121-
title: \`public\` directory static assets
122-
Translation:
123-
title: 靜態資源(public 目錄)
124-
title: 關於 public 目錄的靜態資源
125-
126-
✗ INCORRECT (Never do this):
127-
Original:
128-
title: \`public\` directory static assets
129-
Translation:
130-
title: \`public\` 目錄中的靜態資源
131-
132233
These rules apply ONLY to frontmatter (between --- marks) and are CRITICAL for proper document rendering.
133234
REMEMBER: You must NEVER start with inline code in frontmatter values and ALWAYS include both opening and closing --- delimiters.`;
134235

135236
const prompt = `
237+
${context}
238+
136239
Please read the following rules carefully:
137240
138241
General rules:
@@ -153,8 +256,6 @@ Output format:
153256
• No introduction, prefixes, or meta-explanations
154257
• Just the translation itself
155258
156-
${context}
157-
158259
⚠️ IMPORTANT: The next message contains the COMPLETE original text that needs to be translated. Please translate the ENTIRE content without omitting any parts, sections, or formatting elements. Preserve all whitespace, line breaks, and structural formatting exactly as they appear in the original, PLEASE TRANSLATE IT TO ${langConfig.name}:
159260
`;
160261

packages/translate/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export interface LangConfig {
2+
locale: string;
23
name: string;
34
guide?: string;
45
terms?: Record<string, string>;

packages/translate/translation.config.example.mjs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export default {
22
langs: {
3-
'zh-Hans': {
3+
'zh-hans': {
4+
locale: 'zh-hans',
45
name: 'Simplified Chinese',
56
// 翻译规则和指南
67
guide: `
@@ -13,7 +14,8 @@ export default {
1314
// 格式: 'English term': '中文翻译'
1415
terms: {},
1516
},
16-
'zh-Hant': {
17+
'zh-hant': {
18+
locale: 'zh-hant',
1719
name: 'Traditional Chinese',
1820
// 翻譯規則和指南
1921
guide: `
@@ -27,6 +29,7 @@ export default {
2729
terms: {},
2830
},
2931
ja: {
32+
locale: 'ja',
3033
name: 'Japanese',
3134
guide: `
3235
- For technical terms that should not be fully translated, use the format: "日本語訳 (English term)"
@@ -37,6 +40,7 @@ export default {
3740
terms: {},
3841
},
3942
es: {
43+
locale: 'es',
4044
name: 'Spanish',
4145
guide: `
4246
- For technical terms that should not be fully translated, use the format: "Traducción en español (English term)"
@@ -47,6 +51,7 @@ export default {
4751
terms: {},
4852
},
4953
de: {
54+
locale: 'de',
5055
name: 'German',
5156
guide: `
5257
- For technical terms that should not be fully translated, use the format: "Deutsche Übersetzung (English term)"
@@ -57,6 +62,7 @@ export default {
5762
terms: {},
5863
},
5964
fr: {
65+
locale: 'fr',
6066
name: 'French',
6167
guide: `
6268
- For technical terms that should not be fully translated, use the format: "Traduction française (English term)"
@@ -67,6 +73,7 @@ export default {
6773
terms: {},
6874
},
6975
ru: {
76+
locale: 'ru',
7077
name: 'Russian',
7178
guide: `
7279
- For technical terms that should not be fully translated, use the format: "Русский перевод (English term)"
@@ -77,6 +84,7 @@ export default {
7784
terms: {},
7885
},
7986
ar: {
87+
locale: 'ar',
8088
name: 'Arabic',
8189
guide: `
8290
- For technical terms that should not be fully translated, use the format: "الترجمة العربية (English term)"

0 commit comments

Comments
 (0)