A game framework written with osu! in mind.
at master 2.5 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4#pragma warning disable 8632 // TODO: can be #nullable enable when Bindables are updated to also be. 5 6using osu.Framework.Bindables; 7 8namespace osu.Framework.Localisation 9{ 10 public partial class LocalisationManager 11 { 12 private class LocalisedBindableString : Bindable<string>, ILocalisedBindableString 13 { 14 private IBindable<LocalisationParameters> parameters; 15 16 private LocalisableString text; 17 18 private readonly LocalisationManager manager; 19 20 public LocalisedBindableString(LocalisableString text, LocalisationManager manager) 21 { 22 this.text = text; 23 this.manager = manager; 24 25 updateValue(); 26 } 27 28 private void updateValue() 29 { 30 switch (text.Data) 31 { 32 case string plain: 33 Value = plain; 34 break; 35 36 case ILocalisableStringData data: 37 if (parameters == null) 38 { 39 parameters = new Bindable<LocalisationParameters>(); 40 parameters.BindTo(manager.currentParameters); 41 parameters.BindValueChanged(_ => updateValue()); 42 } 43 44 Value = data.GetLocalised(parameters.Value); 45 break; 46 47 default: 48 Value = string.Empty; 49 break; 50 } 51 } 52 53 LocalisableString ILocalisedBindableString.Text 54 { 55 set 56 { 57 if (text.Equals(value)) 58 return; 59 60 text = value; 61 62 updateValue(); 63 } 64 } 65 66 internal override void UnbindAllInternal() 67 { 68 base.UnbindAllInternal(); 69 70 // optimisation to ensure cleanup happens aggressively. 71 // without this, the central parameters bindable's internal WeakList can balloon out of control due to the 72 // weak reference cleanup only occurring on Value retrieval (which rarely/never happens in this case). 73 parameters?.UnbindAll(); 74 } 75 } 76 } 77}