Show / Hide Table of Contents

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
System.Object
LocalizationBucket
Namespace: GlitchedPolygons.Localization
Assembly: UnityGlitchedLocaleClientDocs.dll
Syntax
public class LocalizationBucket : MonoBehaviour

Fields

| Improve this Doc View Source

DEFAULT_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
| Improve this Doc View Source

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 Source

Item[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 Source

AddLocale(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: en_US.UTF-8 for American English, en_GB.UTF-8 for British English, etc...

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.

| Improve this Doc View Source

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

true if the server was changed, false if no changes were made.

| Improve this Doc View Source

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>

| Improve this Doc View Source

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.

| Improve this Doc View Source

LoadCacheFromDisk()

Loads the localization dictionary cache from disk into the LocalizationBucket instance.

Declaration
public void LoadCacheFromDisk()
| Improve this Doc View Source

Refresh()

Refreshes the translations, contacting the locale server if required.

Declaration
public void Refresh()
| Improve this Doc View Source

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: en_US.UTF-8 for American English, en_GB.UTF-8 for British English, etc...

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.

| Improve this Doc View Source

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. en_US.UTF-8). This value must be in the list of enabled locales (use GetLocales() to find out which locales are currently enabled in the LocalizationBucket).

Returns
Type Description
System.Boolean

Whether or not the locale change was successfully applied.

| Improve this Doc View Source

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

null if the translation couldn't be found in the LocalizationBucket's dictionary; the translated string value otherwise.

| Improve this Doc View Source

WriteCacheToDisk()

Forces the localization cache to be written to disk immediately.

Declaration
public void WriteCacheToDisk()

Events

| Improve this Doc View Source

ChangedLocale

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>
| Improve this Doc View Source

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
| Improve this Doc View Source

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
  • Improve this Doc
  • View Source
Back to top Generated by DocFX