2014-01-16 57 views
1

我需要根據特定規則設置當前語言。我需要訪問當前頁面和當前用戶才能作出決定。我查看了文檔並it said在PageBase上使用了InitializeCulture方法。我的項目使用MVC而不是WebForms,相當於MVC中的InitializeCulture是什麼?自定義語言處理EPiServer

回答

4

您可以實現IAuthorizationFilter並在OnAuthorization中執行檢查。也可以在IActionFilter中完成,但OnAuthorization會在前面調用。您將有權訪問當前的HttpContext並從那裏獲取當前頁面數據。

public class LanguageSelectionFilter : IAuthorizationFilter 
{ 
    public void OnAuthorization(AuthorizationContext filterContext) 
    { 
     // access to HttpContext 
     var httpContext = filterContext.HttpContext; 

     // the request's current page 
     var currentPage = filterContext.RequestContext.GetRoutedData<PageData>(); 

     // TODO: decide which language to use and set them like below 
     ContentLanguage.Instance.SetCulture("en"); 
     UserInterfaceLanguage.Instance.SetCulture("en"); 
    } 
} 

public class FilterConfig 
{ 
    public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
    { 
     // register the filter in your FilterConfig file. 
     filters.Add(new LanguageSelectionFilter()); 
    } 
} 
+1

謝謝!這看起來像我之後的事情。我們設法擺脫了EPiServer中的正常回退行爲,而我的問題是由於EPiServer中的錯誤。如果我在法語網頁上,並在我的視圖中調用瑞典語頁面,如下面的@ Url.PageUrl(page.LinkURL)。事實證明,即使頁面是瑞典的,「LinkURL」實際上包含「epslanguage = fr」。我通過使用我自己的Url的HtmlHelper解決了這個問題:https://gist.github.com/anonymous/8565072 – Andreas

+0

這很好理解,謝謝! – aolde