2010-09-06 28 views
2

我想在我的JavaScript代码中使用全局资源。通常情况下,当代码在ASP代码中时,我使用 <%=GetGlobalResourceObject("Resource", "MONTHS_SHORT1") %> 并且它可以工作。但是,当JavaScript代码不在ASP代码中时,例如在calendar.js文件夹中,它不起作用。在这种情况下如何使用全局资源?注意:资源文件位于我的App_GlobalResources文件夹内。在JavaScript中使用全局资源

回答

2

看看下面的文章,我创建的描述了如何连载全球和本地资源和使用jQuery和JSON的JavaScript检索。

http://bloggingdotnet.blogspot.com/2010_02_01_archive.html

首先,创建一个新的处理程序(ashx的)文件。这是一个相当长的一段时间前写的,所以使用vb.net和自定义JSON序列:

Imports System.Web 
Imports System.Web.Services 
Imports System.Xml 
Imports System.Resources 
Imports System.Reflection 

Public Class Localisation 
    Implements System.Web.IHttpHandler 

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest 
    Dim files As String = context.Request.QueryString("files") 
    Dim local As String = context.Request.QueryString("local") 
    Dim isLocal As Boolean 
    Dim folder As String = "App_GlobalResources" 

    context.Response.ContentType = "text/javascript" 

    'Write out file as object 
    context.Response.Write("{") 

    'Determine if local resource file 
    If local IsNot Nothing Then 
     isLocal = CBool(local) 
     If isLocal Then folder = "App_LocalResources" 
    End If 
    If files Is Nothing OrElse files.Length = 0 Then Throw New ArgumentException("Parameter 'files' was not provided in querystring.") 

    Dim flag As Boolean = False 
    For Each file As String In files.Split(",") 
     If flag Then context.Response.Write(",") 

     Dim className As String = file.Split(".")(0) 

     'Write the class (name of the without any extensions) as the object 
     context.Response.Write(className) 
     context.Response.Write(":{") 

     'Open the resx xml file 
     Dim filePath As String = context.Server.MapPath("~\" & folder & "\" & file) 
     Dim document As New XmlDocument() 
     Dim flag2 As Boolean = False 
     document.Load(filePath) 

     Dim nodes As XmlNodeList = document.SelectNodes("//data") 

     For Each node As XmlNode In nodes 

     'Write out the comma seperator 
     If flag2 Then context.Response.Write(",") 

     Dim attr As XmlAttribute = node.Attributes("name") 
     Dim resourceKey As String = attr.Value 
     context.Response.Write(resourceKey) 
     context.Response.Write(":""") 

     'Write either the local or global value 
     If isLocal Then 
     context.Response.Write(HttpContext.GetLocalResourceObject(String.Format("~/{0}", file.Replace(".resx", "")), resourceKey)) 'Has to be full path to the .aspx page 
     Else 
      context.Response.Write(HttpContext.GetGlobalResourceObject(className, resourceKey)) 
     End If 
     context.Response.Write("""") 

     'Flag that we need a comma seperator 
     flag2 = True 
     Next 

     context.Response.Write("}") 
     flag = True 
    Next 

    'End file 
    context.Response.Write("}") 
    End Sub 

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable 
    Get 
     Return True 
    End Get 
    End Property 
End Class 

当正在工作,请使用以下的jQuery代码发出AJAX调用HTTP处理程序和返回的内容资源文件作为对象文字。

// -- Localisation -- 
var localResources; 
var globalResources; 

//Sample JSON for javascript resource values eg {TrackDetail:{HideHelp:"Hide Help", ShowHelp:"Show Help"}} 
//Usage e.g: alert(localResources.TrackDetail.HideHelp); 

//Load Localisation values into variables so that they can be available on the client 
//Note that these values are loaded asynchronously, the code in the function will not run until the call has completed. 
$.getJSON('Localisation.ashx?files=TrackDetail.aspx.resx&local=true', function(data) { localResources = data}); 
$.getJSON('Localisation.ashx?files=Errors.resx,Strings.resx', function(data) { globalResources = data}); 
1

您必须生成模拟您所需资源的JavaScript代码,因为ASP.NET运行在服务器端,而JavaScript运行在客户端。

<script language="javascript" type="text/javascript"> 
var MONTHS_SHORT1 = <%=GetGlobalResourceObject("Resource", "MONTHS_SHORT1") %>; 
</script> 
1

我经常使用一个隐藏的文本保存资源文本,然后抢使用JavaScript和jQuery的本地化的文本:

<asp:Literal runat="server" ID="Literal1" visible="false" Text="<%$ Resources:String, MyResourceText%>" /> 
    <input type="button" id="Button1" value="" /> 
    <script type="text/javascript"> 
      $(document).ready(function() { 
       // Update the buton text with the correct localised lookup 
       var ButtonText = '<%=Literal1.Text%>'; 
       $("#Button1").attr('value', ButtonText); 
      }); 
    </script> 
0

这将注册资源键。

ClientScriptManager cs = Page.ClientScript; 

String scriptRegVariables = string.Format("var resourcetext = '{0}'", Resources.Resource.keyText); 
if (!cs.IsClientScriptBlockRegistered("RegVariablesScript")) 
{ 
    cs.RegisterClientScriptBlock(typeof(_Default), "RegVariablesScript", scriptRegVariables, true); 
} 

现在在.js文件里面可以直接使用它。 例如警报(resourcetext);

将上面的代码添加到Page_Load的控件或aspx页面