Skip to content

Commit

Permalink
fix for cultural variations
Browse files Browse the repository at this point in the history
  • Loading branch information
neilboyd committed Mar 17, 2022
1 parent d43f50e commit c2e0979
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1945,9 +1945,8 @@ namespace Humanizer.Localisation.Formatters
public virtual string DateHumanize_Never() { }
public virtual string DateHumanize_Now() { }
protected virtual string Format(string resourceKey) { }
protected virtual string Format(string resourceKey, int number, bool toWords = False) { }
protected virtual string Format(string resourceKey, int number, Humanizer.TimeSpanStyle timeSpanStyle = 0) { }
protected virtual string GetResourceKey(string resourceKey, int number) { }
protected virtual string GetResourceKey(string resourceKey) { }
public virtual string TimeSpanHumanize(Humanizer.Localisation.TimeUnit timeUnit, int unit, Humanizer.TimeSpanStyle timeSpanStyle = 0) { }
public virtual string TimeSpanHumanize_Zero() { }
public virtual string TimeUnitHumanize(Humanizer.Localisation.TimeUnit timeUnit) { }
Expand Down
26 changes: 10 additions & 16 deletions src/Humanizer/Localisation/Formatters/DefaultFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private string GetResourceForTimeSpan(TimeUnit unit, int count, TimeSpanStyle ti
{
var resourceKey = ResourceKeys.TimeSpanHumanize.GetResourceKey(unit, count, timeSpanStyle);

return count == 1 ? Format(resourceKey) : Format(resourceKey, count, timeSpanStyle == TimeSpanStyle.Words);
return count == 1 ? Format(resourceKey) : Format(resourceKey, count, timeSpanStyle);
}

/// <summary>
Expand All @@ -111,7 +111,7 @@ private string GetResourceForTimeSpan(TimeUnit unit, int count, TimeSpanStyle ti
/// <exception cref="ArgumentException">If the resource not exists on the specified culture.</exception>
protected virtual string Format(string resourceKey)
{
var resourceString = Resources.GetResource(GetResourceKey(resourceKey), _culture);
var resourceString = Resources.GetResource(GetResourceKey(resourceKey, 0), _culture);

if (string.IsNullOrEmpty(resourceString))
{
Expand All @@ -126,19 +126,23 @@ protected virtual string Format(string resourceKey)
/// </summary>
/// <param name="resourceKey">The resource key.</param>
/// <param name="number">The number.</param>
/// <param name="toWords"></param>
/// <param name="timeSpanStyle">Time span style</param>
/// <returns></returns>
/// <exception cref="ArgumentException">If the resource not exists on the specified culture.</exception>
protected virtual string Format(string resourceKey, int number, bool toWords = false)
protected virtual string Format(string resourceKey, int number, TimeSpanStyle timeSpanStyle = TimeSpanStyle.Full)
{
var resourceString = Resources.GetResource(GetResourceKey(resourceKey, number), _culture);
if (timeSpanStyle == TimeSpanStyle.Full || timeSpanStyle == TimeSpanStyle.Words)
{
resourceKey = GetResourceKey(resourceKey, number);
}
var resourceString = Resources.GetResource(resourceKey, _culture);

if (string.IsNullOrEmpty(resourceString))
{
throw new ArgumentException($"The resource object with key '{resourceKey}' was not found", nameof(resourceKey));
}

return toWords
return timeSpanStyle == TimeSpanStyle.Words
? resourceString.FormatWith(number.ToWords(_culture))
: resourceString.FormatWith(number);
}
Expand All @@ -153,15 +157,5 @@ protected virtual string GetResourceKey(string resourceKey, int number)
{
return resourceKey;
}

/// <summary>
///
/// </summary>
/// <param name="resourceKey"></param>
/// <returns></returns>
protected virtual string GetResourceKey(string resourceKey)
{
return resourceKey;
}
}
}
6 changes: 3 additions & 3 deletions src/Humanizer/Localisation/Formatters/IcelandicFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public override string DataUnitHumanize(DataUnit dataUnit, double count, bool to
{
return base.DataUnitHumanize(dataUnit, count, toSymbol)?.TrimEnd('s');
}
protected override string Format(string resourceKey, int number, bool toWords = false)
protected override string Format(string resourceKey, int number, TimeSpanStyle timeSpanStyle = TimeSpanStyle.Full)
{
var resourceString = Resources.GetResource(GetResourceKey(resourceKey, number), _localCulture);

if (string.IsNullOrEmpty(resourceString))
{
throw new ArgumentException($@"The resource object with key '{resourceKey}' was not found", nameof(resourceKey));
throw new ArgumentException($"The resource object with key '{resourceKey}' was not found", nameof(resourceKey));
}
var words = resourceString.Split(' ');

Expand All @@ -35,7 +35,7 @@ var x when x.StartsWith("ár") => GrammaticalGender.Neuter,
_ => GrammaticalGender.Feminine
};

return toWords ?
return timeSpanStyle == TimeSpanStyle.Words ?
resourceString.FormatWith(number.ToWords(unitGender, _localCulture)) :
resourceString.FormatWith(number);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Humanizer/Localisation/Formatters/RomanianFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public RomanianFormatter()
_romanianCulture = new CultureInfo(RomanianCultureCode);
}

protected override string Format(string resourceKey, int number, bool toWords = false)
protected override string Format(string resourceKey, int number, TimeSpanStyle timeSpanStyle = TimeSpanStyle.Full)
{
var format = Resources.GetResource(GetResourceKey(resourceKey, number), _romanianCulture);
var preposition = ShouldUsePreposition(number)
Expand Down
2 changes: 1 addition & 1 deletion src/Humanizer/Localisation/Formatters/RussianFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected override string GetResourceKey(string resourceKey, int number)
return resourceKey + suffix;
}

private string GetSuffix(RussianGrammaticalNumber grammaticalNumber)
private static string GetSuffix(RussianGrammaticalNumber grammaticalNumber)
{
if (grammaticalNumber == RussianGrammaticalNumber.Singular)
{
Expand Down

0 comments on commit c2e0979

Please sign in to comment.