This guide explains the enhanced fallback system that automatically uses English content when Chinese or Spanish translations are missing.
The enhanced fallback system provides three levels of fallback:
- File-level fallback: If a localized file doesn't exist, use the English version
- Deep merge fallback: Merge localized content with English content, ensuring all properties exist
- Property-level fallback: Access specific properties with automatic fallback to English
The existing useLocalizedContent and useLocalizedContentFile functions now use deep merging:
// Before: Only file-level fallback
const content = useLocalizedContentFile('indexContent.json', defaultContent);
// After: Deep merge with English fallback
const content = useLocalizedContentFile('indexContent.json', defaultContent);
// Now all properties from English are available, even if missing in Chinese/SpanishExample: If the Chinese indexContent.json is missing the mainContent.sections array, the merged content will include the English sections.
The new useLocalizedProperty function allows accessing specific properties with automatic fallback:
import { useLocalizedProperty } from '../utils/contentLoader';
// Access a specific property with fallback
const title = useLocalizedProperty<string>(
'mainContent.intro.title',
indexContent,
'indexContent.json'
);
// Access nested properties
const sectionTitle = useLocalizedProperty<string>(
'mainContent.sections.0.title',
indexContent,
'indexContent.json'
);
// Access array properties
const sectionsCount = useLocalizedProperty<number>(
'mainContent.sections.length',
indexContent,
'indexContent.json'
);import { useLocalizedContentFile, useLocalizedProperty } from '../utils/contentLoader';
import indexContent from '../data/indexContent.json';
function MyComponent() {
// Get entire content with fallback
const content = useLocalizedContentFile('indexContent.json', indexContent);
// Get specific property with fallback
const title = useLocalizedProperty('mainContent.intro.title', indexContent, 'indexContent.json');
return (
<div>
<h1>{title}</h1>
<p>{content.mainContent.intro.content[0]}</p>
</div>
);
}function AdvancedComponent() {
// Access deeply nested properties
const firstSectionTitle = useLocalizedProperty(
'mainContent.sections.0.title',
indexContent,
'indexContent.json'
);
// Access array properties
const sectionsCount = useLocalizedProperty(
'mainContent.sections.length',
indexContent,
'indexContent.json'
);
// Access properties that might not exist in localized version
const donateTitle = useLocalizedProperty(
'donate.title',
indexContent,
'indexContent.json'
);
return (
<div>
<h2>First Section: {firstSectionTitle}</h2>
<p>Total Sections: {sectionsCount}</p>
<p>Donate Title: {donateTitle}</p>
</div>
);
}The useLocalizedProperty function uses dot notation to access nested properties:
'mainContent.intro.title'- Access nested object properties'mainContent.sections.0.title'- Access array elements (0-based index)'mainContent.sections.length'- Access array properties'donate.links.0.text'- Access nested array elements
The system provides helpful console warnings when fallbacks are used:
Property mainContent.sections.0.title not found in zh-Hans/indexContent.json, using English fallback
Localized content not found for locale: zh-Hans, file: missingFile.json, falling back to English
These warnings help developers identify missing translations.
Existing code using useLocalizedContent and useLocalizedContentFile will automatically benefit from the enhanced fallback system. No changes are required.
Consider using useLocalizedProperty for specific property access:
// Instead of this:
const content = useLocalizedContentFile('indexContent.json', indexContent);
const title = content.mainContent.intro.title;
// Consider this for better performance:
const title = useLocalizedProperty('mainContent.intro.title', indexContent, 'indexContent.json');- Graceful Degradation: Missing translations don't break the UI
- Incremental Translation: You can translate content piece by piece
- Better User Experience: Users always see content, even if partially in English
- Developer Friendly: Clear warnings help identify missing translations
- Performance: Property-level access can be more efficient than loading entire files
You can test the fallback system by:
- Visiting
/fallback-demoto see the demonstration page - Switching between languages to see how fallbacks work
- Checking the browser console for fallback warnings
- Comparing content between English and localized versions
- Use property-level access for frequently accessed properties
- Monitor console warnings to identify missing translations
- Test with incomplete translations to ensure fallbacks work correctly
- Document missing translations for future translation work
- Consider performance when choosing between file-level and property-level access
The system expects this file structure:
src/data/
├── indexContent.json # English (fallback)
├── siteCommon.json # English (fallback)
├── zh-Hans/
│ ├── indexContent.json # Chinese (partial)
│ └── siteCommon.json # Chinese (partial)
└── es/
├── indexContent.json # Spanish (partial)
└── siteCommon.json # Spanish (partial)
Missing properties in Chinese/Spanish files will automatically fall back to English.