1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Configuration
;
19 using System
.Globalization
;
20 using System
.Threading
;
23 /// Enum to identify where a value is stored.
25 public enum RequestStore
28 /// Value is stored in the Session object.
32 /// Value is stored in a cookie object.
36 /// Value is stored in the querystring.
40 /// Value is stored in the form collection.
44 /// Value is stored in either query string or form collection.
50 /// The LocalizationFilter can be used to determine the culture to use
51 /// for resources and UI.
53 public class LocalizationFilter
: IFilter
, IFilterAttributeAware
55 private LocalizationFilterAttribute setup
;
57 #region IFilterAttributeAware Members
62 /// <value>The filter.</value>
63 public FilterAttribute Filter
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
;
79 #region IFilter Members
82 /// Executes a sequence of steps to determine the browser location/culture.
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>
89 /// <c>true</c> if the action should be invoked, otherwise <c>false</c>
91 public bool Perform(ExecuteWhen exec
, IEngineContext context
, IController controller
, IControllerContext controllerContext
)
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
;
110 if (setup
.FailOnError
) throw;
118 #region Get locale id from the store
121 /// Gets the user language.
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];
134 /// Gets the locale id.
136 /// <param name="context">The context.</param>
137 /// <returns></returns>
138 private String
GetLocaleId(IEngineContext context
)
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
];