Skip to content

Library Documentation

Notest edited this page May 28, 2024 · 4 revisions

Keys

Keys are identifiers to translations for each locale. They should stay consistent through every language, assuming that a translation was added to the key of course.
Internally, all keys are strings, but you can call the methods with a Localization.Keys enum and they will get converted to a string with ToString. All of the following methods that take string key as their arguments have Localization.Keys enum overloads.
Some short examples are given alternatives with comments, but it doesn't mean that the ones without alternatives in the example code don't have overloads.

GetCreateLocale

Gets a locale, will create one if it doesn't exist.
You may call the method with a CultureInfo or a LocaleIdentifier, keep in mind the CultureInfo gets converted to a LocaleIdentifier internally.

Example:

private void Awake()
{
    /* 
     * Creates a culture info from a given language tag
     * There are multiple language tag sources on the internet
     * One source is from Microsoft itself: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/a9eac961-e77d-41a6-90a5-ce1a8b0cdb9c
     */

    CultureInfo cultureInfo = new CultureInfo("pt-BR"); // Creates a culture info from a given language tag
    Locale locale = LocalizationHandler.GetCreateLocale(cultureInfo);

    // Alternatively:
    // There is not much reason to do this unless you need a LocaleIdentifier  

    LocaleIdentifier localeIdentifier = new LocaleIdentifier(cultureInfo);
    Locale locale = LocalizationHandler.GetCreateLocale(cultureInfo);

AddKeyTranslationForLocale

Adds a translation with a key to a given locale, will not replace an existing translation.

Example:

private void Awake()
{
    CultureInfo cultureInfo = new CultureInfo("pt-BR"); // Creates a culture info from a given language tag
    Locale locale = LocalizationHandler.GetCreateLocale(cultureInfo);
    LocalizationHandler.AddKeyTranslationForLocale(locale, "Sleep", "Dormir"); // Adds translation for the "Sleep" key which is used in-game, this is a valid translation and would show up if the game has an option for Brazilian Portuguese locale!

    // Alternatively:
    LocalizationHandler.AddKeyTranslationForLocale(locale, LocalizationKeys.Keys.Sleep, "Dormir");

    // Alternatively:
    LocalizationHandler.AddKeyTranslationForLocale(locale, Enum.Parse<LocalizationKeys.Keys>("Sleep"), "Dormir");
}

AddKeyTranslationsForLocale

Adds multiple translations with the specified keys to a given locale. Will not replace existing translations.

Example:

private void Awake()
{
    Dictionary<string, string> translationDictionary = new Dictionary<string, string>()
    {
        {"Sleep", "Dormir"},
        {"Save", "Salvar"},
    };

    CultureInfo cultureInfo = new CultureInfo("pt-BR"); // Creates a culture info from a given language tag
    Locale locale = LocalizationHandler.GetCreateLocale(cultureInfo);
    LocalizationHandler.AddKeyTranslationsForLocale(locale, translationDictionary); // These are valid translations and would show up if the game has an option for Brazilian Portuguese locale!
}

AddKeyTranslationForAllLocales

Same as AddKeyTranslationForLocale but adds it to all available locales instead.

Example:

private void Awake()
{
    LocalizationHandler.AddKeyTranslationForAllLocales("Sleep", "Dormir"); // Adds translation for the "Sleep" key which is used in-game, this is a valid translation and would show up if the game has an option for Brazilian Portuguese locale!

    // Alternatively:
    LocalizationHandler.AddKeyTranslationForAllLocales(LocalizationKeys.Keys.Sleep, "Dormir");

    // Alternatively:
    LocalizationHandler.AddKeyTranslationForAllLocales(Enum.Parse<LocalizationKeys.Keys>("Sleep"), "Dormir");
}

AddKeyTranslationsForAllLocales

Same as AddKeyTranslationsForLocale but adds it to all available locales instead.

Example:

private void Awake()
{
    Dictionary<string, string> translationDictionary = new Dictionary<string, string>()
    {
        {"Sleep", "Dormir"},
        {"Save", "Salvar"},
    };

    LocalizationHandler.AddKeyTranslationsForAllLocales(translationDictionary); // These are valid translations and would show up if the game has an option for Brazilian Portuguese locale!
}

SetKeyTranslationForLocale

Replaces a translation with a specified key from a locale.

Example:

private void Awake()
{
    CultureInfo cultureInfo = new CultureInfo("pt-BR"); // Creates a culture info from a given language tag
    Locale locale = LocalizationHandler.GetCreateLocale(cultureInfo);
    LocalizationHandler.AddKeyTranslationForLocale(locale, "Views", "Visualisassoes"); // This translation is wrong but valid and would still show up.
    LocalizationHandler.SetKeyTranslationForLocale(locale, "Views", "Visualizações"); // This translation is correct and still valid!
}

SetKeyTranslationsForLocale

Replaces multiple translations with the specified keys from a given locale.

Example:

private void Awake()
{
    Dictionary<string, string> translationDictionary = new Dictionary<string, string>()
    {
        { "Sleep", "Durmir" },
        { "Save", "Sauva" },
    };

    CultureInfo cultureInfo = new CultureInfo("pt-BR"); // Creates a culture info from a given language tag
    Locale locale = LocalizationHandler.GetCreateLocale(cultureInfo);
    LocalizationHandler.AddKeyTranslationsForLocale(locale, translationDictionary); // These are wrong but valid translations and would still show up!

    Dictionary<string, string> correctTranslationDictionary = new Dictionary<string, string>()
    {
        { "Sleep", "Dormir" },
        { "Save", "Salvar" },
    };
    LocalizationHandler.SetKeyTranslationsForLocale(locale, correctTranslationDictionary); // These are correct and still valid!
}

SetKeyTranslationForAllLocales

Same as SetKeyTranslationForLocale but sets to all available locales instead.

Example:

private void Awake()
{
    LocalizationHandler.AddKeyTranslationForAllLocales("Views", "Visualisassoes"); // This translation is wrong but valid and would still show up.
    LocalizationHandler.SetKeyTranslationForAllLocales("Views", "Visualizações"); // This translation is correct and still valid!
}

SetKeyTranslationsForAllLocales

Same as SetKeyTranslationsForLocale but sets to all available locales instead.

Example:

private void Awake()
{
    Dictionary<string, string> translationDictionary = new Dictionary<string, string>()
    {
        { "Sleep", "Durmir" },
        { "Save", "Sauva" },
    };

    LocalizationHandler.AddKeyTranslationsForAllLocales(translationDictionary); // These are wrong but valid translations and would still show up!

    Dictionary<string, string> correctTranslationDictionary = new Dictionary<string, string>()
    {
        { "Sleep", "Dormir" },
        { "Save", "Salvar" },
    };
    LocalizationHandler.SetKeyTranslationsForAllLocales(correctTranslationDictionary); // These are correct and still valid!
}

SetKeyTranslationsDictionaryForLocale

Replaces the entire locale's dictionary for the given one, or adds a dictionary to the given locale if it doesn't exist.

Example:

private void Awake()
{
    CultureInfo cultureInfo = new CultureInfo("pt-BR"); // Creates a culture info from a given language tag
    Locale locale = LocalizationHandler.GetCreateLocale(cultureInfo);
    LocalizationHandler.AddKeyTranslationForLocale(locale, "Money", "Dinhero"); // This is wrong but is a valid translation and would still show up!

    Dictionary<string, string> correctTranslationDictionary = new Dictionary<string, string>()
    {
        { "Sleep", "Dormir" },
        { "Save", "Salvar" },
    };

    LocalizationHandler.SetKeyTranslationsDictionaryForLocale(locale, correctTranslationDictionary); // These are correct and still valid!
    /*
    * If this was merely SetKeyTranslationsForLocale it would error, as the keys wouldn't exist, but we are replacing the dictionary itself
    * (and adding if it doesn't exist)
    */
}

GetLocalizedString

Gets translation string from the current language dictionary with a given key, may return null if translation doesn't exist.

Example:

private void Awake()
{
    CultureInfo cultureInfo = new CultureInfo("pt-BR"); // Creates a culture info from a given language tag
    Locale locale = LocalizationHandler.GetCreateLocale(cultureInfo);
    LocalizationHandler.AddKeyTranslationForLocale(locale, "Sleep", "Dormir"); // Adds translation for the "Sleep" key which is used in-game, this is a valid translation and would show up if the game has an option for Brazilian Portuguese locale!
    string? translation = LocalizationHandler.GetLocalizedString("Sleep"); // Will get the translation for the "Sleep" key from the current game language being used, if the current game language is pt-BR it will return "Dormir"

    // Alternatively:
    LocalizationHandler.GetLocalizedString(LocalizationKeys.Keys.Sleep);

    // Alternatively:
    LocalizationHandler.GetLocalizedString(Enum.Parse<LocalizationKeys.Keys>("Sleep"));
}

LocaleAdded

Fired whenever a new locale is added to the AvailableLocales list through the library.
Pretty much allows you to retroactively add to locales.

Example:

private void Awake()
{
    LocalizationHandler.AddKeyTranslationForAllLocales("Sleep", "Dormir"); // Adds translation for the "Sleep" key which is used in-game, this is a valid translation and would show up if the game has an option for Brazilian Portuguese locale!
    LocalizationHandler.LocaleAdded += delegate(Locale locale)
    {
        LocalizationHandler.AddKeyTranslationForLocale(locale, "Sleep", "Dormir");
    };
}