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

better perf LuxembourgishFormatter #1393

Merged
merged 1 commit into from
Feb 17, 2024
Merged
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
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;
}
}