Click here to Skip to main content
15,884,473 members
Articles / Web Development / HTML

Resource File to JavaScript Object

Rate me:
Please Sign up or sign in to vote.
4.77/5 (14 votes)
23 Feb 2011CPOL1 min read 76.7K   751   25  
A web control that creates a JavaScript object representing a resx (resources) file and makes it available to be used from HTML and JavaScript code.
using System;
using System.IO;
using System.Web;

namespace ResourcesToJavaScript
{
    /// <summary>
    /// Represents a class that can be used to render a javascript object that contains resource keys and values
    /// of a specific global resX file dependant on the CurrentUI culture.
    /// </summary>
    public class GlobalResourcesToJavaScript : ResourcesToJavaScriptBase
    {
        /// <summary>
        /// The name of the Global ResX file (ex: "Resource1" if the ResX file is "Resource1.resx")
        /// </summary>
        public string GlobalResXFileName
        {
            get;
            set;
        }

        private string _javaScriptObjectName;
        /// <summary>
        /// Sets and Gets the generated JavaScript object name. if not set it will return the normalized GlobalResXFileName.
        /// </summary>
        public override string JavaScriptObjectName
        {
            set
            {
                _javaScriptObjectName = value;
            }
            get
            {
                if (!string.IsNullOrEmpty(_javaScriptObjectName) && _javaScriptObjectName.Trim() != string.Empty)
                {
                    return NormalizeVariableName(_javaScriptObjectName);
                }
                return NormalizeVariableName(GlobalResXFileName);
            }
        }

        protected override string GetResourceValue(string key)
        {
            string value = HttpContext.GetGlobalResourceObject(GlobalResXFileName, key) as string;
            return value == null ? string.Empty : value;
        }

        protected override string GetResXFilePath()
        {
            return Page.MapPath(Path.Combine("~//App_GlobalResources",  GlobalResXFileName + ".resx"));
        }

        protected override void ValidateBeforeRender(System.Web.UI.HtmlTextWriter writer)
        {
            if (string.IsNullOrEmpty(GlobalResXFileName) || GlobalResXFileName.Trim() == string.Empty)
            {
                writer.Write("GlobalResourcesToJavaScript: " + this.ClientID + ": Please specify GlobalResXFileName");
                return;
            }
            base.ValidateBeforeRender(writer);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions