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.
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.
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.
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.
how?
I haven't tested this but something like this could work:
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.
By the way, this should be added to the /config/UrlRewriting.config of course. :-)
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
give each site a domain name.
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?
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";
}
}
is working on a reply...