Click here to Skip to main content
15,894,017 members
Articles / Web Development / XHTML

Localization for Scripts (JavaScript) and Style Sheets in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.75/5 (15 votes)
15 Nov 2014CPOL4 min read 63.3K   352   27  
Often JavaScript content and/or styles in ASP.NET need to be localized too. This article shows how this can be leveraged by using an HttpHandler.
using System;
using System.Globalization;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestWeb
{
    public partial class _Default : Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Sets the <see cref="P:System.Web.UI.Page.Culture"/> and <see cref="P:System.Web.UI.Page.UICulture"/> for the current thread of the page.
        /// </summary>
        protected override void InitializeCulture()
        {
            // Retrieve culture information from session
            string culture = Convert.ToString(Session["Culture"]);

            // Check whether a culture is stored in the session
            if (!string.IsNullOrEmpty(culture))
            {
                Culture = culture;

                // Set culture to current thread
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
            }

            // Call base class
            base.InitializeCulture();
        }

        /// <summary>
        /// Handles the Click event of the RequestLanguageChange control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void RequestLanguageChange_Click(object sender, EventArgs e)
        {
            LinkButton senderLink = sender as LinkButton;

            if (senderLink != null)
            {
                // Store requested language as new culture in the session
                Session["Culture"] = senderLink.CommandArgument;

                // Reload last requested page with new culture
                Server.Transfer(Request.Path);
            }
        }
    }
}

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
Architect Swissworx
Australia Australia
MCAD, MCPD Web Developer 2.0, MCPD Enterprise Developer 3.5

My company: Swissworx
My blog: Sitecore Experts

Hopp Schwiiz Smile | :)

Comments and Discussions