Copied to clipboard

Flag this post as spam?

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


  • syn-rg 282 posts 425 karma points
    May 13, 2010 @ 09:28
    syn-rg
    0

    Switch between EN and ES language sites?

    I've created a multi-lingual site. With both English and Spanish. At the moment some hardcoded links in my templates have required me to prefix the link with /en/mypage.aspx

    Is there a way I can keep the /mypage.aspx and have the /en/ removed so that users in the Spanish language site aren't being redirected to the English site? Some sort of /??/mypage.aspx that would know from the site you're viewing EN or ES and direct you to the correct page within that languages site.

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    May 13, 2010 @ 13:01
    Sebastiaan Janssen
    0

    You could do it with URL rewriting I suppose. Just add something like ?lang=es to the querystring, pick that up using urlrewriting and rewrite the /en/ part to /es/ using the querystring value.

  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    May 13, 2010 @ 13:31
    Biagio Paruolo
    0

    how?

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    May 13, 2010 @ 14:19
    Sebastiaan Janssen
    0

    I haven't tested this but something like this could work:

    <add name="languagerewrite" 
      virtualUrl="^~/([a-zA-Z]{2,3}?)/(.*)[\?\&]lang=([a-zA-Z]{2,3}?)" 
      rewriteUrlParameter="ExcludeFromClientQueryString" 
      destinationUrl="~/$3/$2" 
      ignoreCase="true" />

    The first match is the language that is currently there, two or three characters: ([a-zA-Z]{2,3}?)

    The second match is just anything after that (.*) UNTIL "?lang=" or "&lang=" is encountered. 

    The third match is the language that is entered in the querystring, again two or three characters.

    In the destination url, you just concat the third match (querystring language value) with the second match (the rest of the url).

    Should work, try  it out! Now you could switch to any document in a different language by just adding ?lang=xxx or &lang=xxx to the querystring.

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    May 13, 2010 @ 14:20
    Sebastiaan Janssen
    0

    By the way, this should be added to the /config/UrlRewriting.config of course. :-)

  • syn-rg 282 posts 425 karma points
    Aug 18, 2010 @ 02:11
    syn-rg
    0

    Hi Sebastiaan

    Thanks for the reply.

    How does this work for my site with the /en/ and /es/ ?

    I'm not sure what I need to do to get this to workin the /config/UrlRewriting.config

    <urlrewritingnet xmlns="http://www.urlrewriting.net/schemas/config/2006/07">
      <rewrites>

        <add name="rsstagrewrite" virtualUrl="^~/blog/rss/tags/(.*).aspx" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="~/blog/rss.aspx?tag=$1" ignoreCase="true" />

     <!--
    <add name="languagerewrite" virtualUrl="^~/([a-zA-Z]{2,3}?)/(.*)[\?\&]lang=([a-zA-Z]{2,3}?)" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="~/$3/$2" ignoreCase="true" />-->
     
      </rewrites>
    </urlrewritingnet>
  • sun 403 posts 395 karma points
    Aug 18, 2010 @ 03:10
    sun
    0

    give each site a domain name.

  • syn-rg 282 posts 425 karma points
    Aug 19, 2010 @ 08:39
    syn-rg
    0

    Ok, each site now has a domain name

    EN - www.example.com/en

    ES - www.example.com/es

    Now what do I need to do?

  • fabio 87 posts 107 karma points
    Aug 19, 2010 @ 10:57
    fabio
    0

    i found in some other post this advice and having the same problem.

     from the language section under settings create a list of managed languages, with something like this

     

     Language[] languages = Language.getAll;

             this.ddLangs.DataSource = languages;

             this.ddLangs.DataTextField = "FriendlyName";

             this.ddLangs.DataValueField = "CultureAlias";

     i'm doing it in a user control but i think you can do it also in xslt.

    Supposing that your content structure looks like

    www.example.com

     - en

     - es

    On the template that manage www.example.com

    i have another user control that more or less do the following:

     

    use a cookie to mantain a preferred language.

    if no cookie is defined use Page.Request.UserLanguages to set the preferred

    user language ( setCulture method without parameter )

    otherwise read lang code from cookie and set the culture from the code

    ( setculture with parameter)

    lngswitched is a parameter that i use as a shortcut in the case a user change language.

    i think that inf you also need to show the same page in the selected language, it shoud be easy to rewrite 

    the url from ww.example.com/en/......./a.aspx to  ww.example.com/es/......./a.aspx

    Above a little bit of code ,maybe it helps you.

    --- Code About macro on www.domain.xyz

     

      var cookie = new HttpCookie("preferredLanguage");

      cookie.Expires = DateTime.Now.AddMonths(1);

      string redirectLanguage = "";

      if (lngswitched.Length <= 0)  redirectLanguage = SetCulture();

      else redirectLanguage = SetCulture(lngswitched);

      if (!string.IsNullOrEmpty(redirectLanguage) ) {

       cookie.Value = redirectLanguage;

       Response.Cookies.Add(cookie);

     }

    if (!string.IsNullOrEmpty(redirectLanguage)) {

     Response.Redirect("/" + preferredLanguage + ".aspx");

    }

     

    var defaultLanguage = System.Configuration.ConfigurationSettings.AppSettings["DefaultLanguageRedirect"];

    if (!String.IsNullOrEmpty(defaultLanguage)) {

     Response.Redirect(defaultLanguage);

    }

    Response.Redirect("/en.aspx");

     

     

     

     private string SetCulture() {

             try {

     

                string redirectLanguage = "";

                // Set culture

                if (Page.Request.UserLanguages != null) {

                   foreach (string language in Page.Request.UserLanguages) {

                      if (language.Substring(0, 2).Equals("es")) {

                         redirectLanguage = "es";

                         System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("it-IT");

                         break;

                      }

                      else {

                         redirectLanguage = "en";

                         System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("en");

                         break;

                      }

                   }

                }

     

                return redirectLanguage;

             }

             catch (Exception ex) {

                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

                return "en";

             }

          }

     

     

    private string SetCulture(string par) {

             try {

     

                string redirectLanguage = "";

                // Set culture

                Language[] languages = Language.getAll;

                if (par != null && par.Length > 0) {

                   foreach (Language language in languages) {

                      if (language.CultureAlias.Substring(0, 2).Equals(par.Substring(0, 2))) {

                         redirectLanguage = par.Substring(0, 2);

                         System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(redirectLanguage);

                         break;

                      }

                   }

                }

                return redirectLanguage;

             }

             catch (Exception ex) {

                System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("en");

                return "en";

             }

          }

Please Sign in or register to post replies

Write your reply to:

Draft