I18n

what do you think

step 1

web.php

// ‘language’ => ‘de’,

'components' => [





'i18n' => [


    'translations' => [


        'app*' => [


            'class' => 'yii\i18n\PhpMessageSource',


            'basePath' => '@app/messages',


            'sourceLanguage' => 'en',


            'fileMap' => [


                'app' => 'app.php',


                'app/error' => 'error.php',


              .....

Step2

I created the -foldert messages, and I added three folder in messages folder (en, fr, de) and created three file(each contained one)-app.php

‘language’ => ‘de’, when i change ‘language’ => ‘hu’ Works with translation

step 3.

But because I am a beginner I do not know what’s next.

I created two buttons but I can not write the Controller.

<a href="<?php echo Url::to([’’]); ?>">German</a><br>

<a href="<?php echo Url::to([’’]); ?>">Hungarian</a>

My question is how could the button to switch the language,Need to create a Controller, or work without it, it’s how?

Thanks

There’s a chapter in the yii2-cookbook.

Selecting application language

Questions

This is a Controller or not? This can i create in the SiteController?

but the instead of word UrlRule a must use SiteController or not?

class LanguageUrlRule extends UrlRule

{

public function init()


{


    ....


    parent::init();


}

}

This is not a controller. It’s a rule used by the url manager. You may store it in ‘@app/components’ directory.

I have created the folder components, in folder componens a created the LanguageUrlRule.php file.

My question is under namespace what can i write, What to write?

For example:

<?php

namespace app\components;

//use yii\base\BootstrapInterface;

//use yii\base\UrlRule; or what something else?


<?php

namespace app\components;

//use yii\base\BootstrapInterface;

class LanguageUrlRule extends UrlRule

{

public function init()

You have to declare the namespace of your LanguageUrlRule as:




namespace app\components;



because we have to use Yii’s autoloader.

Guide - Class Autoloading

But "use" lines are not required. You could write like:




class LanguageUrlRule extends yii\base\UrlRule



You have to write




use yii\base\UrlRule;



only when you want to write like:




class LanguageUrlRule extends UrlRule



Thank you

I have more a question:

‘urlManager’ => [

        'ruleConfig' =&gt; [


            'class' =&gt; app&#092;components&#092;LanguageUrlRule::className()


        ],





        'enablePrettyUrl' =&gt; true,


        'showScriptName' =&gt; false,


        'enableStrictParsing' =&gt; false,


        'rules' =&gt; [


            'home' =&gt; 'site/index',  


          //  'de/home' =&gt; 'site/index',           


            'contact' =&gt; 'site/contact',


            'about' =&gt; 'site/about',


        ],


        


    ],

What is missing? or something do not need?

The error message is:

Server error

500

Thank you

Could you show us the detailed error information?

This is the error massage


Server error

500

Hide details

The website encountered an error while retrieving http://www.yiiurl.com/. It may be down for maintenance or configured incorrectly.


www.yiiurl.com this is a localdomain setting in Virtualhost!

Maybe something set in Yii2 framework,That more longer say, more details?

Is the site not running in debug mode?

What about ‘runtime/logs/app.log’?

A have setting php.ini

; error_reporting

Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED

This is now the error message:

Fatal error: Class ‘app\components\LanguageUrlRule’ not found in /var/www/yiiurl/config/web.php on line 29

‘urlManager’ => [

‘ruleConfig’ => [

‘class’ => app\components\LanguageUrlRule::className() // 29 line

],

‘enablePrettyUrl’ => true,

‘showScriptName’ => false,

‘enableStrictParsing’ => false,

‘rules’ => [

‘home’ => ‘site/index’,

// ‘de/home’ => ‘site/index’,

‘contact’ => ‘site/contact’,

‘about’ => ‘site/about’,

],

],


app\components\LanguageUrlRule

<?php

namespace app\components;

use yii\base\UrlRule;

class LanguageUrlRule extends UrlRule

{

public function init()


{


    if (&#036;this-&gt;pattern &#33;== null) {


        &#036;this-&gt;pattern = '&lt;language&gt;/' . &#036;this-&gt;pattern;


        // for subdomain it should be:


        // &#036;this-&gt;pattern =  'http://&lt;language&gt;.example.com/' . &#036;this-&gt;pattern,


    }


    &#036;this-&gt;defaults['language'] = Yii::&#036;app-&gt;language;


    parent::init();


}

}

Are your directory name and file name exactly "components" and "LanguageUrlRule.php"?

Please be case sensitive.

‘urlManager’ => [

‘ruleConfig’ => [

‘class’ => app\components\LanguageUrlRule::className()

],

Repleace

‘urlManager’ => [

‘ruleConfig’ => [

‘class’ => ‘app\components\LanguageUrlRule::className()’,

],

Now the error message is:

ReflectionException

Class app\components\LanguageUrlRule::className() does not exist

Check the directory name and the file name.

I guess there’s a mismatch between the file path and the class name.

Thank you

‘urlManager’ => [

        'enablePrettyUrl' =&gt; true,


        'showScriptName' =&gt; false,


        'enableStrictParsing' =&gt; false,


        'rules' =&gt; [


          


            '&lt;language:&#092;w+&gt;/home' =&gt;'site/index',


            '&lt;language:&#092;w+&gt;/about' =&gt; 'site/about',


            '&lt;language:&#092;w+&gt;/contact' =&gt; 'site/contact',


        ],

I have more a question:

How can I translate the URL, home, about and contact worlds?

‘<language:\w+>/Yii::t(‘app’, ‘Home’)’ =>‘site/index’, This solution did not work

‘<language:\w+>/’.Yii::t(‘app’, ‘Home’) =>‘site/index’, This solution did not work

I want to Translate the webpages of my website, at the same time i want to translate the URL.Possible?

The translate be in good working of my website but (in url not),thanks for the help.

Did you give up using "LanguageUrlRule"?

In order to do this kind of trick, you have to use the solution suggested in the cookbook.