<?php
//make sure the plugin does not run on the mgr context (the manager)
if ( $modx->context->get( 'key' ) != 'mgr') {
/**
* grab the current context from the request "c" parameter (or context_param_alias from system settings)
* the alternative .htaccess friendly URL rewrite rule must be activated
*/
$cntxt_param = $modx->getOption('context_param_alias', null, 'c');
if ( isset( $_REQUEST[$cntxt_param] ) && $_REQUEST[$cntxt_param] != '' ) {
switch ( $_REQUEST[$cntxt_param] ) {
case 'en':
//switch the context
$modx->switchContext('web');
//set the cultureKey
$modx->setOption('cultureKey', 'en');
break;
case 'fr':
$modx->switchContext('fr');
$modx->setOption('cultureKey', 'fr');
break;
case 'de':
$modx->switchContext('de');
$modx->setOption('cultureKey', 'de');
break;
case 'it':
$modx->switchContext('it');
$modx->setOption('cultureKey', 'it');
break;
default:
switch ( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) {
case 'fr':
$modx->switchContext('fr');
$modx->setOption('cultureKey', 'fr');
break;
case 'de':
$modx->switchContext('de');
$modx->setOption('cultureKey', 'de');
break;
case 'it':
$modx->switchContext('it');
$modx->setOption('cultureKey', 'it');
break;
default:
$modx->switchContext('web');
//set the cultureKey
$modx->setOption('cultureKey', 'en');
break;
}
}
}
}
I think mindeffects solution is a bit more elegant. If the user has already requested a specific language (i.e. they written somesite.com/FR) they should go to that language no matter what, but if they haven't selected their language setting should be applied. If you combine it I think this should work:Thank you! ;-)
Thank you! ;-)You're right - I didn't read your code properly - was trying to make it applicable to Mienno's case, but it already is
But that is just how my code works: See, if there is a requested language in the URI and if not, look what the browser tells you and reakt. Of nothing works, pick a default.
Just curious. Why set the cultureKey in a script? In my opinion it is easier and better to set this option on context level. Also the set_locale option could be set on the context itself.Not sure, who your are asking... ;-) Anyway. I didn't use contexts in my example. I was perfectly happy with "the language in the tree" solution – since I did not use Babel but my own scripts. I wanted to have independent language branches, without a 1:1 realtion.
I was just adding some remarks on the use of setting the cultureKey used in the scripts in this topic each time a page wil be loaded while it can be so much easier to handle this setting as option on the context.The performance shouldn't be that much worse with this custom plugin. But that is indeed the price I have to pay for the lexicon usage.
{:o)
Thanks so much guys it work now great!Thank you very much, Sir, for your kind words.
What does the generated URL look like? Does it contain the "xy" in "...com/xy/promo.html"?
http://mysite.com/[[!++cultureKey]]/promo.html
But this will always go to the default English...