Created
April 1, 2009 23:03
-
-
Save EricSch/88936 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class LocalizationHelpers | |
{ | |
public static string Resource(this HtmlHelper htmlhelper, string expression, params object[] args) | |
{ | |
string virtualPath = GetVirtualPath(htmlhelper); | |
return GetResourceString(htmlhelper.ViewContext.HttpContext, expression, virtualPath, args); | |
} | |
public static string Resource(this Controller controller, string expression, params object[] args) | |
{ | |
return GetResourceString(controller.HttpContext, expression, "~/", args); | |
} | |
private static string GetResourceString(HttpContextBase httpContext, string expression, string virtualPath, object[] args) | |
{ | |
var context = new ExpressionBuilderContext(virtualPath); | |
var builder = new ResourceExpressionBuilder(); | |
var fields = (ResourceExpressionFields)builder.ParseExpression(expression, typeof(string), context); | |
if (!string.IsNullOrEmpty(fields.ClassKey)) | |
return string.Format((string)httpContext.GetGlobalResourceObject(fields.ClassKey, fields.ResourceKey, CultureInfo.CurrentUICulture), args); | |
return string.Format((string)httpContext.GetLocalResourceObject(virtualPath, fields.ResourceKey, CultureInfo.CurrentUICulture), args); | |
} | |
private static string GetVirtualPath(HtmlHelper htmlHelper) | |
{ | |
string virtualPath = null; | |
var view = htmlHelper.ViewContext.View as WebFormView; | |
if (view != null) | |
{ | |
virtualPath = view.ViewPath; | |
} | |
return virtualPath; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment