Welcome, Guest. Please login or register.
Did you miss your activation email?
06 Oct 2008, 10:34

Login with username, password and session length
Home Chat Help Search Calendar Login Register
Pages: [1]
Print
Author Topic: Starting homepage based on users browser language  (Read 5172 times)
0 Members and 1 Guest are viewing this topic.
RonnyK
Member Support Team
Support Guru
Power Poster
****

Karma: 71
Offline Offline

Posts: 2519

Location: Raalte, the Netherlands



« on: 26 Jul 2007, 03:41 »

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:
{
$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:
   <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:
{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 Edit: 17 Aug 2007, 02:43 by RonnyK » Logged

Pierre M.
Support Team member
Support Guru
Power Poster
****

Karma: 31
Offline Offline

Posts: 2870

Location: Paris

Please keep it simple


« Reply #1 on: 30 Jul 2007, 09:54 »

Hello Ronny,

Code:
{
$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.
Logged

-- Pierre, support team member. comodrateur du forum francophone.
Please read "how to submit installation/debug help requests" before posting. Don't send private messages to ask for support.
Want to contribute to CMSms ? Improve the wiki with your forum account.
DeMZed
Forum Member
*

Karma: 1
Offline Offline

Posts: 19


« Reply #2 on: 14 Aug 2007, 09:08 »

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

Thanks Smiley
« Last Edit: 14 Aug 2007, 09:21 by DeMZed » Logged
RonnyK
Member Support Team
Support Guru
Power Poster
****

Karma: 71
Offline Offline

Posts: 2519

Location: Raalte, the Netherlands



« Reply #3 on: 14 Aug 2007, 09:24 »

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:
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
Logged

DeMZed
Forum Member
*

Karma: 1
Offline Offline

Posts: 19


« Reply #4 on: 15 Aug 2007, 04:53 »

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 Wink

Thanks
Logged
asfahaan
Forum Member
*

Karma: -1
Offline Offline

Posts: 36

Location: Auckland, New Zealand


« Reply #5 on: 15 Aug 2007, 08:59 »

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.



Logged

Asfahaan Mirza
Auckland, New Zealand
RonnyK
Member Support Team
Support Guru
Power Poster
****

Karma: 71
Offline Offline

Posts: 2519

Location: Raalte, the Netherlands



« Reply #6 on: 15 Aug 2007, 09:14 »

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
Logged

asfahaan
Forum Member
*

Karma: -1
Offline Offline

Posts: 36

Location: Auckland, New Zealand


« Reply #7 on: 15 Aug 2007, 09:48 »

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
Logged

Asfahaan Mirza
Auckland, New Zealand
RonnyK
Member Support Team
Support Guru
Power Poster
****

Karma: 71
Offline Offline

Posts: 2519

Location: Raalte, the Netherlands



« Reply #8 on: 15 Aug 2007, 09:52 »

The link:

http://dev.cmsmadesimple.org/projects/contentaliases/

Ronny
Logged

asfahaan
Forum Member
*

Karma: -1
Offline Offline

Posts: 36

Location: Auckland, New Zealand


« Reply #9 on: 15 Aug 2007, 10:43 »

Cheers for the link.. thats exactly what i wanted Smiley Smiley ta
Logged

Asfahaan Mirza
Auckland, New Zealand
openmtl
Forum Member
*

Karma: 4
Offline Offline

Posts: 14


« Reply #10 on: 03 Sep 2007, 02:34 »

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/index.php?func=detail&aid=1741&group_id=6&atid=103

Logged
OlafNoehring
Forum Member
*

Karma: 3
Offline Offline

Posts: 71


« Reply #11 on: 15 Sep 2007, 15:34 »

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_language_detection.php

Olaf

--
* together for better software *
* http://www.team-noehring.de *
Logged
Pages: [1]
Print
Jump to: