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