简体   繁体   中英

How to represent countries and languages in C#?

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>

Consider looking at CultureInfo's constructor that takes a string for examples (eg "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 (eg 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 (cf this ). Thus, you'd have to define your own.

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_list.php

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

http://en.wikipedia.org/wiki/ISO_3166-1

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

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, ie I would use CultureInfo objects in my code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM