Version: Unity 6.7 Alpha (6000.7)
LanguageEnglish
  • C#

LocalizationSettings.EvaluateStartupLocale

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public Locale EvaluateStartupLocale(Func<IStartupLocaleSelector, bool> filter);

Parameters

Parameter Description
filter Returns true for each selector to evaluate, or null to evaluate them all.

Returns

Locale The locale the chain produces, or null when there is no project locale either.

Description

Runs the startup selector chain and returns the locale it produces, without selecting it.

This is the same evaluation LocalizationSettings.InitializeAsync performs, so the result is directly comparable with LocalizationSettings.SelectedLocale. Pass a filter to leave selectors out: excluding the one that applies a saved user choice gives the locale the project would have started with, which is the value to show as the default and to restore when the user resets. Falls back to LocalizationSettings.ProjectLocale when no selector matches. Selectors are asked again on every call, so a selector with side effects sees more than one call.

Additional resources: LocalizationSettings.StartupSelector, LocalizationSettings.StartupSelectors

<para>Show the saved language alongside the one the project would otherwise start in.</para>

using Unity.Localization;
using UnityEngine;

namespace Unity.Localization.Samples { public class EvaluateStartupLocaleExample : MonoBehaviour { void Start() { var settings = LocalizationSettings.Instance;

// What the project would start in with the saved choice ignored, for a "Reset to default" button. var fallback = settings.EvaluateStartupLocale(selector => selector is not PlayerPrefLocaleSelector);

Debug.Log($"Playing in {LocalizationSettings.SelectedLocale?.LocaleName}, default is {fallback?.LocaleName}"); if (settings.StartupSelector is PlayerPrefLocaleSelector) Debug.Log("The language came from a saved choice rather than the project default."); }

public void ResetToDefault() { var settings = LocalizationSettings.Instance; LocalizationSettings.SelectedLocale = settings.EvaluateStartupLocale(selector => selector is not PlayerPrefLocaleSelector); } } }