S
shapper
Hello,
I have a class named CultureAttribute which is used as follows:
[Culture(CookieName = "Culture", Cultures = "en-US,pt-PT",
DefaultCulture = "pt-PT")]
public class BaseController : Controller {
}
The problem is that I would like to use the Culture attribute as
follows:
[Culture(CookieName = Config.Culture, Cultures = Config.Cultures,
DefaultCulture = Config.DefaultCulture)]
public class BaseController : Controller {
}
However this is not allowed. I don't want to access Config properties
directly from CultureAttribute Class because I would like to have this
class in a library which is common to all projects.
My idea is maybe to be able to use something like:
[Culture]
public class BaseController : Controller {
}
So when a property value is not defined I could get use default value
from the Config.
So I would maybe ??? make my CultureAttribute partial and on each
project I would create a partial class of CultureAttribute and for
each property I would check if it is null and if true get the default
values from Config.
Does this make sense?
What is the correct way to do this?
My entire CultureAttribute class code is as follows:
public class CultureAttribute : FilterAttribute, IActionFilter {
public String Cultures { get; set; }
public String CookieName { get; set; }
public String DefaultCulture { get; set; }
public void OnActionExecuting(ActionExecutingContext
filterContext) {
String code = SetCulture(filterContext, CookieName,
Cultures.Split(), DefaultCulture);
if (String.IsNullOrEmpty(code)) return;
HttpContext.Current.Response.Cookies.Add(
new HttpCookie(CookieName, code) {
HttpOnly = true,
Expires = DateTime.Now.AddYears(100)
}
);
filterContext.HttpContext.Session[CookieName] = code;
CultureInfo culture = new CultureInfo(code);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
} // OnActionExecuting
public void OnActionExecuted(ActionExecutedContext filterContext)
{
} // OnActionExecuted
private static String SetCulture(ActionExecutingContext
filterContext, String cookieName, IList<String> cultures, String
defaultCulture) {
String cookieValue = GetCookieCulture(filterContext, cultures,
cookieName);
// Check cookie culture
if (String.IsNullOrEmpty(cookieValue)) {
String sessionValue = GetSessionCulture(filterContext,
cultures, cookieName);
// Check session culture
if (String.IsNullOrEmpty(sessionValue)) {
String browserCulture = GetBrowserCulture(filterContext,
cultures);
return String.IsNullOrEmpty(browserCulture) ?
defaultCulture : browserCulture;
}
return sessionValue;
}
return cookieValue;
} // SetCulture
private static String GetCookieCulture(ActionExecutingContext
filterContext, ICollection<String> cultures, String cookieName) {
// Get language in culture
HttpCookie userCookie =
filterContext.RequestContext.HttpContext.Request.Cookies[cookieName];
// Check cookie
if (userCookie != null) {
if (!String.IsNullOrEmpty(userCookie.Value)) {
if (cultures.Contains(userCookie.Value))
return userCookie.Value;
else
return String.Empty;
}
return String.Empty;
}
return String.Empty;
} // GetCookieCulture
private static String GetSessionCulture(ActionExecutingContext
filterContext, ICollection<String> cultures, String cookieName) {
// Check session
if (filterContext.RequestContext.HttpContext.Session
[cookieName] != null) {
String sessionCulture =
filterContext.RequestContext.HttpContext.Session[cookieName].ToString
();
if (!String.IsNullOrEmpty(sessionCulture))
return cultures.Contains(sessionCulture) ? sessionCulture :
String.Empty;
else
return String.Empty;
}
return String.Empty;
} // GetSessionCulture
private static String GetBrowserCulture(ActionExecutingContext
filterContext, IEnumerable<String> cultures) {
// Get languages from browser
IList<String> browserLanguages =
filterContext.RequestContext.HttpContext.Request.UserLanguages;
// Check browser languages
foreach (String bl in browserLanguages) {
foreach (String c in cultures) {
if (c != bl)
continue;
return c;
}
}
return String.Empty;
} // GetBrowserCulture
} // CultureAttribute
Thank You,
Miguel
I have a class named CultureAttribute which is used as follows:
[Culture(CookieName = "Culture", Cultures = "en-US,pt-PT",
DefaultCulture = "pt-PT")]
public class BaseController : Controller {
}
The problem is that I would like to use the Culture attribute as
follows:
[Culture(CookieName = Config.Culture, Cultures = Config.Cultures,
DefaultCulture = Config.DefaultCulture)]
public class BaseController : Controller {
}
However this is not allowed. I don't want to access Config properties
directly from CultureAttribute Class because I would like to have this
class in a library which is common to all projects.
My idea is maybe to be able to use something like:
[Culture]
public class BaseController : Controller {
}
So when a property value is not defined I could get use default value
from the Config.
So I would maybe ??? make my CultureAttribute partial and on each
project I would create a partial class of CultureAttribute and for
each property I would check if it is null and if true get the default
values from Config.
Does this make sense?
What is the correct way to do this?
My entire CultureAttribute class code is as follows:
public class CultureAttribute : FilterAttribute, IActionFilter {
public String Cultures { get; set; }
public String CookieName { get; set; }
public String DefaultCulture { get; set; }
public void OnActionExecuting(ActionExecutingContext
filterContext) {
String code = SetCulture(filterContext, CookieName,
Cultures.Split(), DefaultCulture);
if (String.IsNullOrEmpty(code)) return;
HttpContext.Current.Response.Cookies.Add(
new HttpCookie(CookieName, code) {
HttpOnly = true,
Expires = DateTime.Now.AddYears(100)
}
);
filterContext.HttpContext.Session[CookieName] = code;
CultureInfo culture = new CultureInfo(code);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
} // OnActionExecuting
public void OnActionExecuted(ActionExecutedContext filterContext)
{
} // OnActionExecuted
private static String SetCulture(ActionExecutingContext
filterContext, String cookieName, IList<String> cultures, String
defaultCulture) {
String cookieValue = GetCookieCulture(filterContext, cultures,
cookieName);
// Check cookie culture
if (String.IsNullOrEmpty(cookieValue)) {
String sessionValue = GetSessionCulture(filterContext,
cultures, cookieName);
// Check session culture
if (String.IsNullOrEmpty(sessionValue)) {
String browserCulture = GetBrowserCulture(filterContext,
cultures);
return String.IsNullOrEmpty(browserCulture) ?
defaultCulture : browserCulture;
}
return sessionValue;
}
return cookieValue;
} // SetCulture
private static String GetCookieCulture(ActionExecutingContext
filterContext, ICollection<String> cultures, String cookieName) {
// Get language in culture
HttpCookie userCookie =
filterContext.RequestContext.HttpContext.Request.Cookies[cookieName];
// Check cookie
if (userCookie != null) {
if (!String.IsNullOrEmpty(userCookie.Value)) {
if (cultures.Contains(userCookie.Value))
return userCookie.Value;
else
return String.Empty;
}
return String.Empty;
}
return String.Empty;
} // GetCookieCulture
private static String GetSessionCulture(ActionExecutingContext
filterContext, ICollection<String> cultures, String cookieName) {
// Check session
if (filterContext.RequestContext.HttpContext.Session
[cookieName] != null) {
String sessionCulture =
filterContext.RequestContext.HttpContext.Session[cookieName].ToString
();
if (!String.IsNullOrEmpty(sessionCulture))
return cultures.Contains(sessionCulture) ? sessionCulture :
String.Empty;
else
return String.Empty;
}
return String.Empty;
} // GetSessionCulture
private static String GetBrowserCulture(ActionExecutingContext
filterContext, IEnumerable<String> cultures) {
// Get languages from browser
IList<String> browserLanguages =
filterContext.RequestContext.HttpContext.Request.UserLanguages;
// Check browser languages
foreach (String bl in browserLanguages) {
foreach (String c in cultures) {
if (c != bl)
continue;
return c;
}
}
return String.Empty;
} // GetBrowserCulture
} // CultureAttribute
Thank You,
Miguel