capsula
capsula

Reputation: 498

Cakephp 2.0 dynamic multilanguage website (best practice issue)

I have two domains pointing to the same cakephp app.

I read the domain on core.php and depending on a condition, I set the display language

        if(strstr($_SERVER['SERVER_NAME'],'por')>-1 ){
            Configure::write('Config.language', 'por');     
        }else{
            Configure::write('Config.language', 'spa'); 
        }

I'm not sure if this is the best practice since this doesn't use sessions. Moreover, I'm not sure how this may work with high concurrency, I'm guessing some users may experience language flickering.

Upvotes: 0

Views: 1268

Answers (2)

Chuck Burgess
Chuck Burgess

Reputation: 11575

Here is how I would do it.

Configure::write('Config.language', 'eng');
Configure::write('Config.supported_languages', array(
    'en-US' => 'eng',
    'en' => 'eng',
    'es-ES' => 'esp',
    'es' => 'esp',
));

$supported_languages = Configure::read('Config.supported_languages');
$accepted_languages = split(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($accepted_languages as $language) {
    $language_data = split(';', $language);
    if (in_array($language_data[0], array_keys($supported_languages))) {
        Configure::write('Config.language', $supported_languages[$language_data[0]]);
        break;
    }
}

This will default the language to English (or what ever language you want to set as default). This will allow the language to change based on the users browser.

Upvotes: 2

nIcO
nIcO

Reputation: 5001

If the language of your website do depend on the server name, I don't think it is a bad practice.

Configure is a singleton class that is instanciated only once, but for each HTTP request as it is PHP. Unlike some applications servers such as Tomcat or even ASP.Net, it does not configure the 'application' for all users on your website, but only for the current request. So your users won't experience any language flickering.

That said I would not put this test in core.php, but in bootstrap.php as it is intended for this kind of things.

Upvotes: 1

Related Questions