Localized String
A table and a key in one field, for your own scripts
Overview
A serializable type that holds a localization table and one of its keys. Declare it in any script and the Inspector draws it on one line, with no custom editor needed. Use it when a script shows text it writes itself; for text on a component that never changes, use a Localized Object.
Usage
Declare a field in your script: public LocalizedString title; (or a private one with [SerializeField]). Arrays, lists and nested classes work too.
Pick the table and key in the Inspector: click the field to open its popup. You can also drop a table asset onto the field. Write the text itself in the table window.
Read the text with title.GetString(), and read it again when LocalizationManager.OnLanguageSet fires.
The Field
The field is marked with a globe and shows its table and key (HUD / emote.wave) first. The default language's text follows on the right, in the room that's left. It also tells you when something needs attention:
| Shows | Meaning |
|---|---|
| None (Localized String) | No key is set yet. |
| No text | The key exists, but the default language has no text for it. |
| Not in table | The table has no such key (a typo, or a deleted key). |
| No table / Missing table | A key is set without a table, or its table asset was deleted. |
Right-click the field for Copy as Code, Copy Key, Edit Table, Clear Key and Clear Table.
The Popup
- Tables: click the table name at the top to open the list of tables, and pick one to see its keys. A field with no table yet starts in the table its object's other Localized Strings use.
- Keys: search by key, description or default-language text, which each key shows on its right. A click, or Enter, picks the key and closes the popup.
- None at the top of the list clears the key (the table stays).
- The pencil opens the table in the table window.
Key fields in Evo's own editors (such as the Localized Object's) open the same popup. Picking a key from another table there switches the table they read from too.
From Code
Code and network messages can't hold an asset, so they name a table by its GUID instead of its ID. A table keeps its GUID when it's renamed. Right-click any Localized String field and choose Copy as Code to get the line:
static readonly LocalizedString Play = new("84ccd2dd-010d-4d26-a070-c2e3b7b340ec", "menu.play");A Localized String made from a GUID keeps its table only while the game runs. To save one into an asset (from an editor script, for example), use the constructor that takes the table asset.
Public API
Constructors
| Constructor | Description |
|---|---|
LocalizedString(LocalizationTable table, string key) | The key of a table asset. |
LocalizedString(string tableGUID, string key) | The key of the table with that GUID. For code and network messages. |
Properties
| Property | Type | Description |
|---|---|---|
Key | string | The key of the entry. |
Table | LocalizationTable | The table that holds the key, or null when it has none or no table has its GUID. |
TableGUID | string | The table's GUID, as code and network messages name it. |
IsSet | bool | True when it names a table and a key. |
Methods
| Method | Description |
|---|---|
GetString() | The text in the current language, else the default language, else the key (with a warning, once). Outside Play Mode it reads the default language. Empty when no key is set. |
GetString(string fallback) | The same, but returns fallback instead of the key. |
TryGetString(out string value) | Gets the text in the current language, else the default language. False when neither has any. |
TryGetString(LocalizationLanguage language, out string value) | Gets the text in that language only. |
Localized Strings compare by table and key, so one made from a table asset equals one made from its GUID.
Example
using UnityEngine;
using TMPro;
using Evo.Localization;
public class ScoreLabel : MonoBehaviour
{
public TMP_Text label;
public LocalizedString title;
void OnEnable()
{
LocalizationManager.OnLanguageSet += Refresh;
Refresh(null);
}
void OnDisable() => LocalizationManager.OnLanguageSet -= Refresh;
void Refresh(LocalizationLanguage language) => label.text = title.GetString();
}