.NET MVC本地化(resourceprovider)实施工作不正常 [英] .NET MVC Localization (resourceprovider) implementation not working correctly

查看:193
本文介绍了.NET MVC本地化(resourceprovider)实施工作不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在实施一个本地化的网站。我创建了一个自定义的ResourceProvider +工厂为存储在数据库中的资源。这一切工作,我存储的数据如下:

  RESOURCEID | URL(空)|型(空)|名称|文化(空)|值
     1 | NULL |通用|测试| NULL |你好
     2 | 〜/查看/产品/的Index.aspx | NULL |产品| NULL |制品

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

我用这个实现ResourceHelpers的(的链接)(code以下)。这应该让我来显示基于以下code资源:

环球资源:该作品(输出:你好)

 <%= Html.Resource(通用,测试)%GT;

本地资源:这里的问题是,这是不行的,这是怎么回事?

 <%= Html.Resource(产品)%GT;

如预期的第一个code样本输出嗨。然而,第二个code样品抛出异常( 与资源对象键产品没有被发现。)(我标志着$ C $行下面C〜17行)。
后,我打电话给在第二行〜/查看/产品/ Index.aspx的

这里的助手code我使用的:

 < System.Runtime.CompilerServices.Extension()> _
公共职能资源(BYVAL作为的HtmlHelper的HtmlHelper,BYVAL前pression作为字符串,BYVAL的ParamArray ARGS作为对象())作为字符串
    昏暗virtualPath作为字符串= GetVirtualPath(的HtmlHelper)
    返回GetResourceString(htmlhelper.ViewContext.HttpContext,前pression,virtualPath,参数)
结束功能&所述; System.Runtime.CompilerServices.Extension()> _
公共职能资源(BYVAL控制器作为控制器,BYVAL前pression作为字符串,BYVAL的ParamArray ARGS作为对象())作为字符串
    返回GetResourceString(controller.HttpContext,前pression,〜/,参数)
结束功能专用功能GetResourceString(BYVAL的HttpContext作为HttpContextBase,BYVAL前pression作为字符串,BYVAL virtualPath作为字符串,BYVAL ARGS作为对象())作为字符串
    昏暗的环境作为新的前pressionBuilderContext(virtualPath)
    昏暗的建设者作为新ResourceEx pressionBuilder()    下面的代码行抛出异常
    昏暗的领域ResourceEx pressionFields = DirectCast(builder.ParseEx pression(如pression,的GetType(字符串),上下文),ResourceEx pressionFields)    如果不String.IsNullOrEmpty(fields.ClassKey)然后
        返回的String.Format(DirectCast(httpContext.GetGlobalResourceObject(fields.ClassKey,fields.ResourceKey,CultureInfo.CurrentUICulture),字符串),参数)
    万一    返回的String.Format(DirectCast(httpContext.GetLocalResourceObject(virtualPath,fields.ResourceKey,CultureInfo.CurrentUICulture),字符串),参数)
结束功能专用功能GetVirtualPath(BYVAL作为的HtmlHelper的HtmlHelper)作为字符串
    昏暗virtualPath作为字符串=没有
    昏暗的控制器为controller = TryCast(htmlhelper.ViewContext.Controller,控制器)    如果控制器状态并没有任何再
        昏暗的结果作为ViewEngineResult = FindView(controller.ControllerContext,htmlhelper.ViewContext.ViewName)
        昏暗的结果作为ViewEngineResult = FindView(controller.ControllerContext,GetViewName(htmlhelper.ViewContext.View))
        昏暗webFormView作为WebFormView = TryCast(result.View,WebFormView)        如果webFormView状态并没有任何再
            virtualPath = webFormView.ViewPath
        万一
    万一    返回virtualPath
结束功能专用功能FindView(BYVAL controllerContext由于ControllerContext,BYVAL的viewName作为字符串)作为ViewEngineResult
    结果
    昏暗的结果作为ViewEngineResult =无    仅搜索WebFormViewEngine
    昏暗webFormViewEngine作为WebFormViewEngine =无
    对于每个视图引擎在ViewEngines.Engines
        webFormViewEngine = TryCast(视图引擎,WebFormViewEngine)        如果webFormViewEngine状态并没有任何再
            对于出口
        万一
    下一个    结果= webFormViewEngine.FindView(controllerContext,的viewName,,假)
    如果result.View状态并没有任何再
        结果= webFormViewEngine.FindPartialView(controllerContext,的viewName,假)
    万一    '返回
    返回结果
结束功能专用功能GetViewName(BYVAL视图作为IVIEW)作为字符串
    昏暗的viewName作为字符串=没有
    如果TypeOf运算的观点是WebFormView然后
        的viewName = Path.GetFileNameWithoutExtension((DirectCast(查看,WebFormView))。ViewPath)
    万一
    返回的viewName
结束功能


解决方案

原来的资源没有被请求时自动保存..

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.GetFileNameWithoutExtension((DirectCast(view, WebFormView)).ViewPath)
    End If
    Return viewName
End Function

解决方案

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

这篇关于.NET MVC本地化(resourceprovider)实施工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆