本地化的JavaScript消息和有效性文本 [英] Localize javascript messages and validation text

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

问题描述

我工作的一个Web项目是多国语言。例如,该项目的一个部分涉及到一些,利用客户端interace使用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. 使用一个code渲染块在JavaScript本地化的消息中拉从一个资源文件

  1. Use a code render block in the javascript to pull in the localized message from a resource file

使用隐藏域与元:获取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.

请一个Web服务调用通过按键/语言每次需要一个消息的时间获得正确的信息。

Make a webservice call to get the correct message by key/language each time a message is required.

有什么想法,经验?

编辑:

我preFER使用.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

在这里,唯一的缺点是,如果你想要的本地化分配给一个全局变量,你必须运行它异步=假的,否则你不会有可用的翻译,当你需要他们。我试图用'得到',所以我可以缓存响应,但它不是为我工作。看到这个问题:

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:

<一个href=\"http://stackoverflow.com/questions/1033305/cant-return-dictionaryof-string-string-via-get-ajax-web-request-works-with-p\">http://stackoverflow.com/questions/1033305/cant-return-dictionaryof-string-string-via-get-ajax-web-request-works-with-p

这篇关于本地化的JavaScript消息和有效性文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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