locked
Change culture based on domain name extension RRS feed

  • Question

  • User1673332766 posted
    I'm just getting started with the Small Business starter kit. I have added multi-language support by creating resource files for two languages. Since we the domains for both of these languages (.com and .se), we would like to find out if it's possible to change the language (culture) by detecting if the user is in the .com or the .se domain name?
     
    We don't anticipate other languages, and if the above is possible, we would also have links to both domains in the master page somewhere, such that users could still swith languages this way (i.e. the links would just point to either .com or .se and the culture will be changed accordingly).
    Friday, April 17, 2009 9:39 AM

Answers

  • User1282917655 posted

     place the void InitializeCulture inside the page class.

     

    using System.Threading;
    using System.Globalization;
    
    public partial class Default3 : System.Web.UI.Page
    {
        protected override void InitializeCulture()
        {
    
            System.Uri url = Request.Url;
            string hostname = url.Host.ToString();
           string lang = null;
             if (hostname == "urWebsite.com")
            {
                lang = "en-US";
            }
    
            else
            {
                lang = "ar-JO";
            }
    
    
            if ((lang != null))
            {
                 if (!string.IsNullOrEmpty(lang))
                {
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
    
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
    
                }
    
            }
    
        } 
    }
    
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 18, 2009 4:54 PM
  • User1282917655 posted

    Looks like this os working great! Thanks very much for the replies. I want the culture to change on all the pages that the user navigates to, so I need to add the code to all the web pages. Is there a way to reference the same code in all the pages so that I won't have the same code in all the pages?

     

    Yes you can .. Generate a base class that inherits from System.Web.UI.Page .. then write the repeated code inside that class.

    Now lets all pages in the website inherits  from that base class ..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 20, 2009 12:38 PM

All replies

  • User1282917655 posted

     

     

     Protected Overrides Sub InitializeCulture()
    
            Dim url As System.Uri = Request.Url
            Dim hostname As String = url.Host.ToString()
    
            Dim lang As String
           
            If hostname = "urWebsite.com" Then
                lang = "en-US"
            Else
                lang = "ar-JO"
            End If
    
            If (lang <> Nothing) Then
                If lang <> "" Then
                    Thread.CurrentThread.CurrentUICulture = New CultureInfo(lang)
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang)
                End If
            End If
    
        End Sub
      
    Saturday, April 18, 2009 7:06 AM
  • User1673332766 posted

    I tried to place this code at the top of the MasterPage.master.cs page, but Viual Web Developer does not seem to like the syntax. This web is defined in C#. Can you provide some detailed instructions as this is all new to me? Where should the code be placed? Many thanks!

    Saturday, April 18, 2009 4:05 PM
  • User1282917655 posted

     No .. Do not use this code in the master page .. use it in the content page ..

     to change the culture of the page, you need to override the InitializeCulture.

    there you can change the culture of the page ..

    but first you need call the proper namespaces 

     
    using system.threading;
    
    using system.globalization
     
    protected override void InitializeCulture() 
    { 
        
        System.Uri url = Request.Url; 
        string hostname = url.Host.ToString(); 
        
        string lang = null; 
        
        if (hostname == "urWebsite.com") { 
            lang = "en-US"; 
        } 
        else { 
            lang = "ar-JO"; 
        } 
        
        if ((lang != null)) { 
            if (!string.IsNullOrEmpty(lang)) { 
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang); 
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang); 
            } 
            
        } 
    } 

     
    you add any language(culture) you want, just add it to the lang variable.

    in my example, I am using the English Culture and Arabic Culture on some conditions the page will be changed to the proper Culture.

    Saturday, April 18, 2009 4:13 PM
  • User1673332766 posted

    So I need to add this code to all the cs pages of the web sites? I tried it on the default.aspx.cs page as follows, but see some syntax errors on "void" and "cultureinfo" (sorry I need this with a teaspoon...):

    using system.threading;

    using system.globalization;

    protected override void InitializeCulture()

    {

     

    System.Uri url = Request.Url;

    string hostname = url.Host.ToString();

     

    string lang = null;

     

    if (hostname == "urWebsite.com") { lang = "en-US";

    }

    else {

    lang = "ar-JO";

    }

     

    if ((lang != null)) {

    if (!string.IsNullOrEmpty(lang)) {

    Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);

    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);

    }

     

    }

    }

    public partial class _Default : System.Web.UI.Page

    {

    }

    Saturday, April 18, 2009 4:45 PM
  • User1282917655 posted

     place the void InitializeCulture inside the page class.

     

    using System.Threading;
    using System.Globalization;
    
    public partial class Default3 : System.Web.UI.Page
    {
        protected override void InitializeCulture()
        {
    
            System.Uri url = Request.Url;
            string hostname = url.Host.ToString();
           string lang = null;
             if (hostname == "urWebsite.com")
            {
                lang = "en-US";
            }
    
            else
            {
                lang = "ar-JO";
            }
    
    
            if ((lang != null))
            {
                 if (!string.IsNullOrEmpty(lang))
                {
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
    
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
    
                }
    
            }
    
        } 
    }
    
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 18, 2009 4:54 PM
  • User1673332766 posted

    Looks like this os working great! Thanks very much for the replies. I want the culture to change on all the pages that the user navigates to, so I need to add the code to all the web pages. Is there a way to reference the same code in all the pages so that I won't have the same code in all the pages?

    Monday, April 20, 2009 12:29 PM
  • User1282917655 posted

    Looks like this os working great! Thanks very much for the replies. I want the culture to change on all the pages that the user navigates to, so I need to add the code to all the web pages. Is there a way to reference the same code in all the pages so that I won't have the same code in all the pages?

     

    Yes you can .. Generate a base class that inherits from System.Web.UI.Page .. then write the repeated code inside that class.

    Now lets all pages in the website inherits  from that base class ..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 20, 2009 12:38 PM