Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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

, 19 May 2009 CPOL
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)

Share

About the Author

Michael Ulmann
Architect Helvetic Solutions
Australia Australia
MCAD, MCPD Web Developer 2.0, MCPD Enterprise Developer 3.5
My company: www.helveticsolutions.com
Hopp Schwiiz Smile | :)

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 19 May 2009
Article Copyright 2009 by Michael Ulmann
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid