Hi there, I've been searching and trying different methods of implementing a multilingual site.. The most common used way here is to reproduce the site-tree for each different language the site supports. As this could be a useful way to implement the logic for a site which contents may vary between one language to another, I think it is really bad in some cases. That's why I "implemented" a different handling of multilingual content that let switching between languages just a matter of a variable passed once trough querystring avoiding the duplication of the site tree and avoiding the association of languages with hostnames. It is a very simple way that I'm sure nodoby would have problem to make by himself, anyway I'm happy to share it with the whole community..
In this implementation I make use of Session but I'm sure you could use Cache directives or the Profile object or whatever you think it's better...
---To accomplish this steps you need the source code of Umbraco. You will need Visual Studio or whatever will let you recompile the project/solution---
The first step is to edit the library function GetDictionaryItem (open the file library.cs in the presentation project).
With a few editing the function will look like this:
Code:
public static string GetDictionaryItem(string Key)
{
try
{
int lang = System.Convert.ToInt32( Session("langid"));
if (lang>0)
{
return new Dictionary.DictionaryItem(Key).Value(lang);
}
return new Dictionary.DictionaryItem(Key).Value();
}
catch (Exception errDictionary)
{
HttpContext.Current.Trace.Warn("library", "Error outputting dictionary item '" + Key + "'",
errDictionary);
return string.Empty;
}
}
As you can see this code refers to a Session variable called 'langid', retrieved for istance.. from the current user SESSION.. This variable is passed as the id of the current language instead of taking it from the hostname of the current page..
Now I addedd this line in the Global.asax, inside Session_Start method:
Code:Session.Add ("langid",0);
This creates and sets to 0 the Session variable "langid"
Then in the OnInit() method of the default.aspx.cs file, add this code:
Code:
int lang;
if (!String.IsNullOrEmpty(Request["langid"]))
{
lang = System.Convert.ToInt32(Request["langid"]);
if (lang > 0)
{
Session["langid"] = lang;
}
}
(Please do not add this code in the OnLoad() method or it won't work correctly!)
Now, everytime you want your site to change language (maybe when the user clicks a specific link with a countryflag.. or whatever.. just use this link: "?langid=x" Substitute x with the id of the language you want your site to turn in and automatically all the dictionary contents will be loaded in that language..
If you don't know how to find the ids of the languages of your site, just look at the table 'UmbracoLanguage' in your DB and you'll find it..
This code is very simple and I'm pretty sure it could be improved and SURELY extended, I just wrote it some minutes ago and it was very useful for me. I just wanted to share it with you all, to launch an idea and to know what do you think about..
Let me know..
Francesco