This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from AlexTeixeira/development
Development
- Loading branch information
Showing
53 changed files
with
1,753 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
Askmethat.Aspnet.JsonLocalizer/Localizer/DefaultPluralizationRuleSet.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Askmethat.Aspnet.JsonLocalizer.Localizer | ||
{ | ||
public class DefaultPluralizationRuleSet : IPluralizationRuleSet | ||
{ | ||
private Dictionary<string, Predicate<double>> rules; | ||
|
||
public DefaultPluralizationRuleSet() | ||
{ | ||
rules = new Dictionary<string, Predicate<double>>(); | ||
SetupRules(); | ||
} | ||
|
||
/// <summary> | ||
/// Override to define any rules for the custom rule set. | ||
/// </summary> | ||
protected virtual void SetupRules() | ||
{ | ||
rules.Add(PluralizationConstants.Zero, ValueMatchesZero); | ||
rules.Add(PluralizationConstants.One, ValueMatchesOne); | ||
rules.Add(PluralizationConstants.Other, ValueMatchesOther); | ||
} | ||
|
||
protected virtual bool ValueMatchesZero(double value) | ||
{ | ||
return value == 0; | ||
} | ||
|
||
protected virtual bool ValueMatchesOne(double value) | ||
{ | ||
return value == 1.0; | ||
} | ||
|
||
protected virtual bool ValueMatchesOther(double value) | ||
{ | ||
return value != 1.0; | ||
} | ||
|
||
public string GetMatchingPluralizationRule(double count) | ||
{ | ||
foreach (var key in rules.Keys) | ||
{ | ||
if (rules[key](count)) | ||
{ | ||
return key; | ||
} | ||
} | ||
|
||
//If no match is found, always default to Other. | ||
return PluralizationConstants.Other; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
Askmethat.Aspnet.JsonLocalizer/Localizer/IPluralizationRuleSet.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Microsoft.Extensions.Localization; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Askmethat.Aspnet.JsonLocalizer.Localizer | ||
{ | ||
public interface IPluralizationRuleSet | ||
{ | ||
string GetMatchingPluralizationRule(double count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
Askmethat.Aspnet.JsonLocalizer/Localizer/PluralizationConstants.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Askmethat.Aspnet.JsonLocalizer.Localizer | ||
{ | ||
/// <summary> | ||
/// Default pluralization constants as defined in http://unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules | ||
/// Other custom constants may be defined in a custom implementation. | ||
/// </summary> | ||
public class PluralizationConstants | ||
{ | ||
public const string Zero = "Zero"; | ||
public const string One = "One"; | ||
public const string Two = "Two"; | ||
public const string Few = "Few"; | ||
public const string Many = "Many"; | ||
public const string Other = "Other"; | ||
} | ||
} |
Oops, something went wrong.