Skip to content

Commit

Permalink
Fix missing resources errors (#1426)
Browse files Browse the repository at this point in the history
* Add missing default resources
* Improve error messages when resolving resources
  • Loading branch information
hazzik authored Feb 22, 2024
1 parent 177e275 commit aeb7e39
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Humanizer/Localisation/Formatters/DefaultFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ string GetResourceForTimeSpan(TimeUnit unit, int count, bool toWords = false)
/// <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 resolvedKey = GetResourceKey(resourceKey);
var resourceString = Resources.GetResource(resolvedKey, _culture);

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}' (resolved to '{resolvedKey}') was not found", nameof(resourceKey));
}

return resourceString;
Expand All @@ -94,11 +95,12 @@ protected virtual string Format(string resourceKey)
/// <exception cref="ArgumentException">If the resource not exists on the specified culture.</exception>
protected virtual string Format(string resourceKey, int number, bool toWords = false)
{
var resourceString = Resources.GetResource(GetResourceKey(resourceKey, number), _culture);
var resolvedKey = GetResourceKey(resourceKey, number);
var resourceString = Resources.GetResource(resolvedKey, _culture);

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}' for number `{number}' (resolved to '{resolvedKey}') was not found", nameof(resourceKey));
}

if (toWords)
Expand Down
69 changes: 69 additions & 0 deletions src/Humanizer/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -735,4 +735,73 @@
<data name="TimeUnit_Year" xml:space="preserve">
<value>y</value>
</data>
<data name="DateHumanize_MultipleDaysAgo_DualTrialQuadral" xml:space="preserve">
<value>{0} days ago</value>
</data>
<data name="DateHumanize_MultipleDaysAgo_Paucal" xml:space="preserve">
<value>{0} days ago</value>
</data>
<data name="DateHumanize_MultipleDaysFromNow_Paucal" xml:space="preserve">
<value>{0} days from now</value>
</data>
<data name="DateHumanize_MultipleHoursAgo_Paucal" xml:space="preserve">
<value>{0} hours ago</value>
</data>
<data name="DateHumanize_MultipleHoursFromNow_Paucal" xml:space="preserve">
<value>{0} hours from now</value>
</data>
<data name="DateHumanize_MultipleMinutesAgo_Paucal" xml:space="preserve">
<value>{0} minutes ago</value>
</data>
<data name="DateHumanize_MultipleMinutesFromNow_Paucal" xml:space="preserve">
<value>{0} minutes from now</value>
</data>
<data name="DateHumanize_MultipleMonthsAgo_Paucal" xml:space="preserve">
<value>{0} months ago</value>
</data>
<data name="DateHumanize_MultipleMonthsFromNow_Paucal" xml:space="preserve">
<value>{0} months from now</value>
</data>
<data name="DateHumanize_MultipleSecondsAgo_Paucal" xml:space="preserve">
<value>{0} seconds ago</value>
</data>
<data name="DateHumanize_MultipleSecondsFromNow_Paucal" xml:space="preserve">
<value>{0} seconds from now</value>
</data>
<data name="DateHumanize_MultipleYearsAgo_Paucal" xml:space="preserve">
<value>{0} years from ago</value>
</data>
<data name="DateHumanize_MultipleYearsFromNow_Paucal" xml:space="preserve">
<value>{0} years from now</value>
</data>
<data name="DateHumanize_TwoDaysAgo" xml:space="preserve">
<value>two days ago</value>
</data>
<data name="DateHumanize_TwoDaysFromNow" xml:space="preserve">
<value>two days from now</value>
</data>
<data name="TimeSpanHumanize_MultipleDays_Paucal" xml:space="preserve">
<value>{0} days</value>
</data>
<data name="TimeSpanHumanize_MultipleHours_Paucal" xml:space="preserve">
<value>{0} hours</value>
</data>
<data name="TimeSpanHumanize_MultipleMilliseconds_Paucal" xml:space="preserve">
<value>{0} milliseconds</value>
</data>
<data name="TimeSpanHumanize_MultipleMinutes_Paucal" xml:space="preserve">
<value>{0} minutes</value>
</data>
<data name="TimeSpanHumanize_MultipleMonths_Paucal" xml:space="preserve">
<value>{0} months</value>
</data>
<data name="TimeSpanHumanize_MultipleSeconds_Paucal" xml:space="preserve">
<value>{0} months</value>
</data>
<data name="TimeSpanHumanize_MultipleWeeks_Paucal" xml:space="preserve">
<value>{0} weeks</value>
</data>
<data name="TimeSpanHumanize_MultipleYears_Paucal" xml:space="preserve">
<value>{0} years</value>
</data>
</root>

0 comments on commit aeb7e39

Please sign in to comment.