开发者

.NET MVC Localization (resourceprovider) implementation not working correctly

i'm currently implementing a localized website. I've created a custom ResourceProvider + Factory for storing resources in a database. This all works, i'm storing data as follows:

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

I've stored a Global (Test) and a Local (Products) resource in the above sample if i'm correct?

I'm using this implementation of ResourceHelpers (link) (code below). This should allow me to display resources based on the following code:

Global resources: This works (output: "Hi")

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

Local resources: Here is the problem, this doesn't work, what's going on??

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

The first code sample outputs "Hi" as expected. However the second code sample throws an exception (The resource object with key 'Products' was not found.) (i've marked the line in code below ~line 17). yes, i'm calling the second line on "~/Views/Products/Index.aspx"

Here's the Helper code i'm using:

<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.Get开发者_开发知识库FileNameWithoutExtension((DirectCast(view, WebFormView)).ViewPath)
    End If
    Return viewName
End Function


Turns out the resources weren't being saved automatically during the request..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜