Skip to content

Commit

Permalink
move resource throw deeper (#1436)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Feb 22, 2024
1 parent ea1104c commit 3a71cf4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 25 deletions.
14 changes: 1 addition & 13 deletions src/Humanizer/Localisation/Formatters/DefaultFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,7 @@ string GetResourceForTimeSpan(TimeUnit unit, int count, bool toWords = false)
protected virtual string Format(string resourceKey)
{
var resolvedKey = GetResourceKey(resourceKey);
var resourceString = Resources.GetResource(resolvedKey, _culture);

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

return resourceString;
return Resources.GetResource(resolvedKey, _culture);
}

/// <summary>
Expand All @@ -106,11 +99,6 @@ protected virtual string Format(string resourceKey, int number, bool toWords = f
var resolvedKey = GetResourceKey(resourceKey, number);
var resourceString = Resources.GetResource(resolvedKey, _culture);

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

if (toWords)
{
return string.Format(resourceString, number.ToWords(_culture));
Expand Down
5 changes: 0 additions & 5 deletions src/Humanizer/Localisation/Formatters/IcelandicFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ protected override string Format(string resourceKey, int number, bool toWords =
{
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));
}

if (toWords)
{
var unitGender = GetGrammaticalGender(resourceString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ protected override string Format(string resourceKey, int number, bool toWords =
{
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));
}

var unitGender = GetUnitGender(resourceString);

var numberAsWord = number.ToWords(unitGender, localCulture);
Expand Down
13 changes: 11 additions & 2 deletions src/Humanizer/Localisation/Resources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ public static class Resources
/// <param name="resourceKey">The name of the resource to retrieve.</param>
/// <param name="culture">The culture of the resource to retrieve. If not specified, current thread's UI culture is used.</param>
/// <returns>The value of the resource localized for the specified culture.</returns>
public static string GetResource(string resourceKey, CultureInfo culture = null) =>
ResourceManager.GetString(resourceKey, culture);
public static string GetResource(string resourceKey, CultureInfo culture = null)
{
var resource = ResourceManager.GetString(resourceKey, culture);

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

return resource;
}

/// <summary>
/// Tries to get the value of the specified string resource, without fallback
Expand Down

0 comments on commit 3a71cf4

Please sign in to comment.