.NET MVC本地化(resourceprovider)实现无法正常工作

时间:2010-01-11 19:53:03

标签: asp.net-mvc vb.net localization resources resourceproviderfactory

我目前正在实施本地化网站。我已经创建了一个自定义ResourceProvider + Factory来存储数据库中的资源。这一切都有效,我将数据存储如下:

resourceid | url (null)                  | type (null) | name     | culture (null) | value
     1     | NULL                        | Common      | Test     | NULL           | Hi
     2     | ~/Views/Products/Index.aspx | NULL        | Products | NULL           | Products

如果我是正确的,我在上面的示例中存储了全局(测试)和本地(产品)资源?

我正在使用ResourceHelpers(link)的这种实现(下面的代码)。这应该允许我根据以下代码显示资源:

全球资源:这有效(输出:“嗨”)

<% =Html.Resource("Common, Test") %>

本地资源:这是问题,这不起作用,发生了什么?

<% =Html.Resource("Products") %>

第一个代码示例按预期输出“Hi”。但是第二个代码示例抛出异常( 找不到关键字'Products'的资源对象。 )(我在下面的代码中标记了行〜第17行) 。 ,我在“〜/ Views / Products / Index.aspx”上打了第二行

以下是我正在使用的帮助程序代码:

<System.Runtime.CompilerServices.Extension()> _
Public Function Resource(ByVal htmlhelper As HtmlHelper, ByVal expression As String, ByVal ParamArray args As Object()) As String
    Dim virtualPath As String = GetVirtualPath(htmlhelper)
    Return GetResourceString(htmlhelper.ViewContext.HttpContext, expression, virtualPath, args)
End Function

<System.Runtime.CompilerServices.Extension()> _
Public Function Resource(ByVal controller As Controller, ByVal expression As String, ByVal ParamArray args As Object()) As String
    Return GetResourceString(controller.HttpContext, expression, "~/", args)
End Function

Private Function GetResourceString(ByVal httpContext As HttpContextBase, ByVal expression As String, ByVal virtualPath As String, ByVal args As Object()) As String
    Dim context As New ExpressionBuilderContext(virtualPath)
    Dim builder As New ResourceExpressionBuilder()

    'The following line throws the exception
    Dim fields As ResourceExpressionFields = DirectCast(builder.ParseExpression(expression, GetType(String), context), ResourceExpressionFields)

    If Not String.IsNullOrEmpty(fields.ClassKey) Then
        Return String.Format(DirectCast(httpContext.GetGlobalResourceObject(fields.ClassKey, fields.ResourceKey, CultureInfo.CurrentUICulture), String), args)
    End If

    Return String.Format(DirectCast(httpContext.GetLocalResourceObject(virtualPath, fields.ResourceKey, CultureInfo.CurrentUICulture), String), args)
End Function

Private Function GetVirtualPath(ByVal htmlhelper As HtmlHelper) As String
    Dim virtualPath As String = Nothing
    Dim controller As Controller = TryCast(htmlhelper.ViewContext.Controller, Controller)

    If controller IsNot Nothing Then
        'Dim result As ViewEngineResult = FindView(controller.ControllerContext, htmlhelper.ViewContext.ViewName)
        Dim result As ViewEngineResult = FindView(controller.ControllerContext, GetViewName(htmlhelper.ViewContext.View))
        Dim webFormView As WebFormView = TryCast(result.View, WebFormView)

        If webFormView IsNot Nothing Then
            virtualPath = webFormView.ViewPath
        End If
    End If

    Return virtualPath
End Function

Private Function FindView(ByVal controllerContext As ControllerContext, ByVal viewName As String) As ViewEngineResult
    ' Result
    Dim result As ViewEngineResult = Nothing

    ' Search only for WebFormViewEngine
    Dim webFormViewEngine As WebFormViewEngine = Nothing
    For Each viewEngine In ViewEngines.Engines
        webFormViewEngine = TryCast(viewEngine, WebFormViewEngine)

        If webFormViewEngine IsNot Nothing Then
            Exit For
        End If
    Next

    result = webFormViewEngine.FindView(controllerContext, viewName, "", False)
    If result.View IsNot Nothing Then
        result = webFormViewEngine.FindPartialView(controllerContext, viewName, False)
    End If

    ' Return
    Return result
End Function

Private Function GetViewName(ByVal view As IView) As String
    Dim viewName As String = Nothing
    If TypeOf view Is WebFormView Then
        viewName = Path.GetFileNameWithoutExtension((DirectCast(view, WebFormView)).ViewPath)
    End If
    Return viewName
End Function

1 个答案:

答案 0 :(得分:1)

结果在请求期间没有自动保存资源..