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

做~自己de王妃 提交于 2019-12-08 06:47:56

问题


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.


回答1:


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.




回答2:


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.




回答3:


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)


来源:https://stackoverflow.com/questions/16109121/data-member-validation-iso-4217-currency-and-639-1-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!