mikehaertl / localeurls

Automatic locale/language management for URLs
10 stars 3 forks source link

Chtml helper remove language code from url #3

Closed izemize closed 11 years ago

izemize commented 11 years ago

Hy.

Nice job.

CHtml::link helper remove language code from the url, if default language same as selected language.

The sample widget does not work if default language is en.

mikehaertl commented 11 years ago

Thanks, but i'm not sure if i understand. Can you give an example that does not work? Maybe also share your configuration.

izemize commented 11 years ago
<?php
class LanguageSelector extends CWidget
{
    public function run()
    {
        Yii::app()->clientScript->registerCssFile(Yii::app()->getTheme()->baseUrl.'/css/LanguageSelector.css');
        $app = Yii::app();
        $controllerId = $app->controller->id;
        $actionId = $app->controller->action->id;
        $params = $_GET;
        $languages = $app->request->languages;
        $language = $app->language;

        array_unshift($params,"$controllerId/$actionId");

        echo CHtml::openTag('div',array('class'=>'LanguageSelector'));
        echo CHtml::openTag('ul');

        foreach($languages as $lang)
        {
            if($lang === $language)
            {
                continue;
            }

            $params['language'] = $lang;

            echo CHtml::openTag('li');
            echo CHtml::link(CHtml::image('/icons/flags/'.($lang == 'en' ? 'us' : $lang).'.png',$lang),$params);
            echo CHtml::closeTag('li');
        }

        echo CHtml::closeTag('ul');
        echo CHtml::closeTag('div');
    }
}

'language'=>'en',

'request'=>array(
    'enableCsrfValidation'=>true,
    'csrfTokenName'=>'z3us',
    'enableCookieValidation'=>true,
    'class'=>'ext.localeurls.LocaleHttpRequest',
    'languages'=>array('en','hu','sk'),
),

'urlManager'=>array(
    'class'=>'ext.localeurls.LocaleUrlManager',
    'urlFormat'=>'path',
    'showScriptName'=>false,
    'rules'=>array(
        '<view:\w+>'=>'site/page',
        '<controller:\w+>/<id:\d+>'=>'<controller>/view',
        '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
        '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
    ),
),

wrong html output here:

<div class="LanguageSelector">
<ul>
<li><a href="/site/index"><img src="/icons/flags/us.png" alt="en"></a></li>
<li><a href="/hu/site/index"><img src="/icons/flags/hu.png" alt="hu"></a></li>
</ul>
</div>

if i change language to 'ru' works fine.

mikehaertl commented 11 years ago

You mean the link /site/index should be /en/site/index? If so, then you should set redirectDefault to true in your request configuration. Please check the README for an explanation. And let me know, if you think, the docs can be improved in this regard.

mikehaertl commented 11 years ago

Oh, now i think i see your problem: You can't switch back to default language anymore. Ok, let me check this.

izemize commented 11 years ago

seems works, thanks :)