开发者

Best way to represent Gender in a class library used in multilingual applications

I'm creating class library with some commonly used classes like persons, addresses etc. This library will be used in an multilingual application, and I am looking for the most convenient way to represent a persons gender.

Ideally I would like to be able to code like this:

Person person = new Person { Gender = Genders.Male, 
                             FirstName = "Nice", 
                             LastName = "Dude" }

if (person.Gender == Genders.Male)
  Console.WriteLine("person is Male");

Console.W开发者_如何学PythonriteLine(person.Gender); //Should output: Male

Console.WriteLine(person.Gender.ToString("da-DK")); 
//Should output the name of the gender in the language provided

List<Gender> genders = Genders.GetAll();
foreach(Gender gender in genders)
{
  Console.WriteLine(gender.ToString());
  Console.WriteLine(gender.ToString("da-DK"));
}

What would you do? An enumeration and a specialized Gender class? But what about the localization then?


Edit: I'd like to point out that this question is at least as much about how to code some classes that enables writing code as above, I don't have a Gender class or gender enumeration, I'm trying to figure out how to write code that would enable the above code. What would the Genders class look like? Would you use an enum for the Genders?

Regards

Jesper Hauge


You can solve localization issues by using resource strings. You can create a resource string for each Gender-value, and translate it for each culture that you support.


I would not use an enumeration and probably would choose to create the Gender class with an internal constructor and also a Genders static class to contain the available options as properties. This would be basically the same approach that is used in .NET for example for the System.Drawing.Color and it's associated Colors class.

It's easier to deal with globalization if you have a class instead of a enum in my opinion.


Your business and data classes should not care about the fact that you're doing localization, as localization is (should be) an entirely display-side operation. Use exactly what you have, as it's readable and maintainable.

To localize the values, .NET already has a built-in resource manager that automatically selects the appropriate resource value based upon the CurrentUICulture value. For this specific scenario, you could use the ToString() value from your enumeration as the key in the resource file. This would allow you to create different files for different cultures (as is the convention and what the resource manager expects), each with a different value for "Male" and "Female".

http://msdn.microsoft.com/en-us/magazine/cc163609.aspx

http://msdn.microsoft.com/en-us/library/aa309421%28VS.71%29.aspx


Another option is to create an extension method on your enumerated type. For example:

public static class GenderExtensions
{
    public static string ToLocalizedText(this Gender gender)
    {
         // Lookup Localized content based on state of 'gender'
         return "This person is male";
    }
}

And then you can simply write

gender.ToLocalizedText()

instead of

gender.ToString()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜