Skip to content

Instantly share code, notes, and snippets.

@EricSch
Created April 1, 2009 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EricSch/88936 to your computer and use it in GitHub Desktop.
Save EricSch/88936 to your computer and use it in GitHub Desktop.
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