Skip to content

Commit

Permalink
better perf LuxembourgishFormatter (#1393)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Feb 17, 2024
1 parent fb343a1 commit 2fd7410
Showing 1 changed file with 43 additions and 28 deletions.
71 changes: 43 additions & 28 deletions src/Humanizer/Localisation/Formatters/LuxembourgishFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
class LuxembourgishFormatter() :
DefaultFormatter(LocaleCode)
{
private const string LocaleCode = "lb";
private readonly CultureInfo _localCulture = new(LocaleCode);
private const string DualPostfix = "_Dual";
const string LocaleCode = "lb";
readonly CultureInfo localCulture = new(LocaleCode);
const string DualPostfix = "_Dual";
// https://lb.wikipedia.org/wiki/Eifeler_Reegel
private const char EifelerRuleSuffix = 'n';
private const string EifelerRuleCharacters = "unitedzohay";
const char EifelerRuleSuffix = 'n';
const string EifelerRuleCharacters = "unitedzohay";

public override string DataUnitHumanize(DataUnit dataUnit, double count, bool toSymbol = true) =>
base.DataUnitHumanize(dataUnit, count, toSymbol)?.TrimEnd('s');
Expand All @@ -23,11 +23,11 @@ public static string CheckForAndApplyEifelerRule(string word, string nextWord)

public static bool DoesEifelerRuleApply(string nextWord)
=> !string.IsNullOrWhiteSpace(nextWord)
&& !EifelerRuleCharacters.Contains(nextWord.Substring(0, 1));
&& !EifelerRuleCharacters.Contains(nextWord[0]);

protected override string Format(string resourceKey, int number, bool toWords = false)
{
var resourceString = Resources.GetResource(GetResourceKey(resourceKey, number), _localCulture);
var resourceString = Resources.GetResource(GetResourceKey(resourceKey, number), localCulture);

if (string.IsNullOrEmpty(resourceString))
{
Expand All @@ -36,33 +36,48 @@ protected override string Format(string resourceKey, int number, bool toWords =

var unitGender = GetUnitGender(resourceString);

var numberAsWord = number.ToWords(unitGender, _localCulture);
var doesEifelerRuleApply = DoesEifelerRuleApply(numberAsWord);
var numberAsWord = number.ToWords(unitGender, localCulture);

return toWords
? resourceString.FormatWith(numberAsWord, doesEifelerRuleApply ? string.Empty : EifelerRuleSuffix)
: resourceString.FormatWith(number, doesEifelerRuleApply ? string.Empty : EifelerRuleSuffix);
if (DoesEifelerRuleApply(numberAsWord))
{
if (toWords)
{
return string.Format(resourceString, numberAsWord, string.Empty);
}

return string.Format(resourceString, number, string.Empty);
}

if (toWords)
{
return string.Format(resourceString, numberAsWord, EifelerRuleSuffix);
}

return string.Format(resourceString, number, EifelerRuleSuffix);
}

protected override string GetResourceKey(string resourceKey, int number) =>
number switch
protected override string GetResourceKey(string resourceKey, int number)
{
if (number == 2 &&
resourceKey is "DateHumanize_MultipleDaysAgo" or "DateHumanize_MultipleDaysFromNow")
{
2 when resourceKey is "DateHumanize_MultipleDaysAgo" or "DateHumanize_MultipleDaysFromNow" => resourceKey + DualPostfix,
_ => resourceKey
};
return resourceKey + DualPostfix;
}

return resourceKey;
}

private static GrammaticalGender GetUnitGender(string resourceString)
static GrammaticalGender GetUnitGender(string resourceString)
{
var words = resourceString.Split(' ');
return words.Last() switch
if (resourceString.EndsWith(" Millisekonnen") ||
resourceString.EndsWith(" Sekonnen") ||
resourceString.EndsWith(" Minutten") ||
resourceString.EndsWith(" Stonnen") ||
resourceString.EndsWith(" Wochen"))
{
var x when
x.StartsWith("Millisekonnen")
|| x.StartsWith("Sekonnen")
|| x.StartsWith("Minutten")
|| x.StartsWith("Stonnen")
|| x.StartsWith("Wochen") => GrammaticalGender.Feminine,
_ => GrammaticalGender.Masculine
};
return GrammaticalGender.Feminine;
}

return GrammaticalGender.Masculine;
}
}

0 comments on commit 2fd7410

Please sign in to comment.