好的,我构建了一个通用 Web 服务,让我可以抓取资源并将它们返回到字典中(可能是转换为字典的更好方法)...
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=False, XmlSerializeString:=True)> _
Public Function GetResources(ByVal resourceFileName As String, ByVal culture As String) As Dictionary(Of String, String)
Dim reader As New System.Resources.ResXResourceReader(String.Format(Server.MapPath("/App_GlobalResources/{0}.{1}.resx"), resourceFileName, culture))
If reader IsNot Nothing Then
Dim d As New Dictionary(Of String, String)
Dim enumerator As System.Collections.IDictionaryEnumerator = reader.GetEnumerator()
While enumerator.MoveNext
d.Add(enumerator.Key, enumerator.Value)
End While
Return d
End If
Return Nothing
End Function
然后,我可以获取这个 json 结果并将其分配给一个局部变量:
// load resources
$.ajax({
type: "POST",
url: "mapping.asmx/GetResources",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: '{"resourceFileName":"common","culture":"en-CA"}',
cache: true,
async: false,
success: function(data) {
localizations = data.d;
}
});
然后,您可以像这样从局部变量中获取您的值:
localizations.Key1
这里唯一的问题是,如果您想将本地化分配给一个全局变量,您必须运行它 async=false,否则您将无法在需要时获得翻译。我正在尝试使用“获取”,以便可以缓存响应,但它对我不起作用。看到这个问题:
Can't return Dictionary(Of String, String) via GET ajax web request, works with POST