Skip to content

Commit

Permalink
Merge pull request #5276 from peppy/localisation-mass-language-add
Browse files Browse the repository at this point in the history
Allow initial locale mappings to be added in a single call
  • Loading branch information
smoogipoo authored Jun 28, 2022
2 parents 34a5524 + 9e9e476 commit e3eae43
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 13 deletions.
29 changes: 28 additions & 1 deletion osu.Framework.Tests/Localisation/LocalisationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(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()
{
Expand Down Expand Up @@ -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);
}
Expand Down
36 changes: 36 additions & 0 deletions osu.Framework/Localisation/LocaleMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

namespace osu.Framework.Localisation
{
/// <summary>
/// Maps a localisation store to a lookup string.
/// Used by <see cref="LocalisationManager"/>.
/// </summary>
public class LocaleMapping
{
public readonly string Name;
public readonly ILocalisationStore Storage;

/// <summary>
/// Create a locale mapping from a localisation store.
/// </summary>
/// <param name="store">The store to be used.</param>
public LocaleMapping(ILocalisationStore store)
{
Name = store.EffectiveCulture.Name;
Storage = store;
}

/// <summary>
/// Create a locale mapping with a custom lookup name.
/// </summary>
/// <param name="name"></param>
/// <param name="store">The store to be used.</param>
public LocaleMapping(string name, ILocalisationStore store)
{
Name = name;
Storage = store;
}
}
}
29 changes: 17 additions & 12 deletions osu.Framework/Localisation/LocalisationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ public LocalisationManager(FrameworkConfigManager config)
configPreferUnicode.BindValueChanged(_ => UpdateLocalisationParameters(), true);
}

/// <summary>
/// Add multiple locale mappings. Should be used to add all available languages at initialisation.
/// </summary>
/// <param name="mappings">All available locale mappings.</param>
public void AddLocaleMappings(IEnumerable<LocaleMapping> mappings)
{
locales.AddRange(mappings);
configLocale.TriggerChange();
}

/// <summary>
/// Add a single language to this manager.
/// </summary>
/// <remarks>
/// Use <see cref="AddLocaleMappings"/> as a more efficient way of bootstrapping all available locales.</remarks>
/// <param name="language">The culture name to be added. Generally should match <see cref="CultureInfo.Name"/>.</param>
/// <param name="storage">A storage providing localisations for the specified language.</param>
public void AddLanguage(string language, ILocalisationStore storage)
{
locales.Add(new LocaleMapping(language, storage));
Expand Down Expand Up @@ -102,17 +119,5 @@ private void updateLocale(ValueChangedEvent<string> locale)
/// </remarks>
/// <returns>The resultant <see cref="LocalisationParameters"/>.</returns>
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;
}
}
}
}

0 comments on commit e3eae43

Please sign in to comment.