asp.net-mvc-3 - ASP.NET MVC 3 本地化 - Url.Content

标签 asp.net-mvc-3 localization

我有一个 ASP.NET MVC 3 站点,使用自定义路由来指定 URL 中的语言,如下所示:

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

然后,我的基本 Controller 在其 OnActionExecuting 事件中适当设置当前线程区域性,如下所示:

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var culture = filterContext.RouteData.Values["culture"] ?? "en";
    var cultureInfo = CultureInfo.GetCultureInfo((string)culture);
    Thread.CurrentThread.CurrentCulture = cultureInfo;
    Thread.CurrentThread.CurrentUICulture = cultureInfo;
    base.OnActionExecuting(filterContext);
}

只要我认为需要特定于区域设置的字符串,我都会适本地引用资源:

<img src="@Url.Content(Resources.Paths.ChallengeLogo)" alt="@Resources.Strings.AltChallengeLogo" class="logo" />

但是,对于图像,URL (src="...") 将遵循约定“~/Content/Culture/Images/Whatever.jpg”,并且我不想手动维护资源文件对于每个图像位置。简而言之,我想做的是:

<img src="@Url.Content("Images/ChallengeLogo.png")" alt="@Resources.Strings.AltChallengeLogo" class="logo" />

在这种情况下,Url.Content 方法将应用路径约定并将 src 属性值转换为,例如:

/Content/en-US/Images/ChallengeLogo.png
/Content/en-GB/Images/ChallengeLogo.png
/Content/de/Images/ChallengeLogo.png
etc...

我不必专门使用 Url.Content 。我非常愿意为此目的创建一个扩展方法,但我不确定我是否采取了最好的方法。这里的标准方法是什么?

编辑:我最终创建了一个助手。帮助器还有一个额外的好处,可以检查当前区域性的路径是否确实存在,或者在必要时回退到父/默认区域性。代码如下:

private static CultureInfo _defaultCulture = CultureInfo.GetCultureInfo("en");

public static string Localized(this UrlHelper url, string pathTemplate)
{
    var server = url.RequestContext.HttpContext.Server;
    var culture = Thread.CurrentThread.CurrentUICulture;
    var localizedPath = string.Format(pathTemplate, culture.Name);
    var physicalPath = server.MapPath(localizedPath);

    while (!File.Exists(physicalPath) && culture.Parent != null)
    {
        culture = culture.Parent;
        localizedPath = string.Format(pathTemplate, culture.Name);
        physicalPath = server.MapPath(localizedPath);
    }

    if (!File.Exists(physicalPath))
        localizedPath = string.Format(pathTemplate, _defaultCulture.Name);

    return url.Content(localizedPath);
}

在我看来,我可以按如下方式使用这个助手:

<div class="toolbox">

    <a href="#" class="button register">
        <img src="@Url.Localized("~/Content/Localized/{0}/Images/Button_Register.png")" 
             alt="@Resources.Strings.AltRegisterButton" /></a>

    <a href="#" class="button login">
        <img src="@Url.Localized("~/Content/Localized/{0}/Images/Button_Login.png")" 
             alt="@Resources.Strings.AltLoginButton" /></a>

</div>

最佳答案

我想你的建议也是我第一个想到的。 您可以通过创建自己的 html 帮助程序来构造本地化 url,轻松解决此问题。请参阅http://develoq.net/2011/how-to-create-custom-html-helpers-for-asp-net-mvc-3-and-razor-view-engine/作为创建自定义 html 帮助器的示例。

关于asp.net-mvc-3 - ASP.NET MVC 3 本地化 - Url.Content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6049976/

相关文章:

android - 只改变一次语言

c# - 将 Shift+1 转换为 ! C# 中的符号

c# - 在 axml 中使用来自 pcl 的 resx 字符串?

asp.net-mvc - MVC 3 Ajax.beginform 提交 - 导致完全回发

javascript - js中的Url.RouteUrl

asp.net-mvc-3 - uploadify + asp.net-mvc3问题

asp.net-mvc - MVC2或MVC3 html.routelink可以包含&laquo和&raquo吗?

asp.net-mvc - MVC 3 验证隐藏输入上的 htmlAttributes

jsf - JSF国际化,什么时候使用message-bundle和resource-bundle?

c# - .NET 本地化不起作用