Today, I show you some beginner example and some issues I miss when I try to use it. You can find more information in MSDN: http://msdn.microsoft.com/en-us/library/system.data.entity.design.pluralizationservices.pluralizationservice.aspx
For the first, I show you steps to make an example.
- Create a new C# Console Application project (* Note: you need make .NET Framework 4 or later).
- After create done, in solution explorer, right click on project name and choose Properties. In Application tab, make sure Target Framework is .NET Framework 4 (is not .NET Framework Client Profile). It is an issue make me waste many time on Google because I can not add reference for project.
- Next, we add reference, right click on project name, select Add Reference..., in .NET tab, add System.Data.Entity.Design
- Ok, now write this code:
1: using System;
2: using System.Data.Entity.Design.PluralizationServices;
3: using System.Globalization;
4:
5: namespace ConsoleApplication1
6: {
7: internal class Program
8: {
9: public static string SwitchPluralAndSingular(string word)
10: {
11: PluralizationService ps = PluralizationService.CreateService(CultureInfo.GetCultureInfo("en-us"));
12:
13: if (ps.IsPlural(word))
14: {
15: return ps.Singularize(word);
16: }
17: if (ps.IsSingular(word))
18: {
19: return ps.Pluralize(word);
20: }
21:
22: return word;
23: }
24:
25: private static void Main(string[] args)
26: {
27: Console.WriteLine(Program.SwitchPluralAndSingular("Word"));
28: Console.WriteLine(Program.SwitchPluralAndSingular("Words"));
29: Console.WriteLine(Program.SwitchPluralAndSingular("Child"));
30: Console.WriteLine(Program.SwitchPluralAndSingular("Children"));
31: Console.WriteLine(Program.SwitchPluralAndSingular("abc"));
32: Console.WriteLine(Program.SwitchPluralAndSingular("123"));
33: Console.ReadKey(true);
34: }
35: }
36: }
Enjoy it!
* Update:
You can custom translated words by using:
1: private static PluralizationService IntializePluralization()
2: {
3: var ps = PluralizationService.CreateService(CultureInfo.GetCultureInfo("en-us"));
4: ((ICustomPluralizationMapping)ps).AddWord("Die", "Dies");
5: ((ICustomPluralizationMapping)ps).AddWord("Range", "Ranges");
6: return ps;
7: }
No comments:
Post a Comment