Question

So, I am looking to do some custom Data Field Validation on some models in the .NET project I am currently working on.

These involve a Default Language and a Default Currency. In order to meet the standards of this application and the various other applications and services involved, the currency needs to be a ISO 4217 (3 character currency) standard and the language needs to be a ISO 639-1 (2 character language).

Seeing as no model validation is being done for this anywhere yet, I have the joy of setting it up and before I go about making some huge custom data validator, I was wondering if anyone knew of any existing libraries that readily provide these standards, even if it's something like checking a string that I can plug into a Data Validator myself.

Any advice and/or recommendations would be greatly appreciated.

Was it helpful?

Solution

Okay so I found a workable solution to this. It's a little inelegant but it works.

The ISO 639-1 (2 character language) code can be found as the TwoLetterISORegionName property on the RegionInfo object in the System.Globalization namespace.

The ISO 4217 (3 character currency) code can be found on the ISOCurrenySymbol property on the same object.

To create a region info, use:

new RegionInfo(Int32)

where the int is the culture identifier

To Generate a list of culture identifiers it is quite simple to use

System.Globalization.CultureInfo.GetCultures(CULTURETYPE)

where CULTURETYPE is either the enum or respective number for various culture types (just be sure to choose one that doesn't contain neutral cultures as these do not map correctly). This returns an array of CultureInfo objects, each of which contains an LCID property. This property can be used to construct the respective RegionInfo and you can take what you need from there.

OTHER TIPS

I've been having the same problem myself, and I think your problem can be solved as well with this sources I found.

This source teachs us how to find the CultureInfo objects associated to a currency code in ISO 4217 format.

http://eddietech.blogspot.com.es/2008/01/find-cultureinfo-by-iso-currency-code.html

For each of these objects (there can be more than one culture using the same currency code) you can read the TwoLetterISOLanguageName property for the associated ISO 639-1 format language code.

You can also read the Name property of each CultureInfo object where you can find the "(ISO 639-1)-(ISO 3166)" (each code of 2 letter long) of that culture (example: "es-ES", "en-GB", etc.).

Another great source that covers this topic in C# can be found here.

Started by Kira Namida's answer:

Sprinkled with a litle Linq, this would be a list of all known ISO 4217 characters known on the system:

using System.Globalization;
using System.Linq;

IEnumerable<string> currencySymbols = CultureInfo.GetCultures(CultureTypes.SpecificCultures) //Only specific cultures contain region information
    .Select(x => (new RegionInfo(x.LCID)).ISOCurrencySymbol)
    .Distinct()
    .OrderBy(x=>x)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top