How clpsplug/i18ntools works
Project Type and Purpose
• This project is a Unity package designed to enable internationalization of strings in Unity games or applications.
• It includes both runtime components (for fetching and displaying localized strings) and editor components (tools that assist developers in managing and generating localization keys).
And Separate the Main Components
• Editor Components:
• Located under the “Editor” folder.
• Includes tools such as:
• I18n String Viewer (shows string resources in a tree view to facilitate key copying)
• I18n Tools Window (which generates the I18nKeys class and scans for characters used in strings)
• Additional editor inspectors and components (e.g., InspectorComponent for static labels, KeyGenerator for class generation).
• Managed by Unity’s editor through .asmdef files (e.g., clpsplug.I18n.Editor.asmdef).
• Runtime Components:
• Found in the “Runtime” folder.
• Consists of core functionality such as:
• I18nString – provides methods (like For()) to fetch localized strings based on a “key”.
• I18nStaticLabel and I18nStaticLabelUGUI – specialized MonoBehaviour components attached to GameObjects (configured for either world space or UI via TextMeshPro).
• I18nTool – responsible for propagating language change notifications to update the labels.
• Utility Classes (e.g., Util.cs) and configurations (e.g., I18nStringConfig, I18nStrings) for managing localization data.
• Also uses an assembly definition file (clpsplug.i18n.Runtime.asmdef) to define the runtime module.
• Configuration/Resource Files:
• Although not directly in the file tree, the README explains that the plugin expects JSON resources (for supported languages and defined strings) placed in the Unity Resources folder (e.g., Assets/Resources/I18n/SupportedLanguages.json and Assets/Resources/strings.json). These are external but crucial to understanding the data flows.
Relationships and Data Flows
• Data Flow from Configuration to Runtime:
• JSON files in the Resources folder supply the supported languages and string definitions.
• The runtime components (via I18nString and I18nTool) read these configurations, compile keys, and determine which locale text to display.
• Editor Tools’ Role:
• The Editor tools (string viewer and tools window) analyze the JSON files, allowing:
• The generation of a static C# I18nKeys class which encapsulates string keys for safer coding practices.
• Extraction and presentation of characters from string resources (handy for TextMeshPro Font Asset creation).
• The tools help streamline the developer workflow by updating, copying, and ensuring that the runtime understands the current set of localization strings.
• Interaction Layer:
• When the language is changed through code (calling I18nStringRepository.GetInstance().ChangeLanguage(lang) and I18nTool.NotifyLanguageSwitchToStaticLabels()), the new language strings are loaded and propagated to all game objects that use I18nStaticLabel components.
Important Architectural Patterns and Design Principles
• Separation of Concerns:
• Clear delineation between Editor-only utilities and runtime functionality.
• Modular design using Unity’s .asmdef files to encapsulate editor and runtime code in separate assemblies.
• Data-Driven Localization:
• The system uses external JSON configuration to drive both the supported languages and the actual localization strings.
• Code Generation:
• The editor tool that generates the I18nKeys class minimizes runtime errors due to manual string key entry and solidifies the API used by developers.
• Integration with Unity and TextMeshPro:
• Runtime components are designed to work with TextMeshPro by automatically updating labels.
Final Remarks for the Principal Software Engineer
• Don’t shy away from adding several sub-components if required; breaking down the system into as many components as possible will help capture all the detailed interactions.