diff --git a/osu.Framework.Tests/Localisation/LocalisationTest.cs b/osu.Framework.Tests/Localisation/LocalisationTest.cs index 37f9288add..f689e926e3 100644 --- a/osu.Framework.Tests/Localisation/LocalisationTest.cs +++ b/osu.Framework.Tests/Localisation/LocalisationTest.cs @@ -58,6 +58,32 @@ public void TestConfigSettingRetainedWhenAddingNewLanguage() Assert.AreEqual(FakeStorage.LOCALISABLE_STRING_JA_JP, localisedText.Value); } + [Test] + public void TestConfigSettingRetainedWhenAddingLocaleMappings() + { + config.SetValue(FrameworkSetting.Locale, "ja-JP"); + + // ensure that adding a new language which doesn't match the user's choice doesn't cause the configuration value to get reset. + manager.AddLocaleMappings(new[] + { + new LocaleMapping("po", new FakeStorage("po-OP")), + new LocaleMapping("wa", new FakeStorage("wa-NG")) + }); + + Assert.AreEqual("ja-JP", config.Get(FrameworkSetting.Locale)); + + var localisedText = manager.GetLocalisedBindableString(new TranslatableString(FakeStorage.LOCALISABLE_STRING_EN, FakeStorage.LOCALISABLE_STRING_EN)); + Assert.AreEqual(FakeStorage.LOCALISABLE_STRING_EN, localisedText.Value); + + // ensure that if the user's selection is added in a further AddLanguage call, the manager correctly translates strings. + manager.AddLocaleMappings(new[] + { + new LocaleMapping("ja-JP", new FakeStorage("ja-JP")) + }); + + Assert.AreEqual(FakeStorage.LOCALISABLE_STRING_JA_JP, localisedText.Value); + } + [Test] public void TestNotLocalised() { @@ -122,7 +148,8 @@ public void TestFormattedInterpolation() string expectedResult = string.Format(FakeStorage.LOCALISABLE_FORMAT_STRING_JA, arg_0); - var formattedText = manager.GetLocalisedBindableString(new TranslatableString(FakeStorage.LOCALISABLE_FORMAT_STRING_EN, interpolation: $"The {arg_0} fallback should only matches argument count")); + var formattedText = manager.GetLocalisedBindableString(new TranslatableString(FakeStorage.LOCALISABLE_FORMAT_STRING_EN, + interpolation: $"The {arg_0} fallback should only matches argument count")); Assert.AreEqual(expectedResult, formattedText.Value); } diff --git a/osu.Framework/Localisation/LocaleMapping.cs b/osu.Framework/Localisation/LocaleMapping.cs new file mode 100644 index 0000000000..6827480c7e --- /dev/null +++ b/osu.Framework/Localisation/LocaleMapping.cs @@ -0,0 +1,36 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +namespace osu.Framework.Localisation +{ + /// + /// Maps a localisation store to a lookup string. + /// Used by . + /// + public class LocaleMapping + { + public readonly string Name; + public readonly ILocalisationStore Storage; + + /// + /// Create a locale mapping from a localisation store. + /// + /// The store to be used. + public LocaleMapping(ILocalisationStore store) + { + Name = store.EffectiveCulture.Name; + Storage = store; + } + + /// + /// Create a locale mapping with a custom lookup name. + /// + /// + /// The store to be used. + public LocaleMapping(string name, ILocalisationStore store) + { + Name = name; + Storage = store; + } + } +} diff --git a/osu.Framework/Localisation/LocalisationManager.cs b/osu.Framework/Localisation/LocalisationManager.cs index e0971f91f5..262614a506 100644 --- a/osu.Framework/Localisation/LocalisationManager.cs +++ b/osu.Framework/Localisation/LocalisationManager.cs @@ -28,6 +28,23 @@ public LocalisationManager(FrameworkConfigManager config) configPreferUnicode.BindValueChanged(_ => UpdateLocalisationParameters(), true); } + /// + /// Add multiple locale mappings. Should be used to add all available languages at initialisation. + /// + /// All available locale mappings. + public void AddLocaleMappings(IEnumerable mappings) + { + locales.AddRange(mappings); + configLocale.TriggerChange(); + } + + /// + /// Add a single language to this manager. + /// + /// + /// Use as a more efficient way of bootstrapping all available locales. + /// The culture name to be added. Generally should match . + /// A storage providing localisations for the specified language. public void AddLanguage(string language, ILocalisationStore storage) { locales.Add(new LocaleMapping(language, storage)); @@ -102,17 +119,5 @@ private void updateLocale(ValueChangedEvent locale) /// /// The resultant . protected virtual LocalisationParameters CreateLocalisationParameters() => new LocalisationParameters(currentLocale?.Storage, configPreferUnicode.Value); - - private class LocaleMapping - { - public readonly string Name; - public readonly ILocalisationStore Storage; - - public LocaleMapping(string name, ILocalisationStore storage) - { - Name = name; - Storage = storage; - } - } } }