Class LocalizationService
Is responsible for connecting translation sources and localized components together. This service acts as a central hub. Essentially implementing a publisher-consumer pattern. Translation sources publish their translations to the service. Localized components read the corresponding values from the service.
Implements
Namespace: NoSuchStudio.Localization
Assembly: NoSuchStudio-Localization.dll
Syntax
[ExecuteAlways]
public class LocalizationService : Service<LocalizationService>, Service<LocalizationService>
Remarks
For common usage, you should use the static methods of this class.
Fields
ComponentMenuPath
Declaration
public const string ComponentMenuPath = "Localization"
Field Value
| Type | Description |
|---|---|
| String |
PlayerPrefKeyLocale
Declaration
public const string PlayerPrefKeyLocale = "nosuchstudio.localization.currentlocale"
Field Value
| Type | Description |
|---|---|
| String |
Properties
autoDetectLocale
Declaration
public bool autoDetectLocale { get; set; }
Property Value
| Type | Description |
|---|---|
| Boolean |
CurrentLanguage
Declaration
[Obsolete("use CurrentLocale instead")]
public static Locale CurrentLanguage { get; set; }
Property Value
| Type | Description |
|---|---|
| Locale | Return the current locale of the service, null if the service is not ready. |
CurrentLocale
Declaration
public static Locale CurrentLocale { get; set; }
Property Value
| Type | Description |
|---|---|
| Locale | Return the current locale of the service, null if the service is not ready. |
databasePreset
Declaration
public LocalizationService.DatabasePreset databasePreset { get; }
Property Value
| Type | Description |
|---|---|
| LocalizationService.DatabasePreset |
DefaultLanguage
Return the default locale of the service, null if the service is not ready.
Declaration
[Obsolete("use DefaultLocale instead")]
public static string DefaultLanguage { get; set; }
Property Value
| Type | Description |
|---|---|
| String |
DefaultLocale
Declaration
public static string DefaultLocale { get; set; }
Property Value
| Type | Description |
|---|---|
| String | Return the default locale of the service, null if the service is not ready. |
localeDatabase
Declaration
public LocaleDatabase localeDatabase { get; }
Property Value
| Type | Description |
|---|---|
| LocaleDatabase |
localeEnglishNames
Declaration
public IList<string> localeEnglishNames { get; }
Property Value
| Type | Description |
|---|---|
| IList<String> |
localeNames
Declaration
public IList<string> localeNames { get; }
Property Value
| Type | Description |
|---|---|
| IList<String> |
locales
Declaration
public List<Locale> locales { set; }
Property Value
| Type | Description |
|---|---|
| List<Locale> |
saveLocale
Declaration
public bool saveLocale { get; set; }
Property Value
| Type | Description |
|---|---|
| Boolean |
translationSources
Declaration
public Dictionary<string, Dictionary<string, ITranslationSource>> translationSources { get; }
Property Value
| Type | Description |
|---|---|
| Dictionary<String, Dictionary<String, ITranslationSource>> |
Methods
AddLanguageChangeListener(LocalizationService.LocaleChangeDelegate)
Add an event listener for when the current locale changes.
Declaration
[Obsolete("use AddLocaleChangeListener instead")]
public static void AddLanguageChangeListener(LocalizationService.LocaleChangeDelegate action)
Parameters
| Type | Name | Description |
|---|---|---|
| LocalizationService.LocaleChangeDelegate | action | Delegate to run when current Locale changes. |
AddLocaleChangeListener(LocalizationService.LocaleChangeDelegate)
Add an event listener for when the current locale changes.
Declaration
public static void AddLocaleChangeListener(LocalizationService.LocaleChangeDelegate action)
Parameters
| Type | Name | Description |
|---|---|---|
| LocalizationService.LocaleChangeDelegate | action |
AddLocalizationSource(String, String, ITranslationSource)
Called by translation sources to make their data available to the localization system. ITranslationSource
Declaration
public static void AddLocalizationSource(string phrase, string locale, ITranslationSource source)
Parameters
| Type | Name | Description |
|---|---|---|
| String | phrase | The phrase for which there is a translation. |
| String | locale | The locale for which there is a translation. |
| ITranslationSource | source | The translation source providing the translation of phrase in the given locale |
AddTranslationChangeListener(String, LocalizationService.TranslationChangeDelegate)
Declaration
public static void AddTranslationChangeListener(string phrase, LocalizationService.TranslationChangeDelegate action)
Parameters
| Type | Name | Description |
|---|---|---|
| String | phrase | |
| LocalizationService.TranslationChangeDelegate | action |
DetectAndApplySystemLocale(Boolean)
Set the NoSuchStudio.Localization.LocalizationService.currentLocale to the system locale if the system locale is recognized and has been enabled in the localization service.
Declaration
public bool DetectAndApplySystemLocale(bool useDefaultIfFailed = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Boolean | useDefaultIfFailed |
Returns
| Type | Description |
|---|---|
| Boolean | true if system locale was successfully applied, false otherwise. |
DoProcessChangedTranslations()
Declaration
public void DoProcessChangedTranslations()
GetLocaleDatabase()
Get the currently loaded locale database. Avoid modifying the database from code.
To modify the database, update the backing json file and reload the database from the Editor for LocalizationService
Declaration
public LocaleDatabase GetLocaleDatabase()
Returns
| Type | Description |
|---|---|
| LocaleDatabase | The current locale database. |
GetLocales()
Declaration
public IList<Locale> GetLocales()
Returns
| Type | Description |
|---|---|
| IList<Locale> |
GetPhraseTranslation(String)
Get the translation string for a given phrase.
Declaration
public static string GetPhraseTranslation(string phrase)
Parameters
| Type | Name | Description |
|---|---|---|
| String | phrase | phrase to look up. |
Returns
| Type | Description |
|---|---|
| String | The translation if phrase if found. An error string otherwise. |
GetPhraseTranslation(String, Locale)
Get the translation string for a given phrase in a given locale.
Declaration
public static string GetPhraseTranslation(string phrase, Locale locale)
Parameters
| Type | Name | Description |
|---|---|---|
| String | phrase | phrase to look up. |
| Locale | locale | locale for the look up. |
Returns
| Type | Description |
|---|---|
| String | The translation if phrase if found. An error string otherwise. |
Init()
Declaration
public void Init()
OnServiceRegister()
Declaration
public override void OnServiceRegister()
OnServiceUnregister()
Declaration
public override void OnServiceUnregister()
OnValidate()
Declaration
public void OnValidate()
ProcessChangedTranslations()
Declaration
public static void ProcessChangedTranslations()
RemoveLanguageChangeListener2(LocalizationService.LocaleChangeDelegate)
Remove an event listener for when the current locale changes.
Declaration
[Obsolete("use RemoveLocaleChangeListener instead")]
public static void RemoveLanguageChangeListener2(LocalizationService.LocaleChangeDelegate action)
Parameters
| Type | Name | Description |
|---|---|---|
| LocalizationService.LocaleChangeDelegate | action | Delegate to run when current Locale changes. |
RemoveLocaleChangeListener(LocalizationService.LocaleChangeDelegate)
Remove an event listener for when the current locale changes.
Declaration
public static void RemoveLocaleChangeListener(LocalizationService.LocaleChangeDelegate action)
Parameters
| Type | Name | Description |
|---|---|---|
| LocalizationService.LocaleChangeDelegate | action |
RemoveLocalizationSource(ITranslationSource)
Called by translation sources to remove their data from the localization service when they don't have their data anymore (reset or translations removed). ITranslationSource
Declaration
public static void RemoveLocalizationSource(ITranslationSource source)
Parameters
| Type | Name | Description |
|---|---|---|
| ITranslationSource | source | The translation source to be removed |
RemoveLocalizationSource(String, String, ITranslationSource)
Called by translation sources to remove their data from the localization service when they go offline (get disabled or destroyed or reset or translations removed). ITranslationSource
Declaration
public static void RemoveLocalizationSource(string phrase, string locale, ITranslationSource source)
Parameters
| Type | Name | Description |
|---|---|---|
| String | phrase | The phrase for which there is a translation. |
| String | locale | The locale for which there is a translation. |
| ITranslationSource | source | The translation source providing the translation of phrase in the given locale |
RemoveTranslationChangeListener(String, LocalizationService.TranslationChangeDelegate)
Declaration
public static void RemoveTranslationChangeListener(string phrase, LocalizationService.TranslationChangeDelegate action)
Parameters
| Type | Name | Description |
|---|---|---|
| String | phrase | |
| LocalizationService.TranslationChangeDelegate | action |
Reset()
Declaration
public void Reset()