feat: add Legal Translation tool (translate.php)

New dedicated tool for translating Norwegian legal documents (Barnevernet
letters, court decisions, correspondence) into the user's chosen language
with legal-terminology annotations.

- translate.php: new tool page with source/target language selectors,
  4-way UI lang switcher, file upload, doc picker, streaming results
- api/translate.php: NDJSON streaming endpoint; Azure GPT-4o-mini with
  legal-aware prompt that preserves Norwegian statute refs verbatim and
  annotates terms with no target-language equivalent; 2-credit cost
- assets/js/translate.js: form handler, NDJSON stream reader, copy button
- assets/css/tools.css: .lt-* styles for translation result + annotations
- includes/i18n.php: 22 lt_* keys × 4 languages; translate entry in nav
- includes/FreeTier.php: translate → 2 credits
- includes/CaseResults.php + case-result.php: translate in eligible tools,
  toolLabel, toolIcon, deriveTitle, rendering block, rerun map

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-24 09:59:06 +02:00
parent 21c092e0d0
commit effd3289b4
8 changed files with 842 additions and 1 deletions
+146
View File
@@ -0,0 +1,146 @@
<?php
declare(strict_types=1);
$toolName = 'translate';
$toolTitle = 'Legal Translation';
$toolKind = 'Legal Translation';
$toolBadge = 'Azure · GPT-4o';
$extraScripts = ['assets/js/translate.js'];
require_once __DIR__ . '/includes/layout.php';
$ltLang = dbnToolsCurrentLanguage();
$ltT = static fn(string $k): string => dbnToolsT($k, $ltLang);
// Default source = Norwegian; target = UI language, but if UI=no, default to English
$ltDefaultTarget = ($ltLang === 'no') ? 'en' : $ltLang;
$ltI18n = [
'runButton' => $ltT('lt_run_button'),
'runButtonBusy' => $ltT('lt_run_button_busy'),
'extractingFiles' => $ltT('lt_extracting_files'),
'translatingStatus' => $ltT('lt_translating_status'),
'needInput' => $ltT('lt_need_input'),
'errorPrefix' => $ltT('lt_error_prefix'),
'serverReturned' => $ltT('lt_server_returned'),
'resultTitle' => $ltT('lt_result_title'),
'annotationsTitle' => $ltT('lt_annotations_title'),
'copyButton' => $ltT('lt_copy_button'),
'copyDone' => $ltT('lt_copy_done'),
'sameLangError' => $ltT('lt_same_lang_error'),
'disclaimer' => $ltT('lt_disclaimer'),
];
?>
<form id="ltForm" class="tool-form" novalidate>
<div class="lang-switcher" id="ltLangSwitcher" role="group" aria-label="UI language">
<button type="button" class="lang-btn lt-lang-btn <?= $ltLang === 'en' ? 'is-active' : '' ?>" data-lang="en">&#127468;&#127463; EN</button>
<button type="button" class="lang-btn lt-lang-btn <?= $ltLang === 'no' ? 'is-active' : '' ?>" data-lang="no">&#127475;&#127476; NO</button>
<button type="button" class="lang-btn lt-lang-btn <?= $ltLang === 'uk' ? 'is-active' : '' ?>" data-lang="uk">&#127482;&#127462; UK</button>
<button type="button" class="lang-btn lt-lang-btn <?= $ltLang === 'pl' ? 'is-active' : '' ?>" data-lang="pl">&#127477;&#127473; PL</button>
</div>
<div class="lt-source-target-row control-row">
<span class="control-label"><?= htmlspecialchars($ltT('lt_source_label')) ?></span>
<label><input type="radio" name="ltSourceLang" value="no" checked> &#127475;&#127476; NO</label>
<label><input type="radio" name="ltSourceLang" value="en"> &#127468;&#127463; EN</label>
<label><input type="radio" name="ltSourceLang" value="uk"> &#127482;&#127462; UK</label>
<label><input type="radio" name="ltSourceLang" value="pl"> &#127477;&#127473; PL</label>
</div>
<div class="lt-source-target-row control-row" id="ltTargetRow">
<span class="control-label"><?= htmlspecialchars($ltT('lt_target_label')) ?></span>
<label><input type="radio" name="ltTargetLang" value="en" <?= $ltDefaultTarget === 'en' ? 'checked' : '' ?>> &#127468;&#127463; EN</label>
<label><input type="radio" name="ltTargetLang" value="no" <?= $ltDefaultTarget === 'no' ? 'checked' : '' ?>> &#127475;&#127476; NO</label>
<label><input type="radio" name="ltTargetLang" value="uk" <?= $ltDefaultTarget === 'uk' ? 'checked' : '' ?>> &#127482;&#127462; UK</label>
<label><input type="radio" name="ltTargetLang" value="pl" <?= $ltDefaultTarget === 'pl' ? 'checked' : '' ?>> &#127477;&#127473; PL</label>
</div>
<div class="control-row" id="ltDocTypeControl">
<span class="control-label"><?= htmlspecialchars($ltT('lt_doc_type_label')) ?></span>
<label><input type="radio" name="ltDocType" value="auto" checked> <?= htmlspecialchars($ltT('la_doc_type_auto')) ?></label>
<label><input type="radio" name="ltDocType" value="barnevernet"> Barnevernet</label>
<label><input type="radio" name="ltDocType" value="adopsjon"> Adopsjon</label>
<label><input type="radio" name="ltDocType" value="emergency"> Akutt-plassering</label>
<label><input type="radio" name="ltDocType" value="samvær"> Samvær</label>
<label><input type="radio" name="ltDocType" value="fylkesnemnd"> Fylkesnemnd</label>
<label><input type="radio" name="ltDocType" value="other"> <?= htmlspecialchars($ltT('la_doc_type_other')) ?></label>
</div>
<p class="upload-hint"><?= htmlspecialchars($ltT('lt_engine_hint')) ?></p>
<div id="docPickerSection" class="doc-picker-section">
<button type="button" id="docPickerBtn" class="doc-picker-btn" aria-haspopup="dialog">
<svg class="doc-picker-btn__icon" width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><rect x="2" y="1" width="9" height="12" rx="1.5" stroke="currentColor" stroke-width="1.4"/><path d="M5 5h5M5 8h3" stroke="currentColor" stroke-width="1.3" stroke-linecap="round"/><rect x="7" y="9" width="6" height="5" rx="1" fill="white" stroke="currentColor" stroke-width="1.3"/><path d="M9 11h2M9 12.5h1" stroke="currentColor" stroke-width="1" stroke-linecap="round"/></svg>
<span>Select from My Docs</span>
</button>
<div id="docPickerChips" class="doc-picker-chips" aria-label="Selected documents"></div>
<input type="hidden" id="docPickerIds" name="doc_ids" value="">
</div>
<div class="upload-zone" id="ltUploadZone" role="region" aria-label="File upload">
<input type="file" id="ltUploadInput" multiple accept=".pdf,.docx,.txt" aria-label="Choose files to translate">
<div id="ltUploadPrompt" class="upload-prompt">
<span class="upload-icon" aria-hidden="true">&#8679;</span>
<p>Drop up to 5 files here, or <label for="ltUploadInput" class="upload-browse">browse</label></p>
<p class="upload-hint"><strong>PDF</strong>, <strong>DOCX</strong>, <strong>TXT</strong> &mdash; text extracted in memory, never stored</p>
</div>
<div id="ltUploadFileInfo" class="upload-file is-hidden">
<ul id="ltUploadFileList" class="upload-file-list"></ul>
<button type="button" id="ltUploadClear" class="upload-clear">&times; Clear files</button>
</div>
</div>
<label class="input-label" for="ltInput"><?= htmlspecialchars($ltT('lt_input_label')) ?> <small class="control-hint"><?= htmlspecialchars($ltT('lt_input_hint')) ?></small></label>
<textarea id="ltInput" name="text" rows="8" placeholder="<?= htmlspecialchars($ltT('lt_input_placeholder')) ?>"></textarea>
<div class="form-footer">
<p id="ltStatus" class="form-status" role="status" aria-live="polite"></p>
<button id="ltRunButton" type="submit"><?= htmlspecialchars($ltT('lt_run_button')) ?></button>
</div>
</form>
<section id="ltResults" class="results" aria-live="polite">
<div class="empty-state">
<h3><?= htmlspecialchars($ltT('lt_ready_title')) ?></h3>
<p><?= htmlspecialchars($ltT('lt_ready_intro')) ?></p>
</div>
</section>
<script>
window.DBN_LT_I18N = <?= json_encode($ltI18n, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP) ?>;
window.DBN_LT_LANG = <?= json_encode($ltLang) ?>;
</script>
<!-- Hidden stubs so tools.js element refs don't crash on this page -->
<div class="is-hidden" id="languageControl" aria-hidden="true">
<input type="radio" name="language" value="en" <?= $ltLang === 'en' ? 'checked' : '' ?>>
<input type="radio" name="language" value="no" <?= $ltLang === 'no' ? 'checked' : '' ?>>
<input type="radio" name="language" value="uk" <?= $ltLang === 'uk' ? 'checked' : '' ?>>
<input type="radio" name="language" value="pl" <?= $ltLang === 'pl' ? 'checked' : '' ?>>
</div>
<div class="is-hidden" id="redactionControl" aria-hidden="true"></div>
<div class="is-hidden" id="audioZone" aria-hidden="true">
<input type="file" id="audioInput" style="display:none">
<div id="audioPrompt"></div>
<div id="audioFileInfo"><ol id="audioQueueList"></ol><button type="button" id="audioClear"></button></div>
</div>
<div class="is-hidden" id="diarizeControl" aria-hidden="true">
<input type="checkbox" id="diarizeCheck">
<input type="number" id="numSpeakersInput">
</div>
<div class="is-hidden" id="transcribeLangControl" aria-hidden="true"><input type="radio" name="transcribeLang" value="no" checked></div>
<div class="is-hidden" id="vocabControl" aria-hidden="true">
<div id="vocabPresets"></div>
<textarea id="initPromptInput"></textarea>
</div>
<div class="is-hidden" id="aliasSection" aria-hidden="true">
<button type="button" id="addAliasRow"></button>
<div id="aliasRows"></div>
</div>
<div class="is-hidden" id="exemptSection" aria-hidden="true">
<button type="button" id="addExemptRow"></button>
<div id="exemptRows"></div>
</div>
<div class="is-hidden" id="uploadZone" aria-hidden="true">
<input type="file" id="uploadInput">
<div id="uploadPrompt"></div>
<div id="uploadFileInfo"><ul id="uploadFileList"></ul><button type="button" id="uploadClear"></button></div>
</div>
<?php require_once __DIR__ . '/includes/layout_footer.php'; ?>