Search code examples
c#.netisomodel-validation

Data Member Validation - ISO 4217 (currency) and 639-1 (language)


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.


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.