【发布时间】:2010-10-14 17:22:01
【问题描述】:
我正在与一个希望我们的 Web 应用程序中的 URL 使用法语的客户合作。我是一名英语开发人员,我们也有英语客户。这是一个有趣的问题,但我认为 ASP.NET MVC 框架不支持它。
这是场景。路线……
具体示例
英文网址
www.stackoverflow.com/questions/ask
也会支持
法文网址
www.stackoverflow.com/problème/poser
一般示例
英文网址
http://clientA.product.com/AreaNameEnglish/ControllerNameEnglish/ActionNameEnglish/params
也需要支持
法文网址
http://clientB.product.com/AreaNameFrench/ControllerNameFrench/ActionNameFrench/params
所以在 MVC 中,我的 Area、Controller 和 Actions 都需要有英文和法文的翻译。
如果我将所有控制器、视图和操作名称硬编码为法语,显然可维护性将是一个巨大的问题。有没有办法在不这样做的情况下本地化浏览器中显示的路线?请记住,应用程序中有许多不同的路线。几个区域,每个区域都有几个控制器,每个区域都有很多动作?
谢谢,
贾斯汀
编辑
感谢@womp,这就是我迄今为止想出的......虽然最后我采用了我发布的方法作为答案。
public class LocalizedControllerFactory : DefaultControllerFactory
{
public override IController CreateController(RequestContext requestContext, string controllerName)
{
if (string.IsNullOrEmpty(controllerName))
throw new ArgumentNullException("controllerName");
if (CultureInfo.CurrentCulture.TwoLetterISOLanguageName == "fr")
{
controllerName = this.ReplaceControllerName(requestContext, controllerName);
this.ReplaceActionName(requestContext);
this.ReplaceAreaName(requestContext);
}
return base.CreateController(requestContext, controllerName);
}
private string ReplaceControllerName(RequestContext requestContext, string controllerName)
{
// would use the language above to pick the propery controllerMapper. For now just have french
Dictionary<string, string> controllerMapper = new Dictionary<string, string>()
{
{"frenchControllerA", "englishControllerA"},
{"frenchControllerB", "englishControllerB"}
};
return this.ReplaceRouteValue(requestContext, "controller", controllerMapper);
}
private void ReplaceAreaName(RequestContext requestContext)
{
// would use the language above to pick the propery areaMapper. For now just have french
Dictionary<string, string> areaMapper = new Dictionary<string, string>()
{
{"frenchAreaX", "englishAreaX"},
{"frenchAreaY", "englishAreaY"}
};
this.ReplaceRouteValue(requestContext, "area", areaMapper);
}
private void ReplaceActionName(RequestContext requestContext)
{
// would use the language above to pick the propery actionMapper. For now just have french
Dictionary<string, string> actionMapper = new Dictionary<string, string>()
{
{"frenchAction1", "englishAction1"},
{"frenchAction2", "englishAction2"}
};
this.ReplaceRouteValue(requestContext, "action", actionMapper);
}
private string ReplaceRouteValue(RequestContext requestContext, string paramName, Dictionary<string, string> translationLookup)
{
if (requestContext.RouteData.Values[paramName] == null)
{
return null;
}
string srcRouteValue = requestContext.RouteData.Values[paramName] as string;
if (srcRouteValue != null && translationLookup.ContainsKey(srcRouteValue))
{
requestContext.RouteData.Values[paramName] = translationLookup[srcRouteValue];
}
return requestContext.RouteData.Values[paramName] as string;
}
}
一个不错的开始。如果我只本地化 URL 中的 ControllerName 和 ActionName,它将找到并呈现正确的视图。但是我有以下问题。
地区名称无法翻译
本地化区域意味着 Controller.View() 方法无法找到视图。
即使我已经在请求上下文中替换了区域名称,但 ViewEngineCollection.Find() 方法似乎并没有找到它。在我的 Controller 类中执行“return View()”的任何地方都无法找到其操作的默认视图。如果我不本地化该区域,那么其他步骤将起作用。
RedirectToAction 或 Html.ActionLink
任何时候应用程序调用 RedirectToAction 或者如果我使用 Html.ActionLink 帮助器或类似的 Urls 生成的是英文的。看起来我将不得不在多个位置的某个地方添加逻辑,以将英语 URL 转换为法语(或其他语言)。
【问题讨论】:
标签: asp.net-mvc url localization asp.net-mvc-routing