Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • dominik 711 posts 733 karma points
    Aug 26, 2011 @ 10:20
    dominik
    0

    Load website in correct language

    Hello everybody,

    Ive got the following scenario:

    a umbraco site (version 4.7) with the following structure:

    - en (folder)
      - home.aspx
    - de (folder)
      - home.aspx

    Now i want to add a script which redirects the user to the correct home.aspx page. I was able to get the language from the browser via an c# script but my question is how i can now redirect the user to the correct site.

    If i use a switch with Repsonse.Redirect it ends in an endless loop

    Thanks

     

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Aug 26, 2011 @ 10:54
    Michael Latouche
    0

    Hi Dominik,

    Where exactly hev you put your test? In a template? In the global.asa? Or somewhere else.

    My guess is that the test is done in a part of code that gets executed when you load the home.aspx page, hence leading to endless redirects.

    If you can maybe give some more info about how you implemented it, it might help find the problem.

    Cheers,

    Michael.

  • dominik 711 posts 733 karma points
    Aug 26, 2011 @ 11:34
    dominik
    0

    yes i integrated this in the home.aspx but i dont know where to put it instead.

    is there any page which is opened before going to the home.aspx ?

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Aug 26, 2011 @ 11:42
    Michael Latouche
    0

    Hi Dominik,

    This is the reason of the endless loop: every time you get to home.aspx, it redirects to itself.

    I don't know how you implemented your test, but I guess that if you first test if you are in the correct language prior to redirecting, ot should solve the problem. So, you only redirect if you are not in the good "home.aspx".

    If this is not the issue, could you maybe post some code so I can have a better idea of how it is done? It would help in identifying the problem.

    Cheers,

    Michael.

     

  • dominik 711 posts 733 karma points
    Aug 26, 2011 @ 11:48
    dominik
    0

    ok here is my code:

     // Get Language from Browser
            string language = GetBrowserLanguage();
          
            if (!string.IsNullOrEmpty(language))
            {
     
                //Redirect to the correct page based on language, default to english
                switch (language)
                {
                    case "DE":
                        Response.Redirect("de/home.aspx");
                        break;
                    default:
                        Response.Redirect("en/home.aspx");
                        break;
                }
            }

     

    So i think i have to ask in each switch case if the current url has a structure "de" or "en" ?

    How can i check if the browser url includes "de" or "en"?

     

  • Rasmus Berntsen 215 posts 253 karma points c-trib
    Aug 26, 2011 @ 11:57
    Rasmus Berntsen
    0

    I guess you know how the URL looks like?

    You could grab the current url with:

    string currentUrl = Request.Url.AbsoluteUri;

    And check if it contains "de" or "en" ?

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Aug 26, 2011 @ 12:04
    Michael Latouche
    0

    Hi Dominik,

    If I am not mistaking, one way of doing is checking the "Request.RawUrl", which returns the url as a string. So, for example, the following would check if you are in the "en" part:

    if (Request.RawUrl.Contains("/en/"))
    ...

    Another way of doing is first determining in what language you currently are, something like:

    string curLang = Request.RawUrl.Substring(1, Request.RawUrl.IndexOf("/")); //gets the first part of the URL = the language. Start at index 1 because I think the RawUrl alwyas starts with "/" (not sure)

    // Get Language from Browser
            string language = GeBrowserLanguage();

    if (curLang != language) // might need to check on the case here, so maybe all to lower case or something
    // Do your redirect logic

    Hope this helps

    Cheers,

    Michael

    Edit: corrected the strange formatting...

  • dominik 711 posts 733 karma points
    Aug 26, 2011 @ 12:26
    dominik
    0

    Hi Michael

    If i use your code curLang is always empty.


  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Aug 26, 2011 @ 12:34
    Michael Latouche
    0

    Sorry,

    My mistake, I mixed the start indexes. This should work better:

    string curLang =Request.RawUrl.Substring(1,Request.RawUrl.IndexOf("/", 1));//gets the first part of the URL = the language. Start at index 1 because I think the RawUrl alwyas starts with "/" (not sure)

    Cheers,

    Michaël

  • dominik 711 posts 733 karma points
    Aug 26, 2011 @ 12:39
    dominik
    0

    Hi Michael,

    Thanks a lot

    now i get en/ for example - So i have to remove the / and id should work

  • dominik 711 posts 733 karma points
    Aug 26, 2011 @ 12:54
    dominik
    0

    Ok this works now,

    Thanks a lot

    But everytime i change my language now via my language dropdown it overwrite the user choice (language) with this settings.

    The system should just select the default language (from browser settings) but the user should be able to manually change his language via dropdown (which still exists)

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Aug 26, 2011 @ 13:45
    Michael Latouche
    0

    Hi Dominik,

    For the extra "/", this should do the trick but I guess you already found it:

    string curLang = Request.RawUrl.Substring(1,Request.RawUrl.IndexOf("/",1) -1);

    If you have a dropdown where the user can change the language, then I guess the easiest is to use the value of the dropdown instead of getting it from the browser settings. The only thing to do is to initialize the dropdown with the browser settings the first time the visitor enters the site.

    Cheers,

    Michaël.

  • dominik 711 posts 733 karma points
    Aug 26, 2011 @ 13:51
    dominik
    0

    Yes you are right,

    But how can i do this correct inizialization?

    Any idea?

    Thanks for your great support

  • dominik 711 posts 733 karma points
    Aug 26, 2011 @ 13:55
    dominik
    0

    Currently i am using this package

    http://our.umbraco.org/projects/website-utilities/language-selector

    So it would be great if a default browser language selection could be integrated here

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Aug 26, 2011 @ 14:19
    Michael Latouche
    0

    Yes it would be great indeed, too bad it is not.

    I don't know how this package works, so it's hard to help you much further :-S Maybe it creates a cookie that you could check on: if it is available, use it, otherwize, use browser settings.

    I guess the best is to contact the package creator and get some more info on how he keeps the language selected, and use the same system to let the dropdown select the browser settings by default.

    Cheers,

    Michael.

  • dominik 711 posts 733 karma points
    Aug 26, 2011 @ 14:22
    dominik
    0

    I created a thread in the feature request forum of this package.

    It does not store any cookie but this is the script:

    <msxml:using namespace="umbraco.cms.businesslogic.language" />
            public static string GetLanguage(int pageId)
            {
            try
            {
                Domain[] domains = library.GetCurrentDomains(pageId);
                if (domains != null &amp;&amp; domains.Length &gt;= 0)
                {
                int languageId = domains[0].Language.id;
                Language language = new Language(languageId);
                return language.CultureAlias;
                }
            }
            catch (Exception errDictionary)
            {
            }
            return string.Empty;
            }
        </msxml:script>

     

    I think here i must integrate the browser Language

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Aug 26, 2011 @ 14:44
    Michael Latouche
    0

    Mmhhh,

    This is to get the language based on the the page URL, but I don't quite see how to put your code in it wihtout overwriting the user selection every time :-S

    Maybe you should try to put your test in the global.asa file after all... In the Session_Start event. This event gets called only once, at the beginning of the user session. So you set the language based on the browser settings at that point only, and after that you leave the choice to the user to select another language in the dropdown if he wishes

    Hope this works out.

    Cheers,

    Michael.

  • dominik 711 posts 733 karma points
    Aug 26, 2011 @ 14:58
    dominik
    0

    HI Micheal,

    Putting the code in the global.asax is not working on our scenario. I think the best method would be that the language selector pre select the dropdown field if there is an entry which corresponds with the browswer language

     

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Aug 26, 2011 @ 15:09
    Michael Latouche
    0

    Hi dominik,

    This would indeed be the best solution, but when I look at the code you posted below, I don't think it will be that easy because you set the language based on your pageId => you would need to be able to differenciate when you are on that page because you just arrived on the site (and then use browser settings to redirect if needed), or because you have chosen the language to arrive to that page (and then do nothing).

    I am not sure this is the best way, but I guess what you can do is store somewhere (session, cookie, ...) a flag indicating that you have done the bro wser setting check. So the first time the visitor enters the site, the flag is not set and you can do the browser check and also set the flag. For the requests after that, the flag is set and you just let the user choose what he wants from the dropdown without interfering.

    I'm not sure session would be allowed in your scenario, but I guess cookies would?

    Cheers,

    Michael.

Please Sign in or register to post replies

Write your reply to:

Draft