开发者

building a website supporting many languages/cultures

开发者 https://www.devze.com 2023-01-16 15:09 出处:网络
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.

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 b开发者_StackOverflow中文版e 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?


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
     );
}
0

精彩评论

暂无评论...
验证码 换一张
取 消