Alternative implementation of a Multilingual site Options
justfra
Posted: Friday, May 09, 2008 5:40:15 PM
Rank: Newbie

Joined: 4/22/2008
Posts: 12
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
kalpa
Posted: Friday, May 09, 2008 6:04:38 PM

Rank: Fanatic

Joined: 7/19/2006
Posts: 345
Location: Göteborg, Sweden
Interesting solution but couldn't the same functionality be achieved using Umbraco.library methods from XSLT? I would prefer that to be able to update Umbraco...

How do you handle the body content? Via the translate methods or do you use multiple tabs in your document types?

One other thing to consider is search engines. Since SEs isn't session aware you should always try to use unique URLs for each page and language some SEs will even ignore your pages if the only difference between them is a querystring parameter...

Just my 2 cents...

// ;) Kalle


" - Yeah I'd like to share your point of view, as long as it's my view too... (http://www.d-a-d.dk/lyrics/pointofview)
justfra
Posted: Saturday, May 10, 2008 12:37:06 AM
Rank: Newbie

Joined: 4/22/2008
Posts: 12
I don't know if this could be achieved using xslt, as I don't know in which way use xslt to set such of a session variable or stuff like that.. I surely think this could be integrated better in Umbraco, in fact my "solution" was just an idea born from some code I wrote in some minutes trying to solve a problem..
For what refers to Search Engines, to be honest I didn't take in consideration the problem you explained.. You are right on what you say, but I think that duplicate pages is also often useless and unproductive (unless you have got a special reason for doing it)..
However I think that with umbraco urlrewriting and some page properties even the unique urls feature is easly achievable...
kalpa
Posted: Saturday, May 10, 2008 12:55:31 AM

Rank: Fanatic

Joined: 7/19/2006
Posts: 345
Location: Göteborg, Sweden
Hi Justfra!

I didn't mean to make your solution look bad, sorry if it seamed that way...
On the bottom line it's what works that will feed us ; )


Yes, you can set/get cookies via the XSLT Extensions umbraco.library:
umbraco.library:setCookie(string key, string value)
umbraco.library:getCookie(string key, string value)

For what I've read you can't set and get the cookie in the same page render, therefore you can only read the cookie in the next pageload and resort to reading the querystring parameter for the first switch...

// ; ) Kalle



" - Yeah I'd like to share your point of view, as long as it's my view too... (http://www.d-a-d.dk/lyrics/pointofview)
justfra
Posted: Saturday, May 10, 2008 1:08:12 AM
Rank: Newbie

Joined: 4/22/2008
Posts: 12
Thank you, it's not that much that I'm using umbraco and sometimes I don't get everything..
So I think this would surely be better to use that method than using sessions and putting code in the project for doing something that is still implemented.. The only thing to beware of is whether the cookies are enabled or not in the user's browser..

PS: I can't see this post indexed in the section index.. can you ?
kalpa
Posted: Saturday, May 10, 2008 9:57:50 AM

Rank: Fanatic

Joined: 7/19/2006
Posts: 345
Location: Göteborg, Sweden
...and you would have to switch the language for the dictionary as well...

// ;) Kalle

PS. Yes I found it on page 47 when I showed the last two days...



" - Yeah I'd like to share your point of view, as long as it's my view too... (http://www.d-a-d.dk/lyrics/pointofview)
Ig_p118
Posted: Saturday, May 10, 2008 10:36:20 AM
Rank: Aficionado

Joined: 7/21/2006
Posts: 155
Location: Italy
which book?

Red Consulting s.a.s - Umbraco from v1.0
justfra
Posted: Saturday, May 10, 2008 10:45:11 AM
Rank: Newbie

Joined: 4/22/2008
Posts: 12
Hi Kalle, what do you mean that I've to switch the language for the dictionary ? Maybe I'm not getting what you mean because dictionary content is loaded with function getdictionaryitem.. editing this function and letting it load the content of a specific language not by domains but by such a variable (in the example langid) will automatically return the content of every dictionary in the specified language... So you don't have to change anything else... just put the dictionary somewhere... set the content for the dictionary in the n different languages.. now just change somehow the value of langid and everything (related to dictionaries) will turn in the other language...
kalpa
Posted: Sunday, May 18, 2008 9:28:59 AM

Rank: Fanatic

Joined: 7/19/2006
Posts: 345
Location: Göteborg, Sweden
I've been a lazy forum visitor this week, I wasn't referring to book, it was the active topics listing I ment...

...and I think I was wrong in the language switching. I wasn't aware of the langid way to switch language...

// ;) Kalle

" - Yeah I'd like to share your point of view, as long as it's my view too... (http://www.d-a-d.dk/lyrics/pointofview)
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.