本地化 javascript 消息和验证文本 [英] Localize javascript messages and validation text

查看:23
本文介绍了本地化 javascript 消息和验证文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个多语言的网络项目.例如,该项目的一部分涉及一些自定义的谷歌地图,该地图利用客户端接口使用 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:

  1. 在 javascript 中使用代码渲染块从资源文件中提取本地化消息

  1. 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 消息和验证文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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