How maintane language in application?

Hi everyone, I tried to use the widget for maintaine the language in application as explained in http://www.yiiframew…doc/cookbook/26

I created the files as explained, then I call the widget from a view (i try from layouts/main.php but also from other views) with this instruction:

<?php $this->widget('application.components.Langbox'); ?>

but I have always this error message:

Fatal error: Class 'Langbox' not found in …\Yii\yii\framework\web\CBaseController.php on line 138

I saw that the file name (components/LangBox.php) is written different from class name (Langbox) so I tried to rename it (components/Langbox.php) but the problem persist. I'm not sure if I'm calling the widget in the right way

anyone was able to use it or could help me

thanks

the filename should be in consistent with the class name, in this case you should stick with 'LangBox', so please try

<?php $this->widget('application.components.LangBox'); ?>

I tried, but the message is the same:

Fatal error: Class 'LangBox' not found in …Yii\framework\web\CBaseController.php on line 138

This is what i did:

I put in protected/components the file LangBox.php with this code:

class LangBox extends CWidget

{

    public function run()

    {

        $currentLang = Yii::app()->language;

        $this->render('LangBox', array('currentLang' => $currentLang));

    }

}

in protected/components/views the file LangBox.php with this code:

<?= CHtml::form() ?>

    <div id="langdrop">

        <?= CHtml::dropDownList('_lang', $currentLang, array(

            'en_us' => 'English', 'is_is' => 'Icelandic'), array('submit' => '')) ?>

    </div>

</form>

in protected/components the file MyController.php with this code:

<?php

class MyController extends CController

{

    function init()

    {

        parent::init();

        $app = Yii::app();

        if (isset($_POST['_lang']))

        {

            $app->language = $_POST['_lang'];

            $app->session['_lang'] = $app->language;

        }

        else if (isset($app->session['_lang']))

        {

            $app->language = $app->session['_lang'];

        }

    }

}

?>

and then I modified the file \protected\views\layouts\main.php inserting this row:

<?php $this->widget('application.components.LangBox'); ?>

But it doesn't function, I don't know if I'm missing something else…

Did you set import in config?

It is already set in config.php:

            // autoloading model and component classes

&#039;import&#039;=&gt;array(


	&#039;application.models.*&#039;,


	&#039;application.components.*&#039;,


),

the structure has been created with yiic

Quote

It is already set in config.php:

            // autoloading model and component classes

&#039;import&#039;=&gt;array(





	&#039;application.models.*&#039;,





	&#039;application.components.*&#039;,





),

the structure has been created with yiic

Are you still getting any error message or you just couldn't see the lang box?

(PS.: please make sure your controller class inherit from MyController not CController)

Thanks Will for support, at the end I found where were the problem:

  1. the code reported in the tutorial in components/LangBox.php is not marked with <?php … ?> (it's a stupid thing, but i did cut and paste so I didn't realize it, this was the reason of the error message)

  2. In components/views/langBox.php in the 2 rows where the Chtml is used, "echo" is missed, so the rows should be:

<?= echo CHtml::form() ?>

<?= echo CHtml::dropDownList('_lang', $currentLang, array('en_us' => 'English', 'is_is' => 'Icelandic'), array('submit' => '')) ?>

I report the 2 point also in the comment of the tutorial

thank you

Quote

2) In components/views/langBox.php in the 2 rows where the Chtml is used, "echo" is missed, so the rows should be:

<?= echo CHtml::form() ?>

<?= echo CHtml::dropDownList('_lang', $currentLang, array('en_us' => 'English', 'is_is' => 'Icelandic'), array('submit' => '')) ?>

It looks like your short tags directive in your [tt]php.ini[/tt] is off: [tt]short_open_tag = Off[/tt]

You may consider to change your coding style and avoid this one

Quote

<?= ‘meh’; ?>

<?= ''; ?> is a shorthand for <?php echo ''; ?> it simply "echo" (or "print") a string. My experience says it's a bad practice. Not every (shared) hosting out there have this [tt]On[/tt] by default, so you may run into unnecessary problems when you put your app on production.

Btw, is wrong and equals to