Translation API Fix - Summary¶
Problem Statement¶
The translation feature was not showing as available in Chrome Beta, even with Experimental Web Features enabled. Users were seeing "Translation not available" instead of the translation dropdown.
Root Cause Analysis¶
The Issue¶
The codebase was implementing the outdated Translation API specification that used:
// OLD API (deprecated)
if ('translation' in self && self.translation && 'createTranslator' in self.translation) {
const canTranslate = await self.translation.canTranslate({
sourceLanguage: 'en',
targetLanguage: 'es'
});
const translator = await self.translation.createTranslator({
sourceLanguage: 'en',
targetLanguage: 'es'
});
}
However, the current Translation API specification (as of 2024-2025) uses:
// NEW API (current spec)
if ('Translator' in window && window.Translator) {
const availability = await Translator.availability({
sourceLanguage: 'en',
targetLanguage: 'es'
});
const translator = await Translator.create({
sourceLanguage: 'en',
targetLanguage: 'es'
});
}
Why It Failed¶
- The browser check
'translation' in selfalways returnedfalsebecause the API moved fromself.translationtowindow.Translator - The code never detected the API as available, even when it was present
- The UI always showed "Translation not available" instead of the translation controls
Solution Implemented¶
Code Changes¶
1. Updated TypeScript Interfaces¶
Before:
interface TranslationAPI {
canTranslate(options: { sourceLanguage: string; targetLanguage: string }): Promise<string>;
createTranslator(options: { sourceLanguage: string; targetLanguage: string }): Promise<Translator>;
}
declare global {
interface WindowOrWorkerGlobalScope {
translation?: TranslationAPI;
}
}
After:
interface TranslatorConstructor {
create(options: TranslatorCreateOptions): Promise<Translator>;
availability(options: TranslatorCreateOptions): Promise<'unavailable' | 'downloadable' | 'downloading' | 'available'>;
}
declare global {
interface Window {
Translator?: TranslatorConstructor;
}
}
2. Updated Browser Support Check¶
Before:
if ('translation' in self && self.translation && 'createTranslator' in self.translation) {
const canTranslate = await self.translation.canTranslate({
sourceLanguage: 'en',
targetLanguage: 'es'
});
this.canTranslate = canTranslate !== 'no';
}
After:
if ('Translator' in window && window.Translator) {
const availability = await window.Translator.availability({
sourceLanguage: 'en',
targetLanguage: 'es'
});
this.canTranslate = availability !== 'unavailable';
}
3. Updated Translator Creation¶
Before:
this.translator = await self.translation.createTranslator({
sourceLanguage: 'en',
targetLanguage: targetLanguage
});
After:
this.translator = await window.Translator.create({
sourceLanguage: 'en',
targetLanguage: targetLanguage
});
Files Modified¶
- CompendiumUI/script.ts - Updated TranslationService class
- CompendiumUI/translation.test.ts - Updated tests to check for new API
- TRANSLATION_IMPLEMENTATION.md - Updated documentation
- TRANSLATION_FEATURE_README.md - Updated API detection examples
- TESTING_TRANSLATION.md - Added new testing guide
Verification¶
Build & Tests¶
✅ All 69 tests pass ✅ TypeScript compilation successful ✅ No build errors ✅ Code review passed with no issues ✅ Security scan (CodeQL) passed with 0 alerts
Expected Behavior¶
When Translation API is available: - ✅ Translation dropdown appears in navigation menu - ✅ User can select from 12 languages - ✅ Content translates when language is selected - ✅ Warning banner appears when translated - ✅ Can return to English original
When Translation API is not available: - ✅ "Translation not available" link appears in header - ✅ Tooltip explains requirements when clicked - ✅ Suggests using browser's built-in translation
Console Output¶
When the page loads with the new code:
If API is available:
If API requires download:
If API is not supported:
Testing Instructions¶
Prerequisites¶
- Chrome Canary, Dev, or Beta (Version 128+)
- Enable flags in
chrome://flags: #translation-api#enable-experimental-web-platform-features- Restart Chrome
Quick Test¶
Open browser console and type:
// Check if API exists
'Translator' in window // Should return true
// Check availability
await Translator.availability({ sourceLanguage: 'en', targetLanguage: 'es' })
// Should return: 'available', 'downloadable', 'downloading', or 'unavailable'
Full Test¶
- Build and run:
cd CompendiumUI && npm install && npm run dev - Navigate to
http://localhost:5173 - Look for translation dropdown in menu
- Select a language and verify translation works
- Verify warning banner appears
- Return to English and verify restoration
References¶
API Specification¶
- New Spec: https://github.com/WICG/translation-api
- Chrome Status: https://chromestatus.com/feature/5182950152642560
- Explainer: https://github.com/WICG/translation-api/blob/main/explainer.md
Key Differences¶
| Aspect | Old API | New API |
|---|---|---|
| Namespace | self.translation |
window.Translator |
| Check method | canTranslate() returns string |
availability() returns enum |
| Create method | createTranslator() |
create() |
| Return values | 'yes', 'no', 'after-download' |
'available', 'downloadable', 'downloading', 'unavailable' |
Impact¶
This fix ensures that users with Chrome Beta and experimental flags enabled will now see the translation feature as available, allowing them to: - Test the experimental translation feature - Translate Copyright Compendium content into 12 languages - Provide feedback on the feature before wider release
Future Considerations¶
- Monitor Translation API specification for further changes
- Update when API is released to stable Chrome
- Consider adding fallback to cloud translation services
- Add translation quality indicators
- Support for additional languages as they become available