# clpsplug/i18ntools architecture

> Architecture diagram of the GitHub repository clpsplug/i18ntools, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/clpsplug/i18ntools
- Repository: https://github.com/clpsplug/i18ntools

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% External Resources
    subgraph "External Resources"
        EX1["SupportedLanguages.json (JSON)"]
        EX2["Strings.json (JSON)"]
    end

    %% Editor Tools
    subgraph "Editor Tools"
        E1["I18n String Viewer"]
        E2["I18n Tools Window"]
        E3["Key Generator for I18nKeys"]
        E4["Editor Inspector Component"]
        E5["String Resource Tree View"]
        E6["Editor Assembly Definition"]
    end

    %% Runtime Components
    subgraph "Runtime Components"
        R1["I18n String Configuration"]
        R2["I18n Localization Strings"]
        R3["I18n Language Switching Tool"]
        R4["I18n Static Label (World)"]
        R5["I18n Static Label UGUI (TextMeshPro)"]
        R6["Utility Methods"]
        R7["Runtime Assembly Definition"]
    end

    %% External Integration
    INT["External Integration: TextMeshPro, Unity UPM"]

    %% Data Flow from External Resources to Runtime Components
    EX1 -->|"resources"| R1
    EX2 -->|"resources"| R2

    %% Data Flow from External Resources to Editor Tools for analysis
    EX1 -->|"feeds"| E2
    EX2 -->|"feeds"| E2

    %% Editor Tools Internal Data Flow
    E2 -->|"generates-keys"| E3
    E1 -->|"displays"| E5
    E1 -->|"inspects"| E4

    %% Runtime Data Flow and Integration
    R1 -->|"configures"| R3
    R2 -->|"data-for"| R3
    R3 -->|"updates"| R4
    R3 -->|"updates"| R5
    R6 -->|"assists"| R3

    %% Integration with External Systems
    INT -->|"integrates"| R5
    INT -->|"supports"| E2

    %% Styles and Colors
    classDef editor fill:#a6cee3,stroke:#1f78b4,stroke-width:2px;
    classDef runtime fill:#b2df8a,stroke:#33a02c,stroke-width:2px;
    classDef external fill:#fdbf6f,stroke:#ff7f00,stroke-width:2px;
    classDef integration fill:#cab2d6,stroke:#6a3d9a,stroke-width:2px;

    class E1,E2,E3,E4,E5,E6 editor;
    class R1,R2,R3,R4,R5,R6,R7 runtime;
    class EX1,EX2 external;
    class INT integration;

    %% Click Events for Component Mapping
    click E1 "https://github.com/clpsplug/i18ntools/blob/base/Editor/I18nStringViewer.cs"
    click E2 "https://github.com/clpsplug/i18ntools/blob/base/Editor/I18nToolsWindow.cs"
    click E3 "https://github.com/clpsplug/i18ntools/blob/base/Editor/Generator/KeyGenerator.cs"
    click E4 "https://github.com/clpsplug/i18ntools/blob/base/Editor/InspectorComponent/I18nStaticLabelBaseInspector.cs"
    click E5 "https://github.com/clpsplug/i18ntools/blob/base/Editor/Component/StringTreeView.cs"
    click R4 "https://github.com/clpsplug/i18ntools/blob/base/Runtime/I18nStaticLabel.cs"
    click R5 "https://github.com/clpsplug/i18ntools/blob/base/Runtime/I18nStaticLabelUGUI.cs"
    click R1 "https://github.com/clpsplug/i18ntools/blob/base/Runtime/I18nStringConfig.cs"
    click R2 "https://github.com/clpsplug/i18ntools/blob/base/Runtime/I18nStrings.cs"
    click R3 "https://github.com/clpsplug/i18ntools/blob/base/Runtime/I18nTool.cs"
    click R6 "https://github.com/clpsplug/i18ntools/blob/base/Runtime/Util.cs"
    click E6 "https://github.com/clpsplug/i18ntools/blob/base/Editor/clpsplug.I18n.Editor.asmdef"
    click R7 "https://github.com/clpsplug/i18ntools/blob/base/Runtime/clpsplug.i18n.Runtime.asmdef"
```
