Jump to content

how to load the right language ?


Doc
 Share

Recommended Posts

hi guys,

My homepage is built on a home.php file which calls/(includes) a _home_inc.php file.

In the home.php file, if the user is not logged (I don't know anything about him), I take the browser's default language, let's say english ('en').

How can I load the english version of my website ? My default language is french.

I'd like to say to Processwire to go to http://www.myurl.com/en and not http://www.myurl.com/ (which is french based).

(the english version exists, I've setup the multi-language options in the admin and I can go to http://www.myurl.com/en and see the english version).

 

Thanks

Link to comment
Share on other sites

This works for me:

$languages = wire('languages');
                                foreach($languages as $language) {
                                    $selected = '';

                                    // if this page isn't viewable (active) for the language, skip it
                                    if(!$page->viewable($language)) continue;

                                    // determine the "local" URL for this language
                                    $url = $page->localUrl($language);

                                    // if language is current user's language, make it selected
                                    if($user->language->id == $language->id) {
                                        $url = "#";
                                        $selected = " class='current'";
                                    }

                                    // output the option tag
                                    echo "<li$selected><a href='$url'><span>$language->title</span></a></li>";
                                }

 

  • Like 1
Link to comment
Share on other sites

Thanks @harmen

You brought to me part of the answer.

I'll use 

$url = $page->localUrl($language);

... more convenient than the url I was building myself before. I'll update my language selector with that.

Actually I was looking for a way to dynamically load on the first access to my homepage, and without any manual intervention, the right language according to the browser's settings.

I think I'll use this : (from http://processwire.com/api/multi-language-support/multi-language-fields/)

$page->of(false); // turn off outputFormatting (if it's not already)

$dutch = $languages->get('dutch'); // get the language we want

$body = $page->body->getLanguageValue($dutch); // get the unformatted value in Dutch

So I can dynamically load the content I'm looking for whatever the url which is loaded on the first access (http://www.myurl.com/ in my case).

"/" is normally dedicated to french, "/en" to english.

That way, on first try, for an unauthenticated user, my website will land on http://www.myurl.com/ url, in english if I detect english preferences in the browser, or in french if I detect a french browser.

Perhaps there is an easier way ? I think that solution is better than redirect with a 301/302 on that url : 

$url = $page->localUrl($language);

 

 

Link to comment
Share on other sites

There's not really a way to reliably detect an overall first visit to your website. You could however use the session and store some key or really anything by which you can differenciate between first visit (redirect) and later visits (no redirect) while the session remains active. Or you could use a cookie, which could even outlive the session of an user.

  • Like 2
Link to comment
Share on other sites

Thanks for your feedback @LostKobrakai, I won't dig further for the first visit settings.

I'll keep french by default at the moment, and implement the dynamic language preference if I get some extra time before the launch.

I use sessions to store the language once I know the user's preference.

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

×
×
  • Create New...