views:

304

answers:

2

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?

+2  A: 
Eduardo Molteni
+1  A: 

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

Then in your .master...

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