Hussain Rauf
Hussain Rauf

Reputation: 298

Link different CSS to multi-language website in ASP.Net?

I am creating multi-language (En and Ar) website in Asp.Net. I am wondering how can I link RTL CSS on switching culture from English to Arabic. I have successfully created the multi-language website just stuck in linking CSS. I know how to do in MVC using bundles, but not sure about a simple ASP.Net application. Following is my code:

public class BasePage : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        if (!string.IsNullOrEmpty(Request["lang"]))
        {
            Session["lang"] = Request["lang"];
        }
        string lang = Convert.ToString(Session["lang"]);
        string culture = string.Empty;

        if (lang.ToLower().CompareTo("en") == 0 || string.IsNullOrEmpty(culture))
        {
            culture = "en-US";
        }
        if (lang.ToLower().CompareTo("ar") == 0)
        {
            culture = "ar-SA";

        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

        base.InitializeCulture();
    }
}

Aspx Page:

<head runat="server">

    <link href="RTL.css" rel="stylesheet" />   
    <!-- AR, Use this CSS if culture is Arabic -->

    <link href="LTR.css" rel="stylesheet" />
    <!-- EN, Use this CSS if culture is English-->

</head>

<body>

<a href="?lang=en" runat="server" id="enLang">
<asp:Literal ID="Literal1" runat="server" Text="<%$Resources:myWeb.language, langEnglish%>" /></a>

<a href="?lang=ar" runat="server" id="arLang">
<asp:Literal ID="Literal2" runat="server" Text="<%$Resources:myWeb.language, langArabic%>" /></a>

</body>

Upvotes: 4

Views: 1710

Answers (2)

John Adam
John Adam

Reputation: 220

Try this:

<% if (System.Globalization.CultureInfo.CurrentCulture.DisplayName == "English (United States)")
   { %>
      <link href="LTR.css" rel="stylesheet" />
   <% }
   else
   { %>
      <link href="RTL.css" rel="stylesheet" />
   <% } %>

Upvotes: 3

PM.
PM.

Reputation: 1721

You can add css file conditionally using server side tags in your ASPX page. Something like:

 <% if(Session["lang"]=="ar") { %>
   <link href="RTL.css" rel="stylesheet" />   
   <!-- AR, Use this CSS if culture is Arabic -->
 <%} else {%>
    <link href="LTR.css" rel="stylesheet" />
    <!-- EN, Use this CSS if culture is English-->
 <%}%>

I am assuming you do not want to change it for each page, otherwise you can move this code to the page instead of head.

Upvotes: 0

Related Questions