Jump to content

Default language per domain. Language switcher not working


spoetnik
 Share

Recommended Posts

I am developing a multilingual website. This website has 3 languages, and two domain.

Languages: English (default), Dutch, and Chinese.

It has a .com domain, and a .nl domain.

The idea is to serve the default language (English) on the .com-domain

Serve the Dutch language in the .nl domain.

Have a language switcher to switch to the preferred language on any domain.

For the default by domain switching I have included this snippet on the homepage:

// grab the topleveldomain, i.e. "com", "nl", "ch", etc. (wont work for .co.uk... sorry)
$domain = rtrim($config->httpHost, '.');
$languageName = end(explode('.', $domain));
// sanitize it just for good measure
$languageName = $sanitizer->pageName($languageName); 
$language = null;
// attempt to find a language with the same name
if($languageName) $language = $languages->get($languageName); 
// if no language matches, then we'll assume 'default' language.
if(!$language || !$language->id) $language = $languages->get('default'); 
// set the language to the user
$user->language = $language;
 

For the language switcher I have included this snippet on every page of the site:

 echo "<ul class=\"languageselector\">";
                foreach($languages as $language) {
                  if(!$page->viewable($language)) continue; // check if page not published for this language
                  $class = "$language" == "$user->language" ? "active" : "";
                  $url = $page->localUrl($language); 
                  echo "<li class=\"$language->title hide-text \"><a href='$url'>$language->title</a></li>";
                }
                echo "</ul>";
 

Everything works fine, except for the homepage!

The language switcher doesn't work on the homepage, because of the snippet loading the default language, and thus overruling the language switcher..

I would like to set the default language on the initial load of the site. Set the initial language in the session or something, and the let the language switcher overrule this setting.

Any suggestions??

Link to comment
Share on other sites

Your code for setting the language based on the domain looks good. It should be fast, and fine to include in every page. If you wanted to set it to the session, then you would just do something like this (though it's really probably not necessary): 

if($session->language) {
  $user->language = $languages->get($session->language); 
} else {
  // determine language from domain
  // ...your existing code here, then...
  $session->language = $user->language->name; 
}

I don't understand the issue you've mentioned with the homepage. Since the language is  being determined from the domain (rather than the path), it really shouldn't matter what page you are on. Is it possible that you've got your output code happening before your language-determination code?

Reading your message again, you mentioned that you only had the language-determination code on your homepage?  You should really put it in every single template, via a common include (perhaps using $config->prependTemplateFile option). Let the language be determined on every request, regardless of page. There's no guarantee that traffic will always arrive via the homepage, not to mention the potential SEO issues with assuming that. 

Link to comment
Share on other sites

  • 2 weeks later...

Hey Ryan,

Thanks for your help.

The problem is that I want the default language to be determined by the domain. This part works great. I have added the language-determination code via a common include. There is one catch: i want a language selector so the visitor can overrule the determined language. I was thinking to store this overridden language in a session.

So, if there is no chosen language (in the users session), use the language determined by the domain.

If a user has chosen an other language via the language selector, this must overrule the language determined by the domain.

I hope this is clear? 

I think i need to adjust this snippet to store the choice in the users session, but I don't know how.

<?php    echo "<ul class=\"languageselector\">";
        foreach($languages as $language) {
            if(!$page->viewable($language)) continue; // check if page not published for this language
                 $class = "$language" == "$user->language" ? "active" : "";
                $url = $page->localUrl($language); 
                echo "<li class=\"$language->title hide-text \"><a href='$url'>$language->title</a></li>";
        }

    echo "</ul>";
?>
 
Link to comment
Share on other sites

Yeah I think the only way (dirty) is to use GET parameter added to each language switch with the language name, so you can know user has switched.

I have hard time understanding why you need this kinda weird complex setup and not let the url speak. As I see you use LanguageSupportPageNames... I guess you then use no lang segment set on homepage names? There is this behavior of the default system language when those are set that redirects you to the root url if you enter domain.com/en/ when en is the default.

It's kinda hard with this way of doing it, and I'm not sure what the best setup would be as there are so many variations and ways to do a ML setup. I just remember having such session and lang url params projects that was hard to figure out what's happening when something didn't work out or got broken. Too complicated for my simple mind really :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...