|
| 1 | +using System.Collections.Generic; |
| 2 | +using UnityEngine.Localization.Pseudo; |
| 3 | +using UnityEngine.Localization.Settings; |
| 4 | +using UnityEngine.UIElements; |
| 5 | + |
| 6 | +namespace UnityEngine.Localization |
| 7 | +{ |
| 8 | + [UxmlElement] |
| 9 | + public partial class LanguageSelection : PopupField<Locale> |
| 10 | + { |
| 11 | + [UxmlAttribute] |
| 12 | + public string tableName; |
| 13 | + |
| 14 | + [UxmlAttribute] |
| 15 | + public bool showPseudoLocales; |
| 16 | + |
| 17 | + public LanguageSelection() |
| 18 | + { |
| 19 | + RegisterCallback<AttachToPanelEvent>(OnAttachToPanel); |
| 20 | + } |
| 21 | + |
| 22 | + void OnAttachToPanel(AttachToPanelEvent evt) |
| 23 | + { |
| 24 | + formatListItemCallback = FormatLocaleName; |
| 25 | + formatSelectedValueCallback = FormatLocaleName; |
| 26 | + |
| 27 | + if (LocalizationSettings.InitializationOperation.IsDone) |
| 28 | + SetupLocalization(); |
| 29 | + else |
| 30 | + LocalizationSettings.InitializationOperation.Completed += (s) => SetupLocalization(); |
| 31 | + |
| 32 | + RegisterCallback<ChangeEvent<Locale>>(evt => |
| 33 | + { |
| 34 | + LocalizationSettings.SelectedLocale = evt.newValue; |
| 35 | + }); |
| 36 | + |
| 37 | + LocalizationSettings.SelectedLocaleChanged += (s) => SetValueWithoutNotify(LocalizationSettings.SelectedLocale); |
| 38 | + } |
| 39 | + |
| 40 | + void SetupLocalization() |
| 41 | + { |
| 42 | + var choices = new List<Locale>(); |
| 43 | + foreach (var locale in LocalizationSettings.AvailableLocales.Locales) |
| 44 | + { |
| 45 | + if (locale is PseudoLocale) |
| 46 | + { |
| 47 | + if (showPseudoLocales) |
| 48 | + { |
| 49 | + choices.Add(locale); |
| 50 | + } |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + choices.Add(locale); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + this.choices = choices; |
| 59 | + |
| 60 | + // Schedule to avoid the rentering update method exception when called from addressables event. |
| 61 | + schedule.Execute(() => SetValueWithoutNotify(LocalizationSettings.SelectedLocale)); |
| 62 | + } |
| 63 | + |
| 64 | + string FormatLocaleName(Locale locale) |
| 65 | + { |
| 66 | + if (locale == null) |
| 67 | + return "None"; |
| 68 | + if (string.IsNullOrEmpty(tableName)) |
| 69 | + return locale.LocaleName; |
| 70 | + return LocalizationSettings.StringDatabase.GetLocalizedString(tableName, "LANGUAGE_" + locale.LocaleName.ToUpper()); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments