問題描述
我正在開發一個多語言的網絡項目.例如,該項目的一部分涉及一些自定義的谷歌地圖,該地圖利用客戶端接口使用 jquery/.net 將點添加到地圖并將它們保存到數據庫中.
I'm working on a web project that is multilingual. For example, one portion of the project involves some custom google mapping that utilizes a client-side interace using jquery/.net to add points to a map and save them to the database.
會有一些驗證和其他信息性消息(例如請在地圖上至少添加一個點")必須本地化.
There will be some validation and other informational messaging (ex. 'Please add at least one point to the map') that will have to be localized.
我現在能想到的唯一選擇是:
The only options I can think of right now are:
在 javascript 中使用代碼渲染塊從資源文件中提取本地化消息
Use a code render block in the javascript to pull in the localized message from a resource file
使用帶有 meta:resourcekey 的隱藏字段,以使用當前文化自動從資源文件中獲取正確的本地化消息,并在必要時在 jquery 中獲取 .val().
Use hidden fields with meta:resourcekey to automatically grab the proper localized message from the resource file using the current culture, and get the .val() in jquery when necessary.
每次需要消息時,調用網絡服務以按鍵/語言獲取正確的消息.
Make a webservice call to get the correct message by key/language each time a message is required.
有什么想法、經驗嗎?
我更喜歡使用 .net 資源文件來與應用程序的其余部分保持一致.
I'd prefer to use the .net resource files to keep things consistent with the rest of the application.
推薦答案
好的,我構建了一個通用的 Web 服務來允許我獲取資源并將它們返回到字典中(可能是轉換為字典的更好方法)...
Ok, I built a generic web service to allow me to grab resources and return them in a dictionary (probably a better way to convert to the dictionary)...
<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 結果并將其分配給一個局部變量:
Then, I can grab this json result and assign it to a local variable:
// 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;
}
});
然后,您可以像這樣從局部變量中獲取您的值:
Then, you can grab your value from the local variable like so:
localizations.Key1
localizations.Key1
這里唯一的問題是,如果要將本地化分配給全局變量,則必須運行它 async=false,否則在需要時將無法獲得翻譯.我正在嘗試使用獲取",以便可以緩存響應,但它對我不起作用.看到這個問題:
The only catch here is that if you want to assign the localizations to a global variable you have to run it async=false, otherwise you won't have the translations available when you need them. I'm trying to use 'get' so I can cache the response, but it's not working for me. See this question:
不能通過 GET ajax Web 請求返回 Dictionary(Of String, String),適用于 POST
這篇關于本地化 javascript 消息和驗證文本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!