Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow initial locale mappings to be added in a single call #5276

Merged
merged 1 commit into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
}