Add special handling for legal document translations: Introduced new constants for legal files and system prompts in translate-i18n.js. Enhanced translation functions to accommodate legal document requirements, ensuring preservation of German legal terms and structure during translations.

This commit is contained in:
sebseb7
2025-07-16 11:49:37 +02:00
parent a7cfbce072
commit c078abec1a
16 changed files with 466 additions and 5 deletions

View File

@@ -34,6 +34,15 @@ const TRANSLATION_FILES = [
'common.js'
];
// Legal document files that need special translation handling
const LEGAL_FILES = [
'legal-agb.js',
'legal-datenschutz.js',
'legal-impressum.js',
'legal-widerruf.js',
'legal-batterie.js'
];
// Model configuration
const GERMAN_TO_ENGLISH_MODEL = 'gpt-4.1'; // High-quality model for German -> English (critical step)
const ENGLISH_TO_OTHER_MODEL = 'gpt-4.1-mini'; // Faster/cheaper model for English -> Other languages
@@ -96,6 +105,37 @@ Examples:
DO NOT output any string without its German comment. Every single translated string needs the German original as a comment.
`;
// Special system prompt for legal documents (German to English)
const LEGAL_GERMAN_TO_ENGLISH_SYSTEM_PROMPT = `
You MUST translate German legal document strings to English AND add the original German text as a comment after EVERY translated string.
CRITICAL LEGAL DOCUMENT REQUIREMENTS:
- This is a legal document for a GERMAN COMPANY (Growheads) operating under GERMAN LAW
- All legal terms, regulations, and jurisdictions remain GERMAN even when translated
- Company name "Growheads" and German address must remain unchanged
- All references to German laws (DSGVO, BGB, etc.) must remain as German legal references
- German court jurisdiction and German legal framework must be preserved
- The translation is for INFORMATIONAL PURPOSES to help international customers understand the German legal terms
Rules:
1. Translate all German strings to English for comprehension
2. MANDATORY: Add the original German text as a comment after EVERY translated string using // format
3. Preserve all existing comments from the German version
4. Maintain the exact JavaScript object structure and formatting
5. Keep all interpolation variables unchanged
6. Keep company name "Growheads" unchanged
7. Keep German address unchanged
8. Keep German law references (DSGVO, BGB, etc.) unchanged
9. Keep German court jurisdiction references unchanged
10. Preserve any special formatting or HTML entities
11. Return a valid JavaScript object (not JSON) that can be exported
MANDATORY FORMAT for every string:
"englishTranslation": "English Translation", // Original German Text
IMPORTANT: The English translation should help international customers understand the German legal terms, but the legal validity remains under German law.
`;
// System prompt template for English to other languages (file content will be inserted)
const ENGLISH_TO_OTHER_SYSTEM_PROMPT_TEMPLATE = `
Translate the English strings in the following file to {{targetLanguage}}, preserving the German comments.
@@ -115,6 +155,37 @@ Here is the English translation file to translate:
{{englishFileContent}}
`;
// Special system prompt for legal documents (English to other languages)
const LEGAL_ENGLISH_TO_OTHER_SYSTEM_PROMPT_TEMPLATE = `
Translate the English legal document strings to {{targetLanguage}} while preserving the German legal framework.
CRITICAL LEGAL DOCUMENT REQUIREMENTS:
- This is a legal document for a GERMAN COMPANY (Growheads) operating under GERMAN LAW
- All legal terms, regulations, and jurisdictions remain GERMAN even when translated to {{targetLanguage}}
- Company name "Growheads" and German address must remain unchanged
- All references to German laws (DSGVO, BGB, etc.) must remain as German legal references
- German court jurisdiction and German legal framework must be preserved
- The translation is for INFORMATIONAL PURPOSES to help {{targetLanguage}} speakers understand the German legal terms
Rules:
1. Translate only the English strings to {{targetLanguage}}
2. Drop the comments in output
3. Maintain the exact JavaScript object structure and formatting
4. Keep all interpolation variables unchanged
5. Keep company name "Growheads" unchanged
6. Keep German address unchanged
7. Keep German law references (DSGVO, BGB, etc.) unchanged
8. Keep German court jurisdiction references unchanged
9. Preserve any special formatting or HTML entities
10. Return a valid JavaScript object (not JSON) that can be exported
IMPORTANT: The {{targetLanguage}} translation should help speakers understand the German legal terms, but the legal validity remains under German law.
Here is the English legal document translation file to translate:
{{englishFileContent}}
`;
// Function to check if source file is newer than target file
function isSourceNewer(sourcePath, targetPath) {
try {
@@ -137,6 +208,7 @@ function isSourceNewer(sourcePath, targetPath) {
function getFilesNeedingEnglishTranslation() {
const filesToTranslate = [];
// Check regular translation files
for (const fileName of TRANSLATION_FILES) {
const germanFile = path.join(GERMAN_DIR, fileName);
const englishFile = path.join(ENGLISH_DIR, fileName);
@@ -154,6 +226,24 @@ function getFilesNeedingEnglishTranslation() {
}
}
// Check legal files
for (const fileName of LEGAL_FILES) {
const germanFile = path.join(GERMAN_DIR, fileName);
const englishFile = path.join(ENGLISH_DIR, fileName);
if (!fs.existsSync(germanFile)) {
console.log(`⚠️ German legal file not found: ${fileName}`);
continue;
}
if (isSourceNewer(germanFile, englishFile)) {
filesToTranslate.push(fileName);
console.log(`📋 ${fileName} needs German → English legal translation`);
} else {
console.log(`⏭️ ${fileName} is up to date (German → English legal)`);
}
}
return filesToTranslate;
}
@@ -162,6 +252,7 @@ function getFilesNeedingTargetTranslation(langCode) {
const filesToTranslate = [];
const targetDir = path.join(LOCALES_DIR, langCode);
// Check regular translation files
for (const fileName of TRANSLATION_FILES) {
const englishFile = path.join(ENGLISH_DIR, fileName);
const targetFile = path.join(targetDir, fileName);
@@ -179,6 +270,24 @@ function getFilesNeedingTargetTranslation(langCode) {
}
}
// Check legal files
for (const fileName of LEGAL_FILES) {
const englishFile = path.join(ENGLISH_DIR, fileName);
const targetFile = path.join(targetDir, fileName);
if (!fs.existsSync(englishFile)) {
console.log(`⚠️ English legal file not found: ${fileName}`);
continue;
}
if (isSourceNewer(englishFile, targetFile)) {
filesToTranslate.push(fileName);
console.log(`📋 ${fileName} needs English → ${langCode} legal translation`);
} else {
console.log(`⏭️ ${fileName} is up to date (English → ${langCode} legal)`);
}
}
return filesToTranslate;
}
@@ -244,10 +353,15 @@ async function translateContent(content, systemPrompt, targetLanguage = null, mo
}
// Function to translate English to other languages (optimized for caching)
async function translateToTargetLanguage(englishContent, targetLanguage, model = 'gpt-4o-mini') {
async function translateToTargetLanguage(englishContent, targetLanguage, model = 'gpt-4o-mini', isLegalFile = false) {
try {
// Choose appropriate system prompt based on file type
const promptTemplate = isLegalFile
? LEGAL_ENGLISH_TO_OTHER_SYSTEM_PROMPT_TEMPLATE
: ENGLISH_TO_OTHER_SYSTEM_PROMPT_TEMPLATE;
// Create system prompt with file content (cacheable)
const systemPrompt = ENGLISH_TO_OTHER_SYSTEM_PROMPT_TEMPLATE
const systemPrompt = promptTemplate
.replace(/{{targetLanguage}}/g, targetLanguage)
.replace(/{{englishFileContent}}/g, englishContent);
@@ -314,7 +428,15 @@ async function translateToEnglish() {
// Read German translation file
const germanContent = fs.readFileSync(germanFile, 'utf8');
const translatedContent = await translateContent(germanContent, GERMAN_TO_ENGLISH_SYSTEM_PROMPT, null, GERMAN_TO_ENGLISH_MODEL);
// Use special legal prompt for legal documents
const isLegalFile = LEGAL_FILES.includes(fileName);
const systemPrompt = isLegalFile ? LEGAL_GERMAN_TO_ENGLISH_SYSTEM_PROMPT : GERMAN_TO_ENGLISH_SYSTEM_PROMPT;
if (isLegalFile) {
console.log(`📋 Using special legal document translation prompt for ${fileName}`);
}
const translatedContent = await translateContent(germanContent, systemPrompt, null, GERMAN_TO_ENGLISH_MODEL);
const englishObjectString = extractJSObjectString(translatedContent);
if (englishObjectString) {
@@ -335,7 +457,8 @@ async function translateToEnglish() {
console.log(`✅ German to English translation completed for ${translatedFiles.length} files`);
// Return all English files that exist (both newly translated and existing)
return TRANSLATION_FILES.filter(fileName => fs.existsSync(path.join(ENGLISH_DIR, fileName)));
const allFiles = [...TRANSLATION_FILES, ...LEGAL_FILES];
return allFiles.filter(fileName => fs.existsSync(path.join(ENGLISH_DIR, fileName)));
}
// Function to translate English to other languages (multiple files)
@@ -370,10 +493,18 @@ async function translateToOtherLanguages(availableEnglishFiles) {
// Read English file
const englishContent = fs.readFileSync(englishFile, 'utf8');
// Check if this is a legal file
const isLegalFile = LEGAL_FILES.includes(fileName);
if (isLegalFile) {
console.log(`📋 Using special legal document translation prompt for ${fileName}${langName}`);
}
const translatedContent = await translateToTargetLanguage(
englishContent,
langName,
ENGLISH_TO_OTHER_MODEL
ENGLISH_TO_OTHER_MODEL,
isLegalFile
);
const translatedObjectString = extractJSObjectString(translatedContent);