views:

69

answers:

1

I am building my first ASP.NET MVC site and this site needs to be in 2 languages.

When a user selects a language from the menu, the site would present itself in that language.

The content likely will be loaded all from resource files. Can I respond to a change in the culture definition at runtime so it load the correct resource file?

I want different URLs for both languages. Perhaps something like mysite.com/en/home/index.

Is this possible? The users should be able to forward and save links and that the site will be loaded in their language.

How can this be done with ASP.NET MVC?

+2  A: 

I switch the culture in the global.asax page as follows

protected void Application_AcquireRequestState(object sender, EventArgs e)
{
    CultureInfo culture = Settings.UserCultureFromSession();
    System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
    System.Threading.Thread.CurrentThread.CurrentCulture = culture;
}

The static UserCultureFromSession method pulls out a cached culture for the current user. Of course you need to set this first.

Additionally, that method needs to ensure that the Session is available as the first time the site is called (eg after a reset) the session hasn't been built, so you'll need a default language returned in this instance.

It's pretty simple after that. Using the above means that every control and every page will pick up the right culture for every request.

You may wish to have a standard culture for CurrentCulture if you're using culture dependant code in the back end such as date parsing. However you should really have written your code, date or otherwise, to be culture independant.

Update 1 In terms of book marking the site you can have the method above look in the URL to see if a language code exists. The scheme you choose to do this is really up to you. You could use country domains '.co.uk, .co.nz, .co.jp' and infer the culture from these or you could culture identifier folders at the base of your site such as

www.example.com/en-NZ
www.example.com/en-GB
www.example.com/ja-JP

Additionally, you could set up subdomains. There really are a multitude of ways of deciding how this could be done.

Update 2 Well the same way you pick up the routing variables within the mvc route handlers you can just set one for the culture. If you decide to do it in the last example then something like this is where I'd start:

public static void RegisterRoutes(RouteCollection routes)
{
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

     routes.MapRoute(
         "Default",                                              // Route name
         "{culture}/{controller}/{action}/{id}",                           // URL with parameters
         new { culture = "en-NZ", controller = "Home", action = "Index", id = "" }  // Parameter defaults
     );
}
toxaq
i thnks for the answear + 1. when will the Application_AcquireRequestState get triggered? now that i know how to set the culture which is good i need someone to answear me about the second part of the question which is how i direct urls with language shortcut like en inside the url in mvc..
guy schaller
Before the allocated handler gets executed. This means it gets in before the default page handler or any other handlers than are registered get executed. See http://msdn.microsoft.com/en-us/library/system.web.httpapplication.aspx for the order.
toxaq