Skip to content
Open
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
26 changes: 26 additions & 0 deletions Source/Porticle.CLDR.Units.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using Porticle.CLDR.Units.Serialization;

namespace Porticle.CLDR.Units.Tests;
Expand Down Expand Up @@ -63,4 +64,29 @@ public void TestAll()
}
}
}

[TestMethod]
public void GetFormatString_UsesLengthSpecificFallback()
{
var cldrUnits = new CldrUnits(Unit.LengthMeter);

var shortFormat = cldrUnits.GetFormatString("xx", 1, PluralFormLength.Short, GrammaticalCase.Oblique);
var narrowFormat = cldrUnits.GetFormatString("xx", 1, PluralFormLength.Narrow, GrammaticalCase.Oblique);

Assert.AreEqual("{0} m", shortFormat);
Assert.AreEqual("{0}m", narrowFormat);
}

[TestMethod]
public void FormatUnit_UsesLengthSpecificFallback()
{
var cldrUnits = new CldrUnits(Unit.LengthMeter);
var culture = CultureInfo.InvariantCulture;

var shortFormat = cldrUnits.FormatUnit(culture, 1, PluralFormLength.Short, GrammaticalCase.Oblique);
var narrowFormat = cldrUnits.FormatUnit(culture, 1, PluralFormLength.Narrow, GrammaticalCase.Oblique);

Assert.AreEqual("1 m", shortFormat);
Assert.AreEqual("1m", narrowFormat);
}
}
8 changes: 4 additions & 4 deletions Source/Porticle.CLDR.Units/CldrUnits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public CldrUnits(Unit unit)
/// <returns>The format String like "{0} weeks"</returns>
public string GetFormatString(string language, int count, PluralFormLength length, GrammaticalCase grammaticalCase)
{
return _patterns.GetFormat(language, count, length, grammaticalCase) ?? GetFallbackPattern(PluralFormLength.Long);
return _patterns.GetFormat(language, count, length, grammaticalCase) ?? GetFallbackPattern(length);
}

/// <summary>
Expand All @@ -61,7 +61,7 @@ public string GetFormatString(string language, int count, PluralFormLength lengt
/// <returns>A format string that matches the specified culture, count, plural form length, and grammatical case. If no specific format string is found, a fallback pattern is returned.</returns>
public string GetFormatString(CultureInfo culture, int count, PluralFormLength length, GrammaticalCase grammaticalCase)
{
return _patterns.GetFormat(culture.Name, count, length, grammaticalCase) ?? GetFallbackPattern(PluralFormLength.Long);
return _patterns.GetFormat(culture.Name, count, length, grammaticalCase) ?? GetFallbackPattern(length);
}

/// <summary>
Expand All @@ -81,10 +81,10 @@ public string FormatUnit(CultureInfo culture, int count, PluralFormLength length
{
if (numberFormat == null)
{
return string.Format(culture, _patterns.GetFormat(culture.Name, count, length, grammaticalCase) ?? GetFallbackPattern(PluralFormLength.Long), count);
return string.Format(culture, _patterns.GetFormat(culture.Name, count, length, grammaticalCase) ?? GetFallbackPattern(length), count);
}

return string.Format(culture, _patterns.GetFormat(culture.Name, count, length, grammaticalCase) ?? GetFallbackPattern(PluralFormLength.Long),
return string.Format(culture, _patterns.GetFormat(culture.Name, count, length, grammaticalCase) ?? GetFallbackPattern(length),
count.ToString(numberFormat, culture));
}

Expand Down