- Fixed MR-84
[castle.git] / MonoRail / Castle.MonoRail.Framework / Filters / LocalizationFilter.cs
blob867acc95ad542b27c0551826084dbd7cef291fb6
1 // Copyright 2004-2007 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 /// <returns>
88 /// <c>true</c> if the action should be invoked, otherwise <c>false</c>
89 /// </returns>
90 public bool Perform(ExecuteEnum exec, IRailsEngineContext context, IController controller)
92 try
94 String localeId = GetLocaleId(context);
96 if (localeId == null && setup.UseBrowser)
97 localeId = GetUserLanguage(context.Request);
99 if (localeId != null)
101 CultureInfo culture = CultureInfo.CreateSpecificCulture(localeId);
103 Thread.CurrentThread.CurrentCulture = culture;
104 Thread.CurrentThread.CurrentUICulture = culture;
107 catch
109 if (setup.FailOnError) throw;
112 return true;
115 #endregion
117 #region Get locale id from the store
119 /// <summary>
120 /// Gets the user language.
121 /// </summary>
122 /// <param name="request">The request.</param>
123 /// <returns></returns>
124 private String GetUserLanguage(IRequest request)
126 if (request.UserLanguages != null && request.UserLanguages.Length > 0)
127 return request.UserLanguages[0];
129 return null;
132 /// <summary>
133 /// Gets the locale id.
134 /// </summary>
135 /// <param name="context">The context.</param>
136 /// <returns></returns>
137 private String GetLocaleId(IRailsEngineContext context)
139 switch(setup.Store)
141 case RequestStore.Session:
142 return context.Session[setup.Key] as String;
143 case RequestStore.Cookie:
144 return context.Request.ReadCookie(setup.Key);
145 case RequestStore.QueryString:
146 return context.Request.QueryString[setup.Key];
147 case RequestStore.Form:
148 return context.Request.Form[setup.Key];
149 case RequestStore.Params:
150 return context.Request.Params[setup.Key];
153 return null;
156 #endregion