mvc .net multilingual application
I want to create multilingual application in mvc.net. I want to add flags of the top of the screen so that use开发者_运维技巧r can change the language. Pleae guide me to do this.
Thanks Munish
Define this route:
routing.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional, lang = "en" }
);
All your flags at the top should be done like:
<a href="<%= Url.Action(this.ViewContext.RouteData.Values["action"], this.ViewContext.RouteData.Values["controller"], new { lang = "de" }) %>">
<img scr="..." />
</a>
so they will always point to the current page in a different language. This is useful if your language variations are 1:1. But if your language pages are different, you should always point to home page in a particular language.
Regarding translations you can either use resources or you can store them in the DB. I used both in my past although lately I save static interface string in resource files. Not just one, because it become way too overwhelming, but rather one resource file per file if it needs it. This way I do get some repeated definitions (like OK, Cancel etc), but its much easier to maintain it this way. Because if you have only one resource file it become very unpredictable where do things change.
So all my *.cs
files that need strings (ie. exception messages) have their *.resx
companion. The same goes with my views. Views almost all have their own acompanying *.resx
that defines all static content.
If you have more than one language, you'd have to provide *.lang.resx
for every language you need.
精彩评论