views:

127

answers:

4

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.WriteLine(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

+3  A: 

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.

Frederik Gheysels
+1  A: 

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

Adam Robinson
And `Hermaphrodite`, and maybe other values.
ANeves
+1 this is the correct answer, just because it's being run in another language doesn't mean the *source code* needs to be translated...
BlueRaja - Danny Pflughoeft
I can see what you mean about separating the the BOL and the localization, will go that route. My main problem was how to construct the classes to be able to code like shown in my question, so I gave the answer to João Angelo, since he showed me an example of how to do that.
Hauge
A: 

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()
Tejs
A: 

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.

João Angelo
Checked out the way it's done in System.Drawing.Color and emulated that. Seems to work. Thanks!
Hauge