using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;

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

			routes.Add(new Route(
				"{lang}/{controller}/{action}/{id}",
				new RouteValueDictionary(new {
					lang = "en-US",//默认为E文
					controller = "Account",
					action = "Logon",
					id = UrlParameter.Optional
				}),
				new MultiLangRouteHandler()//这个类主要是通过GetHttpHandler来取得当前Lang的值
			));

			routes.MapHttpRoute(
				name: "DefaultApi",
				routeTemplate: "api/{controller}/{id}",
				defaults: new { id = RouteParameter.Optional }
			);

			routes.MapRoute(
				name: "Default",
				url: "{controller}/{action}/{id}",
				defaults: new { controller = "Account", action = "Logon", id = UrlParameter.Optional }
			);
		}
	}

	public class MultiLangRouteHandler : MvcRouteHandler {
		protected override IHttpHandler GetHttpHandler(RequestContext requestContext) {
			string lang = requestContext.RouteData.Values["lang"].ToString();

			//Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
			Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);

			//return new MvcHandler(requestContext);
			return base.GetHttpHandler(requestContext);
		}
	}
}