-1

I am already keeping International calling code (Phone Code) in database as shown below.

Customer table columns

  • FirstName
  • LastName
  • Email
  • PhoneCode

Sample Phone Codes

  • +46

  • +44

  • +47

Expecated outcome

var phoneCode = "+47"; //
var cultureInfo = <some way to achieve it by using phoneCode>

Since I already keep phone code in my table, I want to know is it possible to get locale from the phone code rather than having a separate column to keep locale. I searched a lot on this but could not found anything useful.

Suvethan Nantha
  • 2,404
  • 16
  • 28
  • 1
    There's a *ton* of useful information. There's even [a Wikipedia article](https://en.wikipedia.org/wiki/List_of_country_calling_codes) with international country codes. A simple dictionary or lookup table would be enough to match codes to countries and from there, CultureInfo objects that start with that country code. Are you looking for a ready-made database or library perhaps, so you don't need to fill the table yourself? – Panagiotis Kanavos Sep 06 '18 at 07:41
  • @PanagiotisKanavos yes I know there's a lot of information in wikipedia regarding international country codes. What I want is from international phone codes I need to get country language (locale). Are there any possible ways to get it done? – Suvethan Nantha Sep 06 '18 at 07:44
  • What do you mean locale? In the literal sense, locale *is* the country, which you already know by its two-letter ISO code. In .NET it's a CultureInfo object. In the database, it's the table's collation. CultureInfo codes start with the two-letter country code so you *already* know all locales that match a specific calling code. There are lookup tables for additional cultures to cover cases like `de-AT` for Austria. Database collations have more complex names. – Panagiotis Kanavos Sep 06 '18 at 07:47
  • @PanagiotisKanavos yes I meant CultureInfo object. I don't keep two letter country code in my database at the moment but I have calling code. What I'm asking is, is there a way to get the culture from calling code in c# – Suvethan Nantha Sep 06 '18 at 07:51
  • Then create that table in the database and fill it from the Wikipedia article or any other source you want. The duplicate shows how to find the CultureInfo once you have the country code. The second part of any locale code is the two-letter ISO country code – Panagiotis Kanavos Sep 06 '18 at 07:54
  • @PanagiotisKanavos can't I get the cultureInfo by calling code without keeping the two letter ISO country code in the database? Is there any possible way we can find cultureInfo by calling code? – Suvethan Nantha Sep 06 '18 at 07:56
  • 4
    If with locale you mean a language code, please note that a translation between international dialling codes and language code is not possible for all countries (or even in general). Example: +41 is for Switzerland but how do you decide whether to choose German, French, Italian or Rumantsch as a locale? – dumetrulo Sep 06 '18 at 07:57
  • @dumetrulo and some countries may share the same calling code. The OP though wants to match call code to country letter without storing the lookup info. – Panagiotis Kanavos Sep 06 '18 at 08:00
  • @dumetrulo actually I only need to cover Scandinavian countries in my context. so I don't think it's not a problem so is there any way to find it using calling code? – Suvethan Nantha Sep 06 '18 at 08:04
  • Finland is a Scandinavian country, and a significant portion of people in Finland speak Swedish… also, Sami (basically the same as Finnish) is a recognized minority language in Sweden. – dumetrulo Sep 06 '18 at 08:05
  • @SuvethanNantha there's no built-in database in Windows or .NET if that's what you ask, just like there's no built-in database for area codes or post codes. Such data is *highly* volatile, which is why there are companies that sell such information as lookup services or regularly updated databases. – Panagiotis Kanavos Sep 06 '18 at 08:06
  • @SuvethanNantha in any case, you have your answer. Create and fill a lookup table. It won't even take 30 minutes. – Panagiotis Kanavos Sep 06 '18 at 08:07
  • @PanagiotisKanavos thanks I'll look on it. I just wanted to know there is a possiblity to get cultural info without having a table. – Suvethan Nantha Sep 06 '18 at 08:08

1 Answers1

0

In this post the prefix country number is retrieved by a dictionary using the ISO 3166-1 2-code.

public static string getCountryCode(string PhoneCode)
{
    var dictionary = new Dictionary<string, string>();

    dictionary.Add("AC", "+247");
    dictionary.Add("AD", "+376");
    dictionary.Add("AE", "+971");
    dictionary.Add("AF", "+93");
    dictionary.Add("AG", "+1-268");
    dictionary.Add("AI", "+1-264");
    dictionary.Add("AL", "+355");
    dictionary.Add("AM", "+374");
    dictionary.Add("AN", "+599");
    dictionary.Add("AO", "+244");
    dictionary.Add("AR", "+54");
    dictionary.Add("AS", "+1-684");
    dictionary.Add("AT", "+43");
    dictionary.Add("AU", "+61");
    dictionary.Add("AW", "+297");
    dictionary.Add("AX", "+358-18");
    dictionary.Add("AZ", "+994"); // or +374-97
    dictionary.Add("BA", "+387");
    dictionary.Add("BB", "+1-246");
    dictionary.Add("BD", "+880");
    dictionary.Add("BE", "+32");
    dictionary.Add("BF", "+226");
    dictionary.Add("BG", "+359");
    dictionary.Add("BH", "+973");
    dictionary.Add("BI", "+257");
    dictionary.Add("BJ", "+229");
    dictionary.Add("BM", "+1-441");
    dictionary.Add("BN", "+673");
    dictionary.Add("BO", "+591");
    dictionary.Add("BR", "+55");
    dictionary.Add("BS", "+1-242");
    dictionary.Add("BT", "+975");
    dictionary.Add("BW", "+267");
    dictionary.Add("BY", "+375");
    dictionary.Add("BZ", "+501");
    dictionary.Add("CA", "+1");
    dictionary.Add("CC", "+61");
    dictionary.Add("CD", "+243");
    dictionary.Add("CF", "+236");
    dictionary.Add("CG", "+242");
    dictionary.Add("CH", "+41");
    dictionary.Add("CI", "+225");
    dictionary.Add("CK", "+682");
    dictionary.Add("CL", "+56");
    dictionary.Add("CM", "+237");
    dictionary.Add("CN", "+86");
    dictionary.Add("CO", "+57");
    dictionary.Add("CR", "+506");
    dictionary.Add("CS", "+381");
    dictionary.Add("CU", "+53");
    dictionary.Add("CV", "+238");
    dictionary.Add("CX", "+61");
    dictionary.Add("CY", "+357"); // or +90-392
    dictionary.Add("CZ", "+420");
    dictionary.Add("DE", "+49");
    dictionary.Add("DJ", "+253");
    dictionary.Add("DK", "+45");
    dictionary.Add("DM", "+1-767");
    dictionary.Add("DO", "+1-809"); // and 1-829?
    dictionary.Add("DZ", "+213");
    dictionary.Add("EC", "+593");
    dictionary.Add("EE", "+372");
    dictionary.Add("EG", "+20");
    dictionary.Add("EH", "+212");
    dictionary.Add("ER", "+291");
    dictionary.Add("ES", "+34");
    dictionary.Add("ET", "+251");
    dictionary.Add("FI", "+358");
    dictionary.Add("FJ", "+679");
    dictionary.Add("FK", "+500");
    dictionary.Add("FM", "+691");
    dictionary.Add("FO", "+298");
    dictionary.Add("FR", "+33");
    dictionary.Add("GA", "+241");
    dictionary.Add("GB", "+44");
    dictionary.Add("GD", "+1-473");
    dictionary.Add("GE", "+995");
    dictionary.Add("GF", "+594");
    dictionary.Add("GG", "+44");
    dictionary.Add("GH", "+233");
    dictionary.Add("GI", "+350");
    dictionary.Add("GL", "+299");
    dictionary.Add("GM", "+220");
    dictionary.Add("GN", "+224");
    dictionary.Add("GP", "+590");
    dictionary.Add("GQ", "+240");
    dictionary.Add("GR", "+30");
    dictionary.Add("GT", "+502");
    dictionary.Add("GU", "+1-671");
    dictionary.Add("GW", "+245");
    dictionary.Add("GY", "+592");
    dictionary.Add("HK", "+852");
    dictionary.Add("HN", "+504");
    dictionary.Add("HR", "+385");
    dictionary.Add("HT", "+509");
    dictionary.Add("HU", "+36");
    dictionary.Add("ID", "+62");
    dictionary.Add("IE", "+353");
    dictionary.Add("IL", "+972");
    dictionary.Add("IM", "+44");
    dictionary.Add("IN", "+91");
    dictionary.Add("IO", "+246");
    dictionary.Add("IQ", "+964");
    dictionary.Add("IR", "+98");
    dictionary.Add("IS", "+354");
    dictionary.Add("IT", "+39");
    dictionary.Add("JE", "+44");
    dictionary.Add("JM", "+1-876");
    dictionary.Add("JO", "+962");
    dictionary.Add("JP", "+81");
    dictionary.Add("KE", "+254");
    dictionary.Add("KG", "+996");
    dictionary.Add("KH", "+855");
    dictionary.Add("KI", "+686");
    dictionary.Add("KM", "+269");
    dictionary.Add("KN", "+1-869");
    dictionary.Add("KP", "+850");
    dictionary.Add("KR", "+82");
    dictionary.Add("KW", "+965");
    dictionary.Add("KY", "+1-345");
    dictionary.Add("KZ", "+7");
    dictionary.Add("LA", "+856");
    dictionary.Add("LB", "+961");
    dictionary.Add("LC", "+1-758");
    dictionary.Add("LI", "+423");
    dictionary.Add("LK", "+94");
    dictionary.Add("LR", "+231");
    dictionary.Add("LS", "+266");
    dictionary.Add("LT", "+370");
    dictionary.Add("LU", "+352");
    dictionary.Add("LV", "+371");
    dictionary.Add("LY", "+218");
    dictionary.Add("MA", "+212");
    dictionary.Add("MC", "+377");
    dictionary.Add("MD", "+373"); // or +373-533
    dictionary.Add("ME", "+382");
    dictionary.Add("MG", "+261");
    dictionary.Add("MH", "+692");
    dictionary.Add("MK", "+389");
    dictionary.Add("ML", "+223");
    dictionary.Add("MM", "+95");
    dictionary.Add("MN", "+976");
    dictionary.Add("MO", "+853");
    dictionary.Add("MP", "+1-670");
    dictionary.Add("MQ", "+596");
    dictionary.Add("MR", "+222");
    dictionary.Add("MS", "+1-664");
    dictionary.Add("MT", "+356");
    dictionary.Add("MU", "+230");
    dictionary.Add("MV", "+960");
    dictionary.Add("MW", "+265");
    dictionary.Add("MX", "+52");
    dictionary.Add("MY", "+60");
    dictionary.Add("MZ", "+258");
    dictionary.Add("NA", "+264");
    dictionary.Add("NC", "+687");
    dictionary.Add("NE", "+227");
    dictionary.Add("NF", "+672");
    dictionary.Add("NG", "+234");
    dictionary.Add("NI", "+505");
    dictionary.Add("NL", "+31");
    dictionary.Add("NO", "+47");
    dictionary.Add("NP", "+977");
    dictionary.Add("NR", "+674");
    dictionary.Add("NU", "+683");
    dictionary.Add("NZ", "+64");
    dictionary.Add("OM", "+968");
    dictionary.Add("PA", "+507");
    dictionary.Add("PE", "+51");
    dictionary.Add("PF", "+689");
    dictionary.Add("PG", "+675");
    dictionary.Add("PH", "+63");
    dictionary.Add("PK", "+92");
    dictionary.Add("PL", "+48");
    dictionary.Add("PM", "+508");
    dictionary.Add("PR", "+1-787"); // and 1-939 ?
    dictionary.Add("PS", "+970");
    dictionary.Add("PT", "+351");
    dictionary.Add("PW", "+680");
    dictionary.Add("PY", "+595");
    dictionary.Add("QA", "+974");
    dictionary.Add("RE", "+262");
    dictionary.Add("RO", "+40");
    dictionary.Add("RS", "+381");
    dictionary.Add("RU", "+7");
    dictionary.Add("RW", "+250");
    dictionary.Add("SA", "+966");
    dictionary.Add("SB", "+677");
    dictionary.Add("SC", "+248");
    dictionary.Add("SD", "+249");
    dictionary.Add("SE", "+46");
    dictionary.Add("SG", "+65");
    dictionary.Add("SH", "+290");
    dictionary.Add("SI", "+386");
    dictionary.Add("SJ", "+47");
    dictionary.Add("SK", "+421");
    dictionary.Add("SL", "+232");
    dictionary.Add("SM", "+378");
    dictionary.Add("SN", "+221");
    dictionary.Add("SO", "+252");
    dictionary.Add("SR", "+597");
    dictionary.Add("ST", "+239");
    dictionary.Add("SV", "+503");
    dictionary.Add("SY", "+963");
    dictionary.Add("SZ", "+268");
    dictionary.Add("TA", "+290");
    dictionary.Add("TC", "+1-649");
    dictionary.Add("TD", "+235");
    dictionary.Add("TG", "+228");
    dictionary.Add("TH", "+66");
    dictionary.Add("TJ", "+992");
    dictionary.Add("TK", "+690");
    dictionary.Add("TL", "+670");
    dictionary.Add("TM", "+993");
    dictionary.Add("TN", "+216");
    dictionary.Add("TO", "+676");
    dictionary.Add("TR", "+90");
    dictionary.Add("TT", "+1-868");
    dictionary.Add("TV", "+688");
    dictionary.Add("TW", "+886");
    dictionary.Add("TZ", "+255");
    dictionary.Add("UA", "+380");
    dictionary.Add("UG", "+256");
    dictionary.Add("US", "+1");
    dictionary.Add("UY", "+598");
    dictionary.Add("UZ", "+998");
    dictionary.Add("VA", "+379");
    dictionary.Add("VC", "+1-784");
    dictionary.Add("VE", "+58");
    dictionary.Add("VG", "+1-284");
    dictionary.Add("VI", "+1-340");
    dictionary.Add("VN", "+84");
    dictionary.Add("VU", "+678");
    dictionary.Add("WF", "+681");
    dictionary.Add("WS", "+685");
    dictionary.Add("YE", "+967");
    dictionary.Add("YT", "+262");
    dictionary.Add("ZA", "+27");
    dictionary.Add("ZM", "+260");
    dictionary.Add("ZW", "+263");

    var countyCode = dictionary.Where(d => d.Value == PhoneCode).Select(d => d.Key).FirstOrDefault();

        return countyCode;
}

And call the function:

string isoCountryCode = getCountryCode("+47") //<= Will return "NO"
var cultureInfo = CultureInfo.GetCultures(CultureTypes.AllCultures)
                      .Where(c => c.Name.EndsWith("-" + isoCountryCode )).First;
Yanga
  • 2,885
  • 1
  • 29
  • 32