Class LocalizationBucket
A localization bucket is a component that contains a dictionary of localized key-value string pairs for a list of user-defined locales.
It connects to an instance of the Glitched Locale Server to allow users to change translations at runtime without requiring to submit a new package update/release everytime a typo is corrected...
LocalizedText More information available on: https://glitchedpolygons.com/store/software/glitched-locale-server
Inheritance
Namespace: GlitchedPolygons.Localization
Assembly: UnityGlitchedLocaleClientDocs.dll
Syntax
public class LocalizationBucket : MonoBehaviour
Fields
| Improve this Doc View SourceDEFAULT_LOCALE_SERVER_BASE_URL
Default base URL that points to the official Glitched Polygons Locale Server. Its frontend is reachable under: https://locales.glitchedpolygons.com
Declaration
public const string DEFAULT_LOCALE_SERVER_BASE_URL = "https://api.locales.glitchedpolygons.com"
Field Value
Type | Description |
---|---|
System.String |
DEFAULT_LOCALE_SERVER_TRANSLATION_ENDPOINT
Default endpoint path to use for fetching translations from the server.
Declaration
public const string DEFAULT_LOCALE_SERVER_TRANSLATION_ENDPOINT = "/api/v1/translations/translate"
Field Value
Type | Description |
---|---|
System.String |
Properties
| Improve this Doc View SourceItem[String]
Useful indexer for getting a translation from the bucket directly with the square brackets operator.
Declaration
public string this[string key] { get; }
Parameters
Type | Name | Description |
---|---|---|
System.String | key | Translation key. |
Property Value
Type | Description |
---|---|
System.String |
Methods
| Improve this Doc View SourceAddLocale(String)
Adds a new locale to the LocalizationBucket.
Declaration
public bool AddLocale(string locale)
Parameters
Type | Name | Description |
---|---|---|
System.String | locale | Locale string. For example: |
Returns
Type | Description |
---|---|
System.Boolean | Whether or not the locale insertion was successful. |
Remarks
Note: this will NOT trigger a Refresh()! You need to call that method manually once the call to this one here succeeds.
ChangeServer(String, String)
Change the locale server to fetch translations from. This will trigger a Refresh()! Make sure that the new server you want to point the LocalizationBucket to is online and reachable!
Declaration
public bool ChangeServer(string newLocaleServerBaseUrl = "https://api.locales.glitchedpolygons.com", string translationEndpoint = "/api/v1/translations/translate")
Parameters
Type | Name | Description |
---|---|---|
System.String | newLocaleServerBaseUrl | The new base URL |
System.String | translationEndpoint |
Returns
Type | Description |
---|---|
System.Boolean |
|
GetLocales()
Gets the list of currently enabled locales in the LocalizationBucket
Declaration
public IReadOnlyCollection<string> GetLocales()
Returns
Type | Description |
---|---|
System.Collections.Generic.IReadOnlyCollection<System.String> | System.Collections.Generic.IReadOnlyCollection<T> |
IsLocaleServerReachable()
Checks whether or not the locale server defined in the LocalizationBucket's base URL field is online and reachable.
Declaration
public Task<HttpResponseMessage> IsLocaleServerReachable()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> | System.Threading.Tasks.Task<TResult> - Use System.Net.Http.HttpResponseMessage.System.Net.Http.HttpResponseMessage.IsSuccessStatusCode to find out whether or not the server is reachable. The returned response body furthermore contains the server's public RSA key. |
LoadCacheFromDisk()
Loads the localization dictionary cache from disk into the LocalizationBucket instance.
Declaration
public void LoadCacheFromDisk()
Refresh()
Refreshes the translations, contacting the locale server if required.
Declaration
public void Refresh()
RemoveLocale(String)
Removes a locale from the list of enabled locales in the LocalizationBucket.
Declaration
public bool RemoveLocale(string locale)
Parameters
Type | Name | Description |
---|---|---|
System.String | locale | Locale string. For example: |
Returns
Type | Description |
---|---|
System.Boolean | Whether or not the locale removal was successful. |
Remarks
Note: this will NOT trigger a Refresh()! You need to call that method manually once the call to this one here succeeds.
SetLocale(String)
Changes the LocalizationBucket's active locale setting.
The ChangedLocale event will be raised and all the LocalizedTexts in the scene will need to refresh their UI labels as well as all the other scripts that make use of translations from this bucket.
Declaration
public bool SetLocale(string locale)
Parameters
Type | Name | Description |
---|---|---|
System.String | locale | The new locale to use (e.g. |
Returns
Type | Description |
---|---|
System.Boolean | Whether or not the locale change was successfully applied. |
Translate(String)
Gets the translated string value for a specific translation key.
Note that translation keys must be in the LocalizationBucket's list of translation keys in order for them to be fetched from the locale server!
Use the LocalizationBucket's inspector for that: there is a context menu action that scans the scene for missing keys. Use it every time you added new translation usages to your scene to prevent untranslated values in the final product!
Declaration
public string Translate(string key)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | Translation key. |
Returns
Type | Description |
---|---|
System.String |
|
WriteCacheToDisk()
Forces the localization cache to be written to disk immediately.
Declaration
public void WriteCacheToDisk()
Events
| Improve this Doc View SourceChangedLocale
This event is raised whenever the SetLocale(String) method is called, such that interested subscribers know when to refresh their labels in the UI to reflect the new language setting.
Declaration
public event Action<string> ChangedLocale
Event Type
Type | Description |
---|---|
System.Action<System.String> |
FailedConnectionToLocaleServer
This event is raised when a connection attempt to the locale server resulted in a failure.
Declaration
public event Action FailedConnectionToLocaleServer
Event Type
Type | Description |
---|---|
System.Action |
Refreshed
This event is raised when the LocalizationBucket refreshed its dictionary of translations.
Interested scripts should subscribe to this event in order to know that they should refresh their labels/UI/usages of the translations.
Declaration
public event Action Refreshed
Event Type
Type | Description |
---|---|
System.Action |