views:

42

answers:

2

I'm hoping this is a rather simple question, but I'm pretty new to MVC and can't see clearly how it should be done. I have a site that I need to translate to another language. I've tried to search on this, but all I found was pretty complex translations about how to handle strings etc in resx files. That may be something for later, but for now all I want is to be able to let the user switch language (by links I can place in the master page), and then based on that choice have different pages shown in different languages.

From my search it seemed this could be achieved by routing somehow. As suggested in another post:

routes.MapRoute(
"Default", 
"{language}/{controller}/{action}/{id}", 
new { language = "en", controller = "Home", action = "Index", id = "" }
);

And the master page switch links:

<li><%= Html.ActionLink(
    "Spanish", 
    ViewContext.RouteData.Values["action"].ToString(), 
    new { language = "es" })%></li>
<li><%= Html.ActionLink(
    "French", 
    ViewContext.RouteData.Values["action"].ToString(), 
    new { language = "fr" })%></li>
<li><%= Html.ActionLink(
    "English", 
    ViewContext.RouteData.Values["action"].ToString(), 
    new { language = "en" })%></li>

I could try this, but what I don't understand is, what type of routes does this create? Is it "language/controllername/actionname"? And if so, where does it lead? I mean, usually, with just a controller and an action, all I have is one controller and one view, and as long as that view exists it will work. But what is the language in this? Is it just as a folder, so if I have a folder say en-GB/Home such a route would work? That doesn't make sense, so I guess not. So how do I actually make these routes lead somewhere? Where do I place the translated views?

+2  A: 

I think using resource files instead will be easier in the long run and not that hard to get going with.

Check out this link for more information.

Here's a quick how to on it.

Here's some gotchas to using resources in .Net MVC with solutions.

klabranche
Ok, but as far as I have been able to understand, they are about translating strings for things like logon text fields and so on. What I want is to simply translate the pages (views) themselves, that is simple body text in the main informative pages, not in pages using the more advanced dynamic features of the mvc application.
Anders Svensson
I would still make those resources and load them in a label for example. I think this will ease your long term maintainability of the app
klabranche
klabranche is right ,,, the last thing you want to do is having different web pages for each language. I've worked on the interface of a Multi-Lang Website. the website had two interfaces Arabic (RTL) and English (LTR) and we used Resource files and Themes and everything was working just great.
Manaf Abu.Rous
Yes, I think you are all right, and eglasius too points me to this as an alternative, but for now this is a very simple requirement I have for a rather simple site. The site has very few international customers, so all it needs are some basic pages in English (main language being Swedish). I would surely set it as a long term goal to get acquainted with this technique, but for now I need a simple way to quickly fix the basic requirement. The English pages won't need to be perfectly synced, they just need to be able to supply some basic information.
Anders Svensson
+1  A: 

re the url, it is like you said / like it reads - language/controllername/actionname

re what it calls - what you need to focus on to understand it is in this bit of the route definition:

new { language = "en", controller = "Home", action = "Index", id = "" }

{controller}/{action}, matches the corresponding controller and action like before. Language and id matches those parameters in the action method you define. Those could also be properties of the (view)model, if that's the parameter you have in the method.

I don't think there is anything automatically hooked for the languages in mvc, so you have to explicitly decide how you want to handle it. One way would be for your action methods to return a view in a subfolder for each language or by adding the language as part of the file name.

Another way to go about it, is to define a handler in the route that sets the thread ui culture as you would in classic asp.net. From then on you use the asp.net mvc resources like in klabranche links.

eglasius
Ok, thanks. So the language = "en" is a parameter I can use in the regular default Home/Index action method? And then reroute based on that parameter? But since I don't understand the routing of mvc very well yet, how do I do that? I only know I can reroutetoaction to another controller and action or an action method in the same controller. How do I reroute to the corresponding Index action method in a subfolder like, say, (folder path:) "Home/Views/Swedish"? What would the normal RedirectToAction("Index", "Home"); become to get to "Home/Swedish/Index"? Just that? Does that really work?
Anders Svensson
@Anders this really depends on how you want to achieve it. If you only want a different view, instead of return View() do return View("Index" + language). Alternatively there might be a more automatic way to do so with an ActionFilter i.e. instead of explicitly doing the above in your methods, the action filter could change the view used based on the language contained in the route values in the context.
eglasius
Note that RedirectToAction is like a Redirect(someUrl), so if you use that you'd be telling the browser to go to a different url for the page / but that's already the case, you are in an url for the corresponding language. In my comment above I assume you meant how to display a different view. It'll get you Home/Views/IndexSwedish.aspx. Honestly I'm unsure if you could also do it with a subfolder like return View(language + "/Index") to grab Home/Views/Swedish/Index.aspx
eglasius
Ok, yes sorry, that's what I meant. But what I really wanted to know was how to return to a specific folder...
Anders Svensson
return View(language + "/Index") like in my last comment should work, just did a quick search and got this: http://dotnetache.blogspot.com/2010/08/organize-aspnet-mvc-views-in.html
eglasius
Perfect! Again, I will probably have to deal with the more proper way of handling everything with resource files later, but this did exactly what I wanted as a quick fix right now! Thanks!
Anders Svensson