PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Web/Components/CultureHelperHttpModule.cs

#
C# | 168 lines | 95 code | 35 blank | 38 comment | 12 complexity | a41e2163a7c5c401dc5f37afa5b8895c MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause, CPL-1.0, CC-BY-SA-3.0, GPL-2.0
  1. using System;
  2. using System.Configuration;
  3. using System.Globalization;
  4. using System.Threading;
  5. using System.Web;
  6. using log4net;
  7. using mojoPortal.Business;
  8. using mojoPortal.Business.WebHelpers;
  9. using mojoPortal.Web.Framework;
  10. namespace mojoPortal.Web
  11. {
  12. public class CultureHelperHttpModule : IHttpModule
  13. {
  14. private static readonly ILog log = LogManager.GetLogger(typeof(CultureHelperHttpModule));
  15. public void Init(HttpApplication application)
  16. {
  17. application.Error += new EventHandler(this.Error);
  18. application.BeginRequest += new EventHandler(BeginRequest);
  19. application.EndRequest += new EventHandler(this.EndRequest);
  20. }
  21. private void BeginRequest(object sender, EventArgs e)
  22. {
  23. HttpApplication app = (HttpApplication)sender;
  24. if (WebUtils.IsRequestForStaticFile(app.Request.Path)) { return; }
  25. if (app.Request.Path.ContainsCaseInsensitive("csshandler.ashx")) { return; }
  26. if (app.Request.Path.ContainsCaseInsensitive("thumbnailservice.ashx")) { return; }
  27. if (app.Request.Path.ContainsCaseInsensitive("GCheckoutNotificationHandler.ashx")) { return; }
  28. // 2006-12-29 Joe Audette
  29. // CultureInfo for the executing thread is automatically set to the
  30. // preferred culture of the user's browser by this web.config setting:
  31. // <globalization
  32. // culture="auto:en-US"
  33. // uiCulture="auto:en"
  34. // requestEncoding="utf-8"
  35. // responseEncoding="utf-8"
  36. // fileEncoding="iso-8859-15" />
  37. //
  38. // the "auto" tells the runtime to use the browser preference
  39. // and the :en-US tells the runtime to fall back to en-US as the default culture for
  40. // missing resource keys or if no resource file exists for the preferred culture
  41. // you can specify a different default culture by replacing en-US with your preferred
  42. // culture, but you should make sure that the resource file for the default culture
  43. // has no missing keys or runtime errors could occur
  44. //by default the culture of the executing thread is set to that of the browser preferred language setting
  45. // and this causes the use of the language specific resource if available else it falls back to the default en-US
  46. //as defined in Web.config globalization section
  47. //below we are using a config setting to change the default behavior to force the thread to use a specific culture
  48. //however if there are missing keys in the specified culture it can still fall back to en-US as it should
  49. if (WebConfigSettings.UseCultureOverride)
  50. {
  51. CultureInfo siteCulture;
  52. CultureInfo siteUICulture;
  53. try
  54. {
  55. siteCulture = SiteUtils.GetDefaultCulture();
  56. siteUICulture = SiteUtils.GetDefaultUICulture();
  57. }
  58. catch (InvalidOperationException) { return; }
  59. catch (System.Data.Common.DbException) { return; }
  60. if (siteCulture.IsNeutralCulture)
  61. {
  62. log.Info("cannot use culture " + siteCulture.Name + " because it is a neutral culture. It cannot be used in formatting and parsing and therefore cannot be set as the thread's current culture.");
  63. }
  64. else
  65. {
  66. try
  67. {
  68. Thread.CurrentThread.CurrentCulture = siteCulture;
  69. if (WebConfigSettings.SetUICultureWhenSettingCulture)
  70. {
  71. Thread.CurrentThread.CurrentUICulture = siteUICulture;
  72. }
  73. }
  74. catch (ArgumentException ex)
  75. {
  76. log.Info("swallowed error in culture helper", ex);
  77. }
  78. catch (NotSupportedException ex)
  79. {
  80. log.Info("swallowed error in culture helper", ex);
  81. }
  82. }
  83. }
  84. // below we are overriding only to handle culture specific workarounds, for most cultures
  85. // we don't need to do anything here as the runtime handles it correctly
  86. // but there are some cultures which are either poorly or incorrectly implemented
  87. // in the .NET runtime
  88. if (WebConfigSettings.UseCustomHandlingForPersianCulture)
  89. {
  90. if (
  91. (CultureInfo.CurrentCulture.Name == "fa-IR")
  92. || (CultureInfo.CurrentCulture.TwoLetterISOLanguageName == "fa")
  93. )
  94. {
  95. try
  96. {
  97. CultureInfo PersianCulture = CultureHelper.GetPersianCulture();
  98. Thread.CurrentThread.CurrentCulture = PersianCulture;
  99. Thread.CurrentThread.CurrentUICulture = PersianCulture;
  100. }
  101. catch (System.Security.SecurityException ex)
  102. {
  103. //can happen in medium trust
  104. log.Error(ex);
  105. }
  106. catch (ArgumentException ex)
  107. {
  108. log.Info("swallowed error in culture helper", ex);
  109. }
  110. }
  111. }
  112. //this doesn't work but was a nice idea
  113. //http://weblogs.asp.net/abdullaabdelhaq/archive/2009/06/27/displaying-arabic-number.aspx
  114. //has the only real solution
  115. //if ((CultureInfo.CurrentCulture.TwoLetterISOLanguageName == "ar") && (!CultureInfo.CurrentCulture.IsNeutralCulture))
  116. //{
  117. // CultureInfo arabic = new CultureInfo(CultureInfo.CurrentCulture.Name);
  118. // arabic.NumberFormat.DigitSubstitution = DigitShapes.NativeNational;
  119. // string[] arabicDigits = new string[] { "?", "?", "?", "?", "?", "?", "?", "?", "?", "?" };
  120. // arabic.NumberFormat.NativeDigits = arabicDigits;
  121. // Thread.CurrentThread.CurrentCulture = arabic;
  122. // Thread.CurrentThread.CurrentUICulture = arabic;
  123. //}
  124. }
  125. private void Error(object sender, EventArgs e)
  126. {
  127. }
  128. private void EndRequest(object sender, EventArgs e)
  129. {
  130. }
  131. public void Dispose() { }
  132. }
  133. }