Jump to content

Load dynamically a language with translatable strings ?


Doc
 Share

Recommended Posts

Hi guys,

I was advised to ask that question here rather than in general, so here it is :

I detect the default language from the user browser. Let's say that user is not loggedin, I don't know anything about him.

I'd like to display the page in the language retrieved from the browser.

What I have now :

- a multi-language homepage : displaying french (the default language) if user goes to www.website.com/ or english if user goes to www.website.com/en

-> I'm using multi-language fields for that.

I'm able to use translatable strings also, such as :

echo __("My text to translate");

It works when I switch the url "/" to "/en" for example.

I'd like to do that without manual intervention :

- detect the browser's language

- load the right version of the homepage (User not loggedin, never seen him before scenario).

I know how to do it with multi-language :

$page->of(false); // turn off outputFormatting (if it's not already)
$lg_id = $languages->get('en'); // get the language we want
$body = $page->body->getLanguageValue($lg_id); // get the unformatted value in english

Is there a similar way to do it with translatable strings ?

Thanks

Link to comment
Share on other sites

If you detect an user, who you want to see the english content, just redirect them to the www.website.com/en/. Everything will be automatically be translated on that page. You should almost never have to retrieve language values on your own besides in some edge cases.

Link to comment
Share on other sites

Looks like a good idea but how to do that without falling into infinite redirect loops ?

I mean, I'm doing this check in the homepage, the one loaded by www.website.com/

Let's take the scenario where my default url, www.website.com/ is french.

A user comes in, english one, I detect english as its preferred language, he's not logged in. My code would be :

$lg = detect_lg(); // retrieve the browser's preference here

if((!$user->isLoggedin()) && ($lg != 'fr')) {
    $url = $page->httpUrl; // we now have 'http://www.website.com/' in $url
    $url .= 'en';                     // we now have 'http://www.website.com/en' in $url
    $session->redirect("$url"); // goes to http://www.website.com/en
}

So at the end of that code, we're going to http://www.website.com/en/ and my home.php will start again inside that same 'if', which will go to http://www.website.com/en/en and -> not found.

I can add something like '?redir=1' on the url uses by the first redirect so I can catch this later to avoid running one more time the 'if' block ?

Another solution would be to make the redirect on the homepage (home.php template), and that homepage will only do that, and then redirect to a children page such as www.website.com/en/welcome ?

What do you think ?

Edited by kongondo
Mod Note: Please use code blocks (see the <> icon), thanks.
Link to comment
Share on other sites

I'm speaking about a user who isn't logged in, so I suppose session can't work here ?

ok for cookies or url.

I've read other posts this afternoon and they confirmed it isn't very good to make a 301 or 302 as soon as you land on the homepage. So, for now, and only for the homepage, I've decided to use multi-language fields and I dynamically feed them with 'fr' or 'en' whatever the url is.

$page->of(false); // turn off outputFormatting (if it's not already)
$lg_id = $languages->get('en'); // get the language we want
$body = $page->body->getLanguageValue($lg_id); // get the unformatted value in english

That's many fields to add (30 maybe ?) but for now I'll go with that (on the homepage only).

I'll see if I need to hack this later. I plan to buy Ryan's profiler to see the bottlenecks when I will be live, maybe the homepage won't show up :)

Link to comment
Share on other sites

I'm not sure why a redirect should be bad. I mean lot's of pages to redirect to www.* pages or from http:// to https://, or from an old contents location to a new one. Also regarding the session: Everybody does get a session, which doesn't mean that all of them are actually logged in. 

Link to comment
Share on other sites

Regarding the redirections, SEO purpose apparently, but I'm not expert in this. It also increases the loading time.

I didn't know for the sessions, I think I can use that unexpected feature, thanks :)

Link to comment
Share on other sites

You could also simply add a notification on your homepage, where you suggest users an link to their browsers language instead of forcing a redirect on them. Then you'd only have to get a different language for the notification and everything else can stay automatically translated.

  • Like 1
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...