Class UnityLocalizationService
Unity MonoBehaviour implementation of the localization service that manages game and voice language settings.
Integrates with Unity's Localization system to handle locale switching and responds to game settings changes through event dispatching.
Namespace: Serenity.Localization.Infrastructure.Services
Assembly: Serenity.UnityLocalization.Infrastructure.dll
Syntax
public class UnityLocalizationService : MonoBehaviour, ILocalizationService, IService, IFoundationSettings
Constructors
UnityLocalizationService()
Declaration
public UnityLocalizationService()
Properties
Guid
Declaration
public string Guid { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
Id
Declaration
public string Id { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
LogService
Declaration
public ILogService LogService { get; set; }
Property Value
| Type | Description |
|---|---|
| ILogService |
Methods
GetGameLanguage()
Gets the currently selected game language code from Unity's localization settings.
Returns the identifier code of the currently active locale.
Declaration
public string GetGameLanguage()
Returns
| Type | Description |
|---|---|
| string | Current game language code, or null if no locale is selected |
GetStringTableReference()
Declaration
public string GetStringTableReference()
Returns
| Type | Description |
|---|---|
| string |
GetVoiceLanguage()
Gets the currently selected voice language code.
Returns the cached voice language value that was set through SetVoiceLanguage.
Declaration
public string GetVoiceLanguage()
Returns
| Type | Description |
|---|---|
| string | Current voice language code |
InitializeEventDispatcher(IEventDispatcherService)
Initializes the event dispatcher service and subscribes to relevant events.
Sets up event handling for game settings changes that affect localization.
Declaration
public void InitializeEventDispatcher(IEventDispatcherService eventDispatcherService)
Parameters
| Type | Name | Description |
|---|---|---|
| IEventDispatcherService | eventDispatcherService | Event dispatcher service for handling localization-related events |
InitializeService(ILocalizationSettings, IGameSettingsService)
Initializes the localization service with the provided settings and dependencies.
Sets up language keys, localization tables, and establishes default language values from game settings.
Declaration
public void InitializeService(ILocalizationSettings settings, IGameSettingsService gameSettingsService)
Parameters
| Type | Name | Description |
|---|---|---|
| ILocalizationSettings | settings | Localization settings containing language keys and tables |
| IGameSettingsService | gameSettingsService | Game settings service for accessing language configuration |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when configured game languages are not found in available locales |
SetEventDispatcherService(IEventDispatcherService)
Sets the event dispatcher service reference for handling localization events.
Stores the service instance for later use in event subscription and handling.
Declaration
public void SetEventDispatcherService(IEventDispatcherService eventDispatcherService)
Parameters
| Type | Name | Description |
|---|---|---|
| IEventDispatcherService | eventDispatcherService | Event dispatcher service to set for this localization service |
SetGameLanguage(LocalizationSetGameLanguageInput)
Sets the game language by switching Unity's selected locale to match the specified language.
Uses a coroutine to handle the asynchronous locale switching operation.
Declaration
public void SetGameLanguage(LocalizationSetGameLanguageInput input)
Parameters
| Type | Name | Description |
|---|---|---|
| LocalizationSetGameLanguageInput | input | DTO containing the language code to set as the new game language |
SetVoiceLanguage(LocalizationSetVoiceLanguageInput)
Sets the voice language by storing the language code for voice asset localization.
Currently only caches the value without additional processing.
Declaration
public void SetVoiceLanguage(LocalizationSetVoiceLanguageInput input)
Parameters
| Type | Name | Description |
|---|---|---|
| LocalizationSetVoiceLanguageInput | input | DTO containing the language code to set as the new voice language |
SubscribeToEventDispatcherEvents()
Subscribes to event dispatcher events related to localization settings changes.
Currently subscribes to game settings value updates to automatically sync language changes.
Declaration
public void SubscribeToEventDispatcherEvents()
TryTranslate(string, out string)
Always returns false: in Addressables-using modes (where this service is
active) translation is performed by LocalizeStringEvent.RefreshString on the
TMP updater, NOT through a direct service lookup. The TMP updater branches on
GetStringTableReference() returning a non-empty value and uses that
path — it only falls back to TryTranslate(string, out string) when the reference is empty
(i.e. the SerenityFallbackLocalizationService is active in
ResourcesOnly/WebGL mode).
Implemented for interface conformance only — no Addressables call, no
StringDatabase.GetLocalizedStringAsync WaitForCompletion, nothing that could
stall the main thread.
Declaration
public bool TryTranslate(string key, out string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | key | |
| string | value |
Returns
| Type | Description |
|---|---|
| bool |
Events
GameLanguageChanged
Declaration
public event Action GameLanguageChanged
Event Type
| Type | Description |
|---|---|
| Action |
Remarks
Slice BB: declared for interface conformance, intentionally never invoked.
In Addressables mode UI translation refresh is handled by
LocalizeStringEvent's own subscription to
LocalizationSettings.OnSelectedLocaleChanged — raising a parallel
signal here would cause the TMP updater (if it ever subscribed in this mode)
to refresh twice and corrupt the table-entry reference. The TMP updater
subscribes only in fallback mode for exactly this reason; this event is left
as the no-op the contract permits.