tags:

views:

327

answers:

4

I will retrieve this data from an xml to initialize it for thousands of objects.

So if MyObject has a Country and Language property, what should they be, and how should they be represented both in code and in xml.

I am thinking of using an Enum in code.

I am just looking for other people's opinions to find the best way to do this. Is all Languages and Countries even in the BCL so I can use the minstead of writing one?

Also Country and Language shouldn't be combined. The Language will not represent the language spoken in the specified Country.

How to best implement this both for code and in the Xml.

Should I do it like?:

<Language>English</Language>
+8  A: 

Consider looking at CultureInfo's constructor that takes a string for examples (e.g. "en-us")

Provides information about a specific culture (called a "locale" for unmanaged code development). The information includes the names for the culture, the writing system, the calendar used, and formatting for dates and sort strings.

It's fairly specific to what's actually out there being used in the real world (e.g. the country/language pairs make sense). You can also create your own cultures.

You specified that the language might not be the one spoken in the country. Could you clarify what exactly you'll use it for? Without further information, it seems like you're trying to define something similar to a CultureInfo.

Alternatively, you could also define a simple object that just had the two properties (Country and Language) where Country is an ISO 3166-2 string and Language is an ISO 639-1 string as Edward Loper suggests below.

You could store a list of the ISO codes in an XML file and parse them using traditional techniques. I suggested CultureInfo because you specified that you were looking for something already in the BCL.

Using enums is, in general, discouraged by the Framework Design Guidelines for open sets:

DO NOT use an enum for open sets (such as the operating system version, names of your friends, etc.).

You could take a hybrid approach where you define two static class that has a bunch of static readonly strings as in

// ISO-3166-2 codes
public static Countries
{
   public static readonly string France = "FR";
   ...
}

// CultureInfo style codes
public static Languages
{
   public static readonly string BritishEnglish = "en-GB";
}

UPDATE: Based on your comment that this is specifically for movies, you could still use a CultureInfo for the culture where it was produced and the culture of the content. Anything beyond that is probably too political for Microsoft to be involved with making it part of the OS (c.f. this). Thus, you'd have to define your own.

Jeff Moser
Thanks, does it have all languages. And what advantage CultureInfo would give me instead of just having a Country and Language? I also think this will give me more languages, right? Like New Zealand English, Britain English, etc?
Joan Venge
+1 - use what's there. There's no point in re-inventing the wheel.
ChrisF
Thanks, but this is for movies. So the movie can be from France but in English.
Joan Venge
@Joan Venge - ah, sorry. I only skim-read the question and missed that bit.
ChrisF
Thanks, no worries. I didn't know about CultureInfo so I learnt from you, lol.
Joan Venge
That's funny Jeff, hehe.
Joan Venge
anyone got a class similar to this with all the countries?
Luke Lowrey
+1  A: 

I would save and retrieve from xml using a prebuilt serialization library (e.g. this tutorial). Also, I would avoid using an enum and simply use strings or something like CultureInfo.

Pace
But I could use a string in the xml and use enum in code, right? Or you mean you wouldn't use enum in code too?
Joan Venge
I wouldn't use an enum in code. Generally if there are more than 3 or 4 possibilities than an enum isn't the right approach.
Pace
THanks. You think for this case, it's a bad approach? To me it seems like it's the simplest.
Joan Venge
A: 

This might be the case where you can use the xml:lang attribute of an element as described here: http://docs.oasis-open.org/dita/v1.1/OS/archspec/xmllang.html

As it suggests, you then use the "country-LANGUAGE" identifiers for locales. This can be used to create a CultureInfo object when deserializing, i.e. I would use CultureInfo objects in my code.

naivists
+2  A: 

ISO 639-2 (Codes for the representation of names of languages) gives a fairly complete list of human languages, along with standard codes you can use for those languages:

http://www.loc.gov/standards/iso639-2/php/code%5Flist.php

Similarly, ISO 3166-1 gives standard codes for all countries:

http://en.wikipedia.org/wiki/ISO%5F3166-1

Edward Loper