To use the functions in the StringUtil class, you should set a reference to the CodeSmith.Engine assembly and import its namespace:
<%@ Assembly Name="CodeSmith.Engine" %> <%@ Import Namespace="CodeSmith.Engine" %>
The StringUtil class includes these static methods:
- IsPlural - returns True if a string is plural.
- IsSingular - returns True if a string is singular
- ToCamelCase - converts a set of words to a single camel case identifier
- ToPlural - converts a word to its plural form
- ToSingular - converts a word to its singular form
- ToSpacedWords - converts a camel case identifier to separate words
StringUtil ToPlural and ToSinglular supports overriding the converted word with a special csmap file. The default override file is called Plural-Overrides.csmap. It will be used by default. However, you can use a different Map file if needed. The following is a sample of how to use the overrides.
<%@ CodeTemplate Language="C#" TargetLanguage="Text" Debug="False" Description="Plural Overrides." %> <%@ Map Name="PluralOverrides" Src="Plural.csmap" Reverse="False" Description="Convert system data types to c# alias" %> <%@ Map Name="SingleOverrides" Src="Plural.csmap" Reverse="True" Description="Convert system data types to c# alias" %>
Use Built in csmap ...
Child: <%= StringUtil.ToPlural("Child") %>
Children: <%= StringUtil.ToSingular("Children") %>
Use MapCollection ...
Knife: <%= StringUtil.ToPlural("nife", PluralOverrides) %>
Knives: <%= StringUtil.ToSingular("nives", SingleOverrides) %>
Use csmap file name...
Knife: <%= StringUtil.ToPlural("nife", "Plural.csmap") %>
Knives: <%= StringUtil.ToSingular("nives", "Plural.csmap") %>