code_junkie

ASP.Net Validating Culture Names

Nov 13th, 2011
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. System.Globalization.CultureInfo ci;
  2. try
  3. {
  4. ci = new System.Globalization.CultureInfo(Request.QueryString["lang"] ?? string.Empty);
  5. }
  6. catch
  7. {
  8. ci = new System.Globalization.CultureInfo(string.Empty);
  9. }
  10.  
  11. private static bool IsValidCultureName(string cultureName)
  12. {
  13. CultureInfo[] cultures =
  14. CultureInfo.GetCultures(CultureTypes.SpecificCultures);
  15. foreach (CultureInfo culture in cultures)
  16. {
  17. if (culture.Name == cultureName)
  18. return true;
  19. }
  20.  
  21. return false;
  22. }
  23.  
  24. CultureInfo[] cultures = System.Globalization.CultureInfo.GetCultures
  25. (CultureTypes.SpecificCultures);
  26.  
  27. var selectCulture = from p in cultures
  28. where p.Name == value
  29. select p;
  30.  
  31. if (selectCulture.Count() == 1)
  32. {
  33. // your culture is good
  34. }
  35.  
  36. private static bool IsValidCultureInfoName(string name)
  37. {
  38. return
  39. CultureInfo
  40. .GetCultures(CultureTypes.SpecificCultures)
  41. .Any(c => c.Name == name);
  42. }
Add Comment
Please, Sign In to add comment