diff --git a/readme.md b/readme.md index 1839ab43a..3d4b7972d 100644 --- a/readme.md +++ b/readme.md @@ -18,6 +18,7 @@ Humanizer meets all your .NET needs for manipulating and displaying strings, enu - [Inflector methods](#inflector-methods) - [Pluralize](#pluralize) - [Singularize](#singularize) + - [Adding Words](#adding-words) - [ToQuantity](#toquantity) - [Ordinalize](#ordinalize) - [Titleize](#titleize) @@ -459,6 +460,25 @@ Normally you would call `Singularize` on a plural word but if you're unsure abou The overload of `Singularize` with `plurality` argument is obsolete and will be removed in next major release. +##Adding Words +Sometimes, you may need to add a rule from the singularization/pluralization vocabulary (the examples below are already in the `DefaultVocabluary` used by `Inflector`): + +```C# +// Adds a word to the vocabulary which cannot easily be pluralized/singularized by RegEx: +Vocabularies.Default.AddIrregular("person", "people"); + +// Adds an uncountable word to the vocabulary. Will be ignored when plurality is changed: +Vocabularies.Default.AddUncountable("fish"); + +// Adds a rule to the vocabulary that does not follow trivial rules for pluralization, e.g. "bus" -> "buses" +Vocabularies.Default.AddPlural("bus", "buses"); + +// Adds a rule to the vocabulary that does not follow trivial rules for singularization +// (will match both "vertices" -> "vertex" and "indices" -> "index"): +Vocabularies.Default.AddSingular("(vert|ind)ices$", "$1ex"); + +``` + ####ToQuantity Many times you want to call `Singularize` and `Pluralize` to prefix a word with a number; e.g. "2 requests", "3 men". `ToQuantity` prefixes the provided word with the number and accordingly pluralizes or singularizes the word: diff --git a/release_notes.md b/release_notes.md index 18274410a..9b99aa563 100644 --- a/release_notes.md +++ b/release_notes.md @@ -1,6 +1,7 @@ ###In Development [Commits](https://github.com/MehdiK/Humanizer/compare/v1.35.0...master) +- [#408](https://github.com/MehdiK/Humanizer/pull/408): Added support for adding/removing rules from singular/pluralization by adding `Vocabulary` class and `Vocabularies.Default`. ###v1.35.0 - 2015-03-29 - [#399](https://github.com/MehdiK/Humanizer/pull/399): Added support for humanizing DateTimeOffset diff --git a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt index 46ef0744c..1536c1e6b 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -243,6 +243,23 @@ public class In public System.DateTime TheYear(int year) { } } +public class Vocabularies +{ + public Vocabularies() { } + public Humanizer.Inflections.Vocabulary Default { get; } +} + +public class Vocabulary +{ + public Vocabulary() { } + public void AddIrregular(string singular, string plural) { } + public void AddPlural(string rule, string replacement) { } + public void AddSingular(string rule, string replacement) { } + public void AddUncountable(string word) { } + public string Pluralize(string word, bool inputIsKnownToBeSingular) { } + public string Singularize(string word, bool inputIsKnownToBePlural) { } +} + public class InflectorExtensions { public string Camelize(string input) { } diff --git a/src/Humanizer.Tests/Humanizer.Tests.csproj b/src/Humanizer.Tests/Humanizer.Tests.csproj index 2e9aa81c0..3820b4293 100644 --- a/src/Humanizer.Tests/Humanizer.Tests.csproj +++ b/src/Humanizer.Tests/Humanizer.Tests.csproj @@ -206,7 +206,9 @@ - + + +