Change culture based on domain name extension

Last post 04-20-2009 12:38 PM by Abdulla.AbdelHaq. 7 replies.

Sort Posts:

  • Change culture based on domain name extension

    04-17-2009, 9:39 AM
    • Member
      2 point Member
    • pgroettjord
    • Member since 04-17-2009, 1:37 PM
    • Posts 4
    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).
  • Re: Change culture based on domain name extension

    04-18-2009, 7:06 AM

     

     

     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
      
    Plz remember to click "Mark as Answer" if this helped you.

    Abdulla AbdelHaq    MCTS, MCPD

    - My Articles on ASP Alliance
    - My Weblogs
    - My Sessions on JorDev User Group

    "Experience is simply the name we give our mistakes"
  • Re: Change culture based on domain name extension

    04-18-2009, 4:05 PM
    • Member
      2 point Member
    • pgroettjord
    • Member since 04-17-2009, 1:37 PM
    • Posts 4

    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!

  • Re: Change culture based on domain name extension

    04-18-2009, 4:13 PM

     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.

    Plz remember to click "Mark as Answer" if this helped you.

    Abdulla AbdelHaq    MCTS, MCPD

    - My Articles on ASP Alliance
    - My Weblogs
    - My Sessions on JorDev User Group

    "Experience is simply the name we give our mistakes"
  • Re: Change culture based on domain name extension

    04-18-2009, 4:45 PM
    • Member
      2 point Member
    • pgroettjord
    • Member since 04-17-2009, 1:37 PM
    • Posts 4

    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

    {

    }

  • Re: Change culture based on domain name extension

    04-18-2009, 4:54 PM
    Answer

     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);
    
                }
    
            }
    
        } 
    }
    
      
    Plz remember to click "Mark as Answer" if this helped you.

    Abdulla AbdelHaq    MCTS, MCPD

    - My Articles on ASP Alliance
    - My Weblogs
    - My Sessions on JorDev User Group

    "Experience is simply the name we give our mistakes"
  • Re: Change culture based on domain name extension

    04-20-2009, 12:29 PM
    • Member
      2 point Member
    • pgroettjord
    • Member since 04-17-2009, 1:37 PM
    • Posts 4

    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?

  • Re: Change culture based on domain name extension

    04-20-2009, 12:38 PM
    Answer

    pgroettjord:

    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 ..

    Plz remember to click "Mark as Answer" if this helped you.

    Abdulla AbdelHaq    MCTS, MCPD

    - My Articles on ASP Alliance
    - My Weblogs
    - My Sessions on JorDev User Group

    "Experience is simply the name we give our mistakes"
Page 1 of 1 (8 items)