在ASP.NET MVC应用程序的母版页本地化字符串 [英] Localizing strings in master pages of ASP.NET MVC application

查看:116
本文介绍了在ASP.NET MVC应用程序的母版页本地化字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有<一个href=\"http://stackoverflow.com/questions/1147028/problem-in-accessing-localized-string-in-asp-net-mvc-application\">managed本地化的网页在我的应用程序的观点,但也有它含有某些字符串母版页。

I have managed to localize the view pages in my application but there are master pages which contain some strings.

似乎包含在主页串在每一页的资源文件被添加。这似乎太可怕了。我怎样才能优雅本地化母版页中的字符串?

It appears that the string contained in master pages has to be added in resource file of each page. this seems horrible. How can I elegantly localize the strings in master pages?

推荐答案

如果你不想与访问修饰符烂摊子,你可以做一个帮手来简化code你必须写才能访问资源文件,是这样的:

If you don't want to mess with the access modifier, you could make a helper to simplify the code you have to write in order to access the resource file, something like:

public static class LocalizationHelper
{
    public static string Localize(this HtmlHelper helper, string key)
    {
        var resourceObject = helper.ViewContext.HttpContext.GetGlobalResourceObject("NameOfResourceFileClass", key);
        if (resourceObject == null)
        {
            // i don't recommend throwing the Exception class, I'd define my own Exception type here
            throw new Exception(String.Format("Resource key '{1}' could not be found in Resource class '{0}'","NameOfResourceFileClass", key));
        }

        return resourceObject.ToString();
    }
}

然后在你的.master ...

Then in your .master...

<%= Html.Localize("NameOfResourceKey") %>

这篇关于在ASP.NET MVC应用程序的母版页本地化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆