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.
Inheritance
UnityLocalizationService
Assembly: Serenity.UnityLocalization.Infrastructure.dll
Syntax
public class UnityLocalizationService : MonoBehaviour, ILocalizationService, IService, IFoundationSettings
Constructors
UnityLocalizationService()
Declaration
public UnityLocalizationService()
Fields
_currentVoiceLanguage
Declaration
protected string _currentVoiceLanguage
Field Value
_eventDispatcherService
Declaration
protected IEventDispatcherService _eventDispatcherService
Field Value
_gameLanguageKey
Declaration
protected string _gameLanguageKey
Field Value
_gameLocalizationTableCollection
Declaration
protected UnityLocalizationStringTable _gameLocalizationTableCollection
Field Value
_gameSettingsService
Declaration
protected IGameSettingsService _gameSettingsService
Field Value
_guid
Declaration
Field Value
_id
Declaration
Field Value
_logService
Declaration
protected ILogService _logService
Field Value
_voiceLanguageKey
Declaration
protected string _voiceLanguageKey
Field Value
_voiceLocalizationTableCollection
Declaration
protected UnityLocalizationAssetTable _voiceLocalizationTableCollection
Field Value
Properties
Guid
Declaration
public string Guid { get; set; }
Property Value
Id
Declaration
public string Id { get; set; }
Property Value
LogService
Declaration
public ILogService LogService { get; set; }
Property Value
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 virtual string GetGameLanguage()
Returns
| Type |
Description |
| string |
Current game language code, or null if no locale is selected
|
GetStringTableReference()
Declaration
public virtual string GetStringTableReference()
Returns
GetVoiceLanguage()
Gets the currently selected voice language code.
Returns the cached voice language value that was set through SetVoiceLanguage.
Declaration
public virtual 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 virtual 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
|
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 virtual void SetGameLanguage(LocalizationSetGameLanguageInput input)
Parameters
Sets the voice language by storing the language code for voice asset localization.
Currently only caches the value without additional processing.
Declaration
public virtual void SetVoiceLanguage(LocalizationSetVoiceLanguageInput input)
Parameters
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 virtual bool TryTranslate(string key, out string value)
Parameters
Returns
Events
GameLanguageChanged
Declaration
public event Action GameLanguageChanged
Event Type
Implements