1
我在我的MVC應用程序中使用基於URL的本地化邏輯。 因此,默認路由將是mysite/someControler,並且本地化路由將是mysite/en-US/someControler。在MVC中更改文化
「en-US」是具有默認值的「culture」參數的值。
我在想什麼有什麼通用的方式來切換文化,並保持所有的url路由值和參數?
感謝
我在我的MVC應用程序中使用基於URL的本地化邏輯。 因此,默認路由將是mysite/someControler,並且本地化路由將是mysite/en-US/someControler。在MVC中更改文化
「en-US」是具有默認值的「culture」參數的值。
我在想什麼有什麼通用的方式來切換文化,並保持所有的url路由值和參數?
感謝
我做一些簡單:
在我的基本控制器(所有其他控制器從它繼承)我設置以下的ViewData位:
If Request.Path.Contains("/en/") Then
ViewData("PathEs") = Request.Path.Replace("/en/", "/es/")
ViewData("PathPt") = Request.Path.Replace("/en/", "/pt/")
ElseIf Request.Path.Contains("/pt/") Then
ViewData("PathEn") = Request.Path.Replace("/pt/", "/en/")
ViewData("PathEs") = Request.Path.Replace("/pt/", "/es/")
Else
ViewData("PathEn") = Request.Path.Replace("/es/", "/en/")
ViewData("PathPt") = Request.Path.Replace("/es/", "/pt/")
End If
然後在母版頁
<div id="langbar">
<% if not string.isnullorempty(ViewData("PathEs")) then %>
<a href="<%= ViewData("PathEs") %>">Español</a>
<% end if %>
<% if not string.isnullorempty(ViewData("PathEn")) then %>
<a href="<%= ViewData("PathEn") %>">English</a>
<% end if %>
<% if not string.isnullorempty(ViewData("PathPt")) then %>
<a href="<%= ViewData("PathPt") %>">Portugues</a>
<% end if %>
</div>
不是太聰明,但是如果你的處理方法很少,你就可以把工作做得很好(你確實可以優化兩種雜種)
http://adamyan.blogspot.com/2010/02/aspnet-mvc-2-localization-complete.html –