Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework / Filters / LocalizationFilter.cs
blob3d7b352a71c2b17db98786c21a9483d0c06f9b7c
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.Filters
17 using System;
18 using System.Configuration;
19 using System.Globalization;
20 using System.Threading;
22 /// <summary>
23 /// Enum to identify where a value is stored.
24 /// </summary>
25 public enum RequestStore
27 /// <summary>
28 /// Value is stored in the Session object.
29 /// </summary>
30 Session = 1,
31 /// <summary>
32 /// Value is stored in a cookie object.
33 /// </summary>
34 Cookie = 2,
35 /// <summary>
36 /// Value is stored in the querystring.
37 /// </summary>
38 QueryString = 3,
39 /// <summary>
40 /// Value is stored in the form collection.
41 /// </summary>
42 Form = 4,
43 /// <summary>
44 /// Value is stored in either query string or form collection.
45 /// </summary>
46 Params = 5
49 /// <summary>
50 /// The LocalizationFilter can be used to determine the culture to use
51 /// for resources and UI.
52 /// </summary>
53 public class LocalizationFilter : IFilter, IFilterAttributeAware
55 private LocalizationFilterAttribute setup;
57 #region IFilterAttributeAware Members
59 /// <summary>
60 /// Sets the filter.
61 /// </summary>
62 /// <value>The filter.</value>
63 public FilterAttribute Filter
65 set
67 if (!(value is LocalizationFilterAttribute))
69 String message = "LocalizationFilter can only be defined by a LocalizationFilterAttribute.";
70 throw new ConfigurationErrorsException(message);
73 setup = value as LocalizationFilterAttribute;
77 #endregion
79 #region IFilter Members
81 /// <summary>
82 /// Executes a sequence of steps to determine the browser location/culture.
83 /// </summary>
84 /// <param name="exec">When this filter is being invoked</param>
85 /// <param name="context">Current context</param>
86 /// <param name="controller">The controller instance</param>
87 /// <param name="controllerContext">The controller context.</param>
88 /// <returns>
89 /// <c>true</c> if the action should be invoked, otherwise <c>false</c>
90 /// </returns>
91 public bool Perform(ExecuteWhen exec, IEngineContext context, IController controller, IControllerContext controllerContext)
93 try
95 String localeId = GetLocaleId(context);
97 if (localeId == null && setup.UseBrowser)
98 localeId = GetUserLanguage(context.Request);
100 if (localeId != null)
102 CultureInfo culture = CultureInfo.CreateSpecificCulture(localeId);
104 Thread.CurrentThread.CurrentCulture = culture;
105 Thread.CurrentThread.CurrentUICulture = culture;
108 catch
110 if (setup.FailOnError) throw;
113 return true;
116 #endregion
118 #region Get locale id from the store
120 /// <summary>
121 /// Gets the user language.
122 /// </summary>
123 /// <param name="request">The request.</param>
124 /// <returns></returns>
125 private String GetUserLanguage(IRequest request)
127 if (request.UserLanguages != null && request.UserLanguages.Length > 0)
128 return request.UserLanguages[0];
130 return null;
133 /// <summary>
134 /// Gets the locale id.
135 /// </summary>
136 /// <param name="context">The context.</param>
137 /// <returns></returns>
138 private String GetLocaleId(IEngineContext context)
140 switch(setup.Store)
142 case RequestStore.Session:
143 return context.Session[setup.Key] as String;
144 case RequestStore.Cookie:
145 return context.Request.ReadCookie(setup.Key);
146 case RequestStore.QueryString:
147 return context.Request.QueryString[setup.Key];
148 case RequestStore.Form:
149 return context.Request.Form[setup.Key];
150 case RequestStore.Params:
151 return context.Request.Params[setup.Key];
154 return null;
157 #endregion