Skip to content

Game.UI.Editor.LocalizationField

Assembly: Assembly-CSharp
Namespace: Game.UI.Editor

Type: class

Base: Widget

Summary:
LocalizationField is a UI widget used by the editor to collect, edit and export localized text for multiple locales. It stores a list of locale → text entries (LocalizationFieldEntry), provides helper methods to initialize the list from locale assets or existing entries, to add/remove languages, validate entries, and to build LocaleData dictionaries used when writing locale files. It also exposes a nested Bindings class that creates UI trigger bindings (set/add/remove entry) for integration with the widget binding system.


Fields

  • private System.Collections.Generic.List<LocalizationFieldEntry> m_Localization = new List<LocalizationFieldEntry>()
    Holds the collection of locale/text entries managed by the widget. Most public operations (add, remove, set, validate) operate on this list.

Properties

  • public System.Collections.Generic.IReadOnlyList<LocalizationFieldEntry> localization => m_Localization
    Read-only accessor for the current list of LocalizationFieldEntry items. Useful for reading entries for display or export.

  • public LocalizedString placeholder { get; set; }
    Placeholder LocalizedString shown in the UI for empty fields or to indicate the key/purpose of this localization field.

Constructors

  • public LocalizationField(LocalizedString placeholder)
    Creates a new LocalizationField and sets the placeholder. The constructor calls Initialize() to populate mandatory locales (fallback and active locale).

Methods

  • public void Clear()
    Clears all entries from the internal localization list.

  • public void Initialize()
    Resets and initializes the list with mandatory entries (fallback locale) and ensures the active locale is present. Called by constructor.

  • public void Initialize(System.Collections.Generic.IEnumerable<LocaleAsset> assets, string localeFormat)
    Initializes entries from a collection of LocaleAsset objects. For each asset, if the asset contains an entry matching localeFormat and it is non-empty, that entry is added or merged into the list.

  • public void Initialize(System.Collections.Generic.IEnumerable<LocalizationField.LocalizationFieldEntry> entries)
    Initializes entries from an existing collection of LocalizationFieldEntry, merging them into the list (replacing existing entries with the same localeId).

  • public static bool IsMandatory(string localeId)
    Returns true when the given localeId equals the game's fallback locale id. Mandatory locales cannot be removed — only cleared.

  • private void InitializeMandatory()
    Ensures the fallback locale (if configured) exists in the list and is placed at index 0. If the fallback locale already exists at a non-zero index, it swaps it to index 0.

  • public void Add(LocaleAsset asset, string localeFormat)
    Adds or updates a single LocaleAsset into the internal list using the provided localeFormat (key within the asset entries). If asset.data.entries contains the key and it is non-empty, the asset's localeId is added or updated.

  • public void Add(LocalizationField.LocalizationFieldEntry entry)
    Adds the provided entry if its localeId does not exist yet; otherwise replaces the existing entry for that localeId.

  • public void SetEntry(int index, string localeId, string text)
    Updates the entry at the given index with the provided localeId and text, and marks the widget's properties as changed (SetPropertiesChanged()).

  • public void AddLanguage()
    Attempts to add an unused language. Prefers the active locale if not already present; otherwise iterates supported locales to find an unused one. Adds an empty entry and marks properties changed.

  • public void RemoveLanguage(int index)
    Removes the entry at the provided index. If the entry is mandatory (fallback locale), its text is cleared instead of removing. Marks properties changed.

  • public bool IsValid()
    Returns true if the field contains at least one valid (non-empty) entry. If fallback locale is configured, it prefers validating that entry first.

  • public System.Collections.Generic.IEnumerable<LocalizationField.LocalizationFieldEntry> ValidEntries()
    Enumerates all entries whose text is non-empty/whitespace (uses the private IsValid(LocalizationFieldEntry) helper).

  • private bool IsValid(LocalizationField.LocalizationFieldEntry entry)
    Helper that returns true when entry.text is not null/empty/whitespace.

  • private bool TryGetUnusedLanguage(out string localeID)
    Finds an unused locale id to add: first tries the active locale, then iterates all supported locales. Returns false and sets localeID to null if none available.

  • public void BuildLocaleData(string format, System.Collections.Generic.Dictionary<string, LocaleData> localeDatas, string fallback = null)
    Copies valid entries into the supplied localeDatas dictionary keyed by localeId. For each valid entry it ensures a LocaleData exists and sets localeDatas[localeId].entries[format] = entry.text. If a fallback string is provided, ensures the fallback locale has an entry for the format (TryAdd).

  • protected override void WriteProperties(IJsonWriter writer)
    Writes widget properties for serialization: supported locales, the localization list, placeholder, and mandatoryId (fallback locale id). Calls base.WriteProperties then writes:

  • "supportedLocales" → GameManager.instance.localizationManager.GetSupportedLocales()
  • "localization" → the m_Localization list
  • "placeholder" → placeholder
  • "mandatoryId" → GameManager.instance.localizationManager.fallbackLocaleId

Additional notes: - Nested types: - LocalizationField.LocalizationFieldEntry (struct) — represents a single locale/text pair and implements IJsonWritable to serialize itself with properties "localeId" and "text". - LocalizationField.Bindings (class) — creates bindings for UI triggers: "setLocalizationEntry" (sets an entry), "addLocalizationEntry" (adds a language), and "removeLocalizationEntry" (removes a language). These bindings call the corresponding LocalizationField methods and invoke onValueChanged callbacks.

Usage Example

// Create with a placeholder (LocalizedString can be constructed or referenced from resources)
var placeholder = new LocalizedString("MYMOD_PLACEHOLDER");
var locField = new LocalizationField(placeholder);

// Ensure mandatory and active locales exist
locField.Initialize();

// Add a new language (tries active locale first)
locField.AddLanguage();

// Set an entry (index must be valid)
locField.SetEntry(0, GameManager.instance.localizationManager.activeLocaleId, "My localized text");

// Validate field
if (!locField.IsValid())
{
    // handle missing translations
}

// Build the LocaleData dictionary for export
var localeDatas = new Dictionary<string, LocaleData>();
locField.BuildLocaleData("MYMOD.MYSTRING", localeDatas, fallback: "Default fallback text");

// The Bindings class can be registered with the widget binding system to enable UI triggers:
// - "setLocalizationEntry"  -> calls SetEntry(index, localeId, text)
// - "addLocalizationEntry"  -> calls AddLanguage()
// - "removeLocalizationEntry" -> calls RemoveLanguage(index)

This widget is intended for editor/mod tools that collect localized strings for assets or UI and export them into LocaleData structures compatible with the game's localization system.