Skip to content

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

  1. Assets/Script/App/Managers/LocalizationManager.cs
  2. Added locale change event
  3. Added event invocation after locale change

New Files

  1. Assets/Script/App/UI/Shared/LocalizedTextHandler.cs
  2. Main component implementation
  3. Complete with documentation and editor helpers

  4. docs/LocalizedTextHandlerGuide.md

  5. Comprehensive usage guide
  6. Examples and best practices
  7. Troubleshooting section

  8. docs/LocalizedTextHandlerImplementation.md (this file)

  9. Implementation summary
  10. Technical details
  11. 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

  1. Create a simple UI panel with 2-3 text elements
  2. Add LocalizedTextHandler component
  3. Configure text pairs with LocalizedString references
  4. Change language in game
  5. Verify all texts update correctly

Edge Cases

  1. Test with empty/null references (should handle gracefully)
  2. Test enabling/disabling GameObject multiple times
  3. Test removing text pairs at runtime
  4. Test adding text pairs at runtime

Performance

  1. Profile with many LocalizedTextHandler components
  2. Verify no memory leaks after multiple enable/disable cycles
  3. Check GC allocations

Migration Guide

For New UI Panels

  1. Create panel as usual
  2. Add LocalizedTextHandler component
  3. Configure text pairs in Inspector
  4. 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:

  1. Font Support
  2. Add TMP_FontAsset per text pair
  3. Change fonts based on language

  4. Formatting Support

  5. Add custom formatting functions
  6. Support string interpolation

  7. Animation Support

  8. Animate text changes (fade, slide, etc.)
  9. Configurable transition effects

  10. Editor Improvements

  11. Auto-detect TMP_Text components in hierarchy
  12. Quick-setup wizard for common patterns
  13. Validation warnings for missing references

  14. Asset References

  15. Support for LocalizedAsset (sprites, audio clips, etc.)
  16. Extend beyond text-only localization
  • 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.