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

Introduce a pluralisable string type #4918

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
84 changes: 84 additions & 0 deletions osu.Framework.Tests/Localisation/LocalisationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,58 @@ public void TestTranslatableComplexStringUsesFallbackFormatWithTranslatedParts()
Assert.AreEqual("12.34 / number 98.76% EN / number romanised EN", text.Value);
}

[Test]
public void TestPluralisableString()
{
const string key = FakeStorage.LOCALISABLE_PLURALISABLE_STRING_EN;

manager.AddLanguage("fr", new FakeStorage("fr"));

var textSingularVariant = manager.GetLocalisedBindableString(new PluralisableString(key, key, 1, 1));
Assert.AreEqual("1 circle", textSingularVariant.Value);

config.SetValue(FrameworkSetting.Locale, "fr");
Assert.AreEqual("1 cercle", textSingularVariant.Value);

var textPluralVariant = manager.GetLocalisedBindableString(new PluralisableString(key, key, 2, 2));
Assert.AreEqual("2 cercles", textPluralVariant.Value);

config.SetValue(FrameworkSetting.Locale, "en");
Assert.AreEqual("2 circles", textPluralVariant.Value);
}

[Test]
public void TestPluralisableStringNonEnglishPluralRules()
{
const string key = FakeStorage.LOCALISABLE_PLURALISABLE_STRING_EN;

manager.AddLanguage("pl", new FakeStorage("pl"));
config.SetValue(FrameworkSetting.Locale, "pl");

var textFirstVariant = manager.GetLocalisedBindableString(new PluralisableString(key, key, 1, 1));
Assert.AreEqual("1 krąg", textFirstVariant.Value);

var textSecondVariant = manager.GetLocalisedBindableString(new PluralisableString(key, key, 3, 3));
Assert.AreEqual("3 kręgi", textSecondVariant.Value);

var textThirdVariant = manager.GetLocalisedBindableString(new PluralisableString(key, key, 13, 13));
Assert.AreEqual("13 kręgów", textThirdVariant.Value);
}

[Test]
public void TestPluralisableStringPluralFormFallback()
{
const string key = FakeStorage.LOCALISABLE_PLURALISABLE_INCOMPLETE_EN;

manager.AddLanguage("fr", new FakeStorage("fr"));

var textString = manager.GetLocalisedBindableString(new PluralisableString(key, key, 2, 2));
Assert.AreEqual("2 circle", textString.Value);

config.SetValue(FrameworkSetting.Locale, "fr");
Assert.AreEqual("2 cercle", textString.Value);
}

private class FakeFrameworkConfigManager : FrameworkConfigManager
{
protected override string Filename => null;
Expand Down Expand Up @@ -406,6 +458,11 @@ private class FakeStorage : ILocalisationStore
public const string LOCALISABLE_NUMBER_FORMAT_STRING_FR = "number {0} FR";
public const string LOCALISABLE_COMPLEX_FORMAT_STRING_EN = "number {0} with {1} and {2} EN";
public const string LOCALISABLE_COMPLEX_FORMAT_STRING_FR = "number {0} with {1} and {2} FR";
public const string LOCALISABLE_PLURALISABLE_STRING_EN = "{0} circle|{0} circles";
public const string LOCALISABLE_PLURALISABLE_STRING_FR = "{0} cercle|{0} cercles";
public const string LOCALISABLE_PLURALISABLE_STRING_PL = "{0} krąg|{0} kręgi|{0} kręgów";
public const string LOCALISABLE_PLURALISABLE_INCOMPLETE_EN = "{0} circle";
public const string LOCALISABLE_PLURALISABLE_INCOMPLETE_FR = "{0} cercle";

public CultureInfo EffectiveCulture { get; }

Expand Down Expand Up @@ -469,6 +526,33 @@ public string Get(string name)
return LOCALISABLE_COMPLEX_FORMAT_STRING_FR;
}

case LOCALISABLE_PLURALISABLE_STRING_EN:
{
switch (locale)
{
default:
return LOCALISABLE_PLURALISABLE_STRING_EN;

case "fr":
return LOCALISABLE_PLURALISABLE_STRING_FR;

case "pl":
return LOCALISABLE_PLURALISABLE_STRING_PL;
}
}

case LOCALISABLE_PLURALISABLE_INCOMPLETE_EN:
{
switch (locale)
{
default:
return LOCALISABLE_PLURALISABLE_INCOMPLETE_EN;

case "fr":
return LOCALISABLE_PLURALISABLE_INCOMPLETE_FR;
}
}

default:
return null;
}
Expand Down
Loading