Starting homepage based on users browser language

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
RonnyK
Support Guru
Support Guru
Posts: 4962
Joined: Wed Oct 25, 2006 8:29 pm
Location: Raalte, the Netherlands

Starting homepage based on users browser language

Post by RonnyK »

For a MultiLanguage site I setup CMSMS in a specific way to have the language in the menu as first-level menu. It uses a UDT and switches to the default language of that user, showing all children (language specific pages) under that language.

My page structure looks like:

- Home
- NL
  - NLpage1
  - NLpage2
- EN
  - Enpage1
  - Enpage2
- FR
  - FRpage1
  - FRpage2

I created a UDT called "browserlanguage" that holds:

Code: Select all

{
$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
if (substr($lang, 0, 2) == 'en')
{
header("Location: index.php?page=en");
} elseif (substr($lang, 0, 2) == 'fr')
{
header("Location: index.php?page=fr");
} else {
header("Location: index.php?page=nl");
}
}
The logic of the UDT, is to retrieve the language "if (substr($lang, 0, 2) == 'en')" leads to "en" and the page to be opened is "header("Location: index.php?page=en");", where "page=en" is the alias of the page to be opened.

Content-page 'Home' has this tag in the contentpage.

Insert in the template, where you want the flags to appear for the language. page="XX", where "XX" is the language page in the menu. This is to have the ability to choose a language different than their browser or the default.

Code: Select all

   <div style="float: right;">
              {cms_selflink page="en" image="uploads/flags/lang_en.gif" alt="English" imageonly=1}
              {cms_selflink page="fr" image="uploads/flags/lang_fr.gif" alt="French" imageonly=1}
              {cms_selflink page="nl" image="uploads/flags/lang_nl.gif" alt="Nederlands" imageonly=1}
   </div>
The original {menu} needs an additional parameter (start_level) as it only has to show the children of the chosen parent,

Code: Select all

{menu template='simple_navigation.tpl' start_level='2' collapse='1'}
By going to the site, the home-page will open, run the UDT and re-direct to the language set in the browser.

You can test this on http://demo.krijt.eu, where the code described above is active. For users on IE, the language pulled is the language on top in "Tools -> Internet Options -> Languages".

As you all can see the default language is "NL", so for most users that have their language set to "EN", the script should bring them to the English page.

Ronny
Last edited by RonnyK on Fri Aug 17, 2007 6:43 am, edited 1 time in total.
Pierre M.

Re: Starting homepage based on users browser language

Post by Pierre M. »

Hello Ronny,
RonnyK wrote:

Code: Select all

{
$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
if (substr($lang, 0, 2) == 'en')
{
 ...
} elseif (substr($lang, 0, 2) == 'fr')
{
 ...
} else {
 ...
}
}
Ronny, you know what ? you have begun to implement TCN in CMSms :-)

Pierre M.
DeMZed
Forum Members
Forum Members
Posts: 19
Joined: Thu Apr 05, 2007 3:29 pm

Re: Starting homepage based on users browser language

Post by DeMZed »

Thanks for your contribution Rony !
Very good job ! That's the cheapest solution I've ever seen !

Thanks :)
Last edited by DeMZed on Tue Aug 14, 2007 1:21 pm, edited 1 time in total.
RonnyK
Support Guru
Support Guru
Posts: 4962
Joined: Wed Oct 25, 2006 8:29 pm
Location: Raalte, the Netherlands

Re: Starting homepage based on users browser language

Post by RonnyK »

DeMZed,

what do you exactly mean? The way the logic works is as follows. The UDT that is the only content in the page that is marked as the mainpage. The UDT determines the language that is set in the users browser.

F.E.

Code: Select all

if (substr($lang, 0, 2) == 'en')
{
header("Location: index.php?page=en");
is that if the language is "en", the page to be opened is " index.php?page=en" the startpage for the English site. The calling of the menu, as these languages are the main-level of the menu, needs to be changed, to only show the child of the language level down. The examples of these changes are reflected in my opening topic.

Ronny
DeMZed
Forum Members
Forum Members
Posts: 19
Joined: Thu Apr 05, 2007 3:29 pm

Re: Starting homepage based on users browser language

Post by DeMZed »

RonnyK wrote: What do you exactly mean?
Well, I think our answer was just at the same moment I've edited my thread !
Your descriptions are enough clear to make it work ;)

Thanks
asfahaan
Forum Members
Forum Members
Posts: 43
Joined: Wed Aug 15, 2007 11:28 am
Location: Auckland, New Zealand

Re: Starting homepage based on users browser language

Post by asfahaan »

Hi,

Firstly thanks for this post!

I used it to implement differnt menu items based which country flag the user selected. and it worked perfectly.

But I have a query.

Lets take the same example as RonnyK and add Company profile under NL.
- Home
- NL
  - CompanyProfile
  - NLpage1
  - NLpage2
- EN
  - Enpage1
  - Enpage2
- FR
  - FRpage1
  - FRpage2

If I have a Company Profile page under NL and I wanted it to be displayed under EN and FR as well? How would I do that? Why I am asking is because I am creating a website for a company who has branches in 3 different regions. The company profile is the same for all three of them. So is investor relations, Contact Us, etc...

And if I have sperate page under each country. I would need to edit all corresponding pages seperately? Is that correct?

Thanks in advance.

Any comments are welcome.
Asfahaan Mirza
Auckland, New Zealand
RonnyK
Support Guru
Support Guru
Posts: 4962
Joined: Wed Oct 25, 2006 8:29 pm
Location: Raalte, the Netherlands

Re: Starting homepage based on users browser language

Post by RonnyK »

I thought that you wanted to have the text on the page, in your case "CompanyProfile" to be different in the languages. If so, you can name the page and menu-page the same, while the page-alias on the options-tab should be different, for example "companyprofile-nl" and "companyprofile-de" the name in the menu and title can still be "CompanyProfile". All three pages are seperate pages and can be edited in the different languages.

If you want to share the text on the three pages, you should check "ContentAlias" in the development area.

Ronny
asfahaan
Forum Members
Forum Members
Posts: 43
Joined: Wed Aug 15, 2007 11:28 am
Location: Auckland, New Zealand

Re: Starting homepage based on users browser language

Post by asfahaan »

Hi ROnny,

I think we are not realy on the same page... i think its my mistake as its 3am right now...

This doesnt have anything to do with different language.

Its for Global / Australia / New Zealand.

All are english based regions. Its just that i wanted to use the same content e.g. company profile on all of regions.... rather than having a seperate page for each of them.

i will look into "ContentAlias".

Thanks man
Asfahaan Mirza
Auckland, New Zealand
RonnyK
Support Guru
Support Guru
Posts: 4962
Joined: Wed Oct 25, 2006 8:29 pm
Location: Raalte, the Netherlands

Re: Starting homepage based on users browser language

Post by RonnyK »

asfahaan
Forum Members
Forum Members
Posts: 43
Joined: Wed Aug 15, 2007 11:28 am
Location: Auckland, New Zealand

Re: Starting homepage based on users browser language

Post by asfahaan »

Cheers for the link.. thats exactly what i wanted :) :) ta
Asfahaan Mirza
Auckland, New Zealand
openmtl
Forum Members
Forum Members
Posts: 16
Joined: Sat Jul 07, 2007 11:26 am

Re: Starting homepage based on users browser language

Post by openmtl »

RonnyK wrote: For a MultiLanguage site I setup CMSMS in a specific way to have the language in the menu as first-level menu. It uses a UDT and switches to the default language of that user, showing all children (language specific pages) under that language.

My page structure looks like:

- Home
- NL
  - NLpage1
  - NLpage2
- EN
  - Enpage1
  - Enpage2
- FR
  - FRpage1
  - FRpage2

I created a UDT called "browserlanguage" that holds:
.......

You can test this on http://demo.krijt.eu, where the code described above is active. For users on IE, the language pulled is the language on top in "Tools -> Internet Options -> Languages".

As you all can see the default language is "NL", so for most users that have their language set to "EN", the script should bring them to the English page.

Ronny
That is working excellent for me. One quirk is that the cms_selflink div=next when it gets to the end of one language displays the first page of the next language. This isn't nice so as a fix that kind of works for me I have created a patch on the cms_selflink code that only shows the next "sibling" i.e. the same menu parent. If you have gaps in your menus because it is jumping to the next language tree then the parent won't be the same so next doesn't display anything. I've called my option nextsibling

See,  http://dev.cmsmadesimple.org/tracker/in ... 6&atid=103
OlafNoehring
Forum Members
Forum Members
Posts: 78
Joined: Mon Oct 23, 2006 4:43 pm

Re: Starting homepage based on users browser language

Post by OlafNoehring »

Hi

I "scanned" only very quick over this thread and concerning the user language I think it would be way easier to use this:
http://techpatterns.com/downloads/php_l ... ection.php

Olaf

--
* together for better software *
* http://www.team-noehring.de *
Post Reply

Return to “Tips and Tricks”