rkatona
(K Robert)
1
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
softark
(Softark)
2
There’s a chapter in the yii2-cookbook.
Selecting application language
rkatona
(K Robert)
3
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();
}
}
softark
(Softark)
4
This is not a controller. It’s a rule used by the url manager. You may store it in ‘@app/components’ directory.
rkatona
(K Robert)
5
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()
…
softark
(Softark)
6
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
rkatona
(K Robert)
7
Thank you
I have more a question:
‘urlManager’ => [
'ruleConfig' => [
'class' => app\components\LanguageUrlRule::className()
],
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
'home' => 'site/index',
// 'de/home' => 'site/index',
'contact' => 'site/contact',
'about' => 'site/about',
],
],
What is missing? or something do not need?
The error message is:
Server error
500
Thank you
softark
(Softark)
8
Could you show us the detailed error information?
rkatona
(K Robert)
9
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?
softark
(Softark)
10
Is the site not running in debug mode?
What about ‘runtime/logs/app.log’?
rkatona
(K Robert)
11
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 ($this->pattern !== null) {
$this->pattern = '<language>/' . $this->pattern;
// for subdomain it should be:
// $this->pattern = 'http://<language>.example.com/' . $this->pattern,
}
$this->defaults['language'] = Yii::$app->language;
parent::init();
}
}
softark
(Softark)
12
Are your directory name and file name exactly "components" and "LanguageUrlRule.php"?
Please be case sensitive.
rkatona
(K Robert)
13
‘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
softark
(Softark)
14
Check the directory name and the file name.
I guess there’s a mismatch between the file path and the class name.
rkatona
(K Robert)
15
Thank you
‘urlManager’ => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
'<language:\w+>/home' =>'site/index',
'<language:\w+>/about' => 'site/about',
'<language:\w+>/contact' => '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.
softark
(Softark)
16
Did you give up using "LanguageUrlRule"?
In order to do this kind of trick, you have to use the solution suggested in the cookbook.