Use ASP.NET Resource strings from within javascript files

后端 未结 9 1702
迷失自我
迷失自我 2020-12-13 01:02

How would one get resx resource strings into javascript code stored in a .js file?

If your javascript is in a script block in the markup, you can use this syntax:

相关标签:
9条回答
  • 2020-12-13 01:11

    whereas "Common" is the name of the resource file and Msg1 is the fieldname. This also works for culture changes.

                Partial Javascript...:
                messages: 
                {
                    <%=txtRequiredField.UniqueID %>:{                       
                        required: "<%=Resources.Common.Msg1 %>",
                        maxlength: "Only 50 character allowed in required field."
                    }
                }
    
    0 讨论(0)
  • 2020-12-13 01:17

    If you have your resources in a separate assembly you can use the ResourceSet instead of the filename. Building on @Domenics great answer:

    public class ResourcesController : Controller
    {
        private static readonly JavaScriptSerializer Serializer = new JavaScriptSerializer();
    
        public ActionResult GetResourcesJavaScript()
        {
            // This avoids the file path dependency.
            ResourceSet resourceSet = MyResource.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
    
            // Create dictionary.
            var resourceDictionary = resourceSet
                            .Cast<DictionaryEntry>()
                            .ToDictionary(entry => entry.Key.ToString(), entry => entry.Value.ToString());
            var json = Serializer.Serialize(resourceDictionary);
            var javaScript = string.Format("window.Resources = window.Resources || {{}}; window.Resources.resources = {1};", json);
    
            return JavaScript(javaScript);
        }
    }
    

    The downside is that this will not enable more than one resource-file per action. In that way @Domenics answer is more generic and reusable.

    You may also consider using OutputCache, since the resource won't change a lot between requests.

    [OutputCache(Duration = 3600, Location = OutputCacheLocation.ServerAndClient)]
    public ActionResult GetResourcesJavaScript()
    { 
        // Logic here...
    }
    

    http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/improving-performance-with-output-caching-cs

    0 讨论(0)
  • 2020-12-13 01:20

    There's no native support for this.

    I built a JavaScriptResourceHandler a while ago that can serve Serverside resources into the client page via objects where each property on the object represents a localization resource id and its value. You can check this out and download it from this blog post:

    http://www.west-wind.com/Weblog/posts/698097.aspx

    I've been using this extensively in a number of apps and it works well. The main win on this is that you can localize your resources in one place (Resx or in my case a custom ResourceProvider using a database) rather than having to have multiple localization schemes.

    0 讨论(0)
  • 2020-12-13 01:22

    I the brown field application I'm working on we have an xslt that transforms the resx file into a javascript file as part of the build process. This works well since this is a web application. I'm not sure if the original question is a web application.

    0 讨论(0)
  • 2020-12-13 01:24

    I usually pass the resource string as a parameter to whatever javascript function I'm calling, that way I can continue to use the expression syntax in the HTML.

    0 讨论(0)
  • 2020-12-13 01:26

    In a nutshell, make ASP.NET serve javascript rather than HTML for a specific page. Cleanest if done as a custom IHttpHandler, but in a pinch a page will do, just remember to:

    1) Clear out all the ASP.NET stuff and make it look like a JS file.

    2) Set the content-type to "text/javascript" in the codebehind.

    Once you have a script like this setup, you can then create a client-side copy of your resources that other client-side scripts can reference from your app.

    0 讨论(0)
提交回复
热议问题