Archived on 2026-10-24. This is a historical implementation note, not current setup instructions or an active task list. Original claims, code examples, file paths, and completion statements have not all been revalidated. Use the current documentation and issue register for maintenance decisions.
LocalizedTextHandler Implementation Summary¶
Overview¶
This document summarizes the implementation of a new localization system that allows UI panels and menus to handle text localization without requiring a dedicated controller script.
Problem Solved¶
Previously, simple UI panels (like confirmation dialogs, static menus, text-only pages) needed custom controller scripts just to handle localization. This created unnecessary overhead and code duplication.
Solution Implemented¶
1. Enhanced LocalizationManager (Assets/Script/App/Managers/LocalizationManager.cs)¶
Changes Made:
- Added OnLocaleChanged event that fires when language changes
- Event passes the new locale ID as a parameter
- Minimal changes - only 2 lines of code added
Code Added:
public static event System.Action<int> OnLocaleChanged;
// In SetLocale coroutine:
OnLocaleChanged?.Invoke(localeID);
Benefits: - Backward compatible - doesn't break existing code - Decoupled architecture - manager doesn't need to know about UI components - Event-driven - components subscribe/unsubscribe as needed
2. Created LocalizedTextHandler Component (Assets/Script/App/UI/Shared/LocalizedTextHandler.cs)¶
Key Features: - Attach to any GameObject with TMP_Text components - Configure text-localized string pairs in Inspector - Automatically updates when language changes - Supports multiple text elements on one GameObject - Includes debug mode for troubleshooting - Runtime methods for dynamic text management
Public API:
- UpdateOnEnable - Toggle immediate update on enable
- DebugMode - Enable logging for troubleshooting
- RefreshTexts() - Manually update all texts
- AddTextPair(target, localized) - Add text pair at runtime
- RemoveTextPair(target) - Remove text pair at runtime
Technical Details:
- Subscribes to LocalizationManager.OnLocaleChanged in OnEnable()
- Unsubscribes in OnDisable() to prevent memory leaks
- Uses LocalizedString.GetLocalizedString() to retrieve text
- Error handling with try-catch for each text pair
- Includes editor-only menu item for quick component creation
Files Created/Modified¶
Modified Files¶
Assets/Script/App/Managers/LocalizationManager.cs- Added locale change event
- Added event invocation after locale change
New Files¶
Assets/Script/App/UI/Shared/LocalizedTextHandler.cs- Main component implementation
-
Complete with documentation and editor helpers
-
docs/LocalizedTextHandlerGuide.md - Comprehensive usage guide
- Examples and best practices
-
Troubleshooting section
-
docs/LocalizedTextHandlerImplementation.md(this file) - Implementation summary
- Technical details
- Architecture decisions
Usage Example¶
Before (Controller Required)¶
// Needed a separate controller script
public class ConfirmationController : MonoBehaviour
{
public TMP_Text titleText;
public TMP_Text messageText;
public LocalizedString titleString;
public LocalizedString messageString;
private void OnEnable()
{
UpdateTexts();
LocalizationManager.OnLocaleChanged += UpdateTexts;
}
private void UpdateTexts()
{
titleText.text = titleString.GetLocalizedString();
messageText.text = messageString.GetLocalizedString();
}
private void OnDisable()
{
LocalizationManager.OnLocaleChanged -= UpdateTexts;
}
}
After (No Controller Needed)¶
ConfirmationPanel (GameObject)
├── LocalizedTextHandler
│ ├── Text Pairs:
│ │ ├── Element 0
│ │ │ ├── Target Text: TitleText
│ │ │ └── Localized String: Confirmation_Title
│ │ └── Element 1
│ │ ├── Target Text: MessageText
│ │ └── Localized String: Confirmation_Message
Architecture Decisions¶
Why Event-Based?¶
- Decoupled: Components don't need direct references to LocalizationManager
- Scalable: Easy to add more components later
- Clean: Follows Unity's event-based patterns
Why List of Pairs?¶
- Flexible: Supports any number of text elements
- Organized: Keeps related data together
- Easy to Use: Clear mapping in Inspector
Why Both OnEnable and Refresh?¶
- OnEnable: Automatic update when panel shows
- Refresh: Manual control for dynamic content
- Best of both worlds
Why Add/Remove Methods?¶
- Dynamic: Support runtime UI changes
- Flexible: Can be used by other scripts
- Future-proof: Extensible for advanced use cases
Performance Considerations¶
- Memory: Minimal overhead - only stores list of pairs
- CPU: Only updates when locale changes or manually refreshed
- GC: No allocations during normal operation
- Subscriptions: Properly unsubscribes in OnDisable to prevent memory leaks
Testing Recommendations¶
Basic Functionality¶
- Create a simple UI panel with 2-3 text elements
- Add LocalizedTextHandler component
- Configure text pairs with LocalizedString references
- Change language in game
- Verify all texts update correctly
Edge Cases¶
- Test with empty/null references (should handle gracefully)
- Test enabling/disabling GameObject multiple times
- Test removing text pairs at runtime
- Test adding text pairs at runtime
Performance¶
- Profile with many LocalizedTextHandler components
- Verify no memory leaks after multiple enable/disable cycles
- Check GC allocations
Migration Guide¶
For New UI Panels¶
- Create panel as usual
- Add LocalizedTextHandler component
- Configure text pairs in Inspector
- Done! No controller needed
For Existing Controllers¶
Option 1 - Keep existing controller (if it has other logic): - No changes needed - Existing controllers continue to work
Option 2 - Replace with LocalizedTextHandler (if only handling localization): 1. Remove controller script 2. Add LocalizedTextHandler component 3. Migrate text and LocalizedString references to Inspector 4. Delete controller file
Future Enhancements¶
Potential additions for future iterations:
- Font Support
- Add TMP_FontAsset per text pair
-
Change fonts based on language
-
Formatting Support
- Add custom formatting functions
-
Support string interpolation
-
Animation Support
- Animate text changes (fade, slide, etc.)
-
Configurable transition effects
-
Editor Improvements
- Auto-detect TMP_Text components in hierarchy
- Quick-setup wizard for common patterns
-
Validation warnings for missing references
-
Asset References
- Support for LocalizedAsset (sprites, audio clips, etc.)
- Extend beyond text-only localization
Related Documentation¶
docs/LocalizedTextHandlerGuide.md- Detailed usage guide- Unity Localization Package Documentation
Assets/Script/App/Managers/LocalizationManager.cs- Core localization manager
Conclusion¶
This implementation provides a clean, efficient solution for handling UI localization without requiring controller scripts for simple panels. It's: - ✅ Easy to use - ✅ Performant - ✅ Maintainable - ✅ Extensible - ✅ Well-documented
The system is ready for immediate use in the Tiny-Chaos project and can be easily extended for future needs.