yii2 translations stopped working - yii2

I have changed nothing in my config file, and maybe because of an update or I don't know, I realized that translations doesn't work anymore.
'components' => [
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#app/messages',
'fileMap' => [
'yii' => 'yii.php',
],
],
],
],
I have tried with '*' =>, 'app' =>, 'app*' =>, nothing is working, and I've tried to search but found nothing relevant. There are no error messages, except a warning for rbac-admin:
warning: The message file for category 'rbac-admin' does not exist: .../vendor/mdmsoft/messages/de/rbac-admin.php
however it's there, but it doesn't really matter at the moment.
Can you please point me to the right direction? Thanks a lot!

Related

yii2 url manager rules accept not pattern

I need configure my Yii2 UrlManager rules like this:
change http://domain/site/action to http://domain/action
change http://domain/module/default to http://domain/module
so far what I have done:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<module:(!site)>' => '<module>/default',
'<action:\w+>' => 'site/<action>',
],
],
when I trying access module it return 404. But when I remove '<action:\w+>' => 'site/<action>', access module again will show as module/default page. So how to solve this?
You can try this code
'<action:(about|contact)>' => 'site/<action>',
insted of this
'<action:\w+>' => 'site/<action>',
and it will not work then change sequence of rules
'rules' => [
'<action:\w+>' => 'site/<action>', <-----------it will be first
'<module:(!site)>' => '<module>/default',
],

Using different value for same key in yii2 i18n

i', trying to incorporate multi-language functionality into my project and i have a situation whereby the same key has multiple meaning and i wounder how to fix this for example i have in my project
return[
'Home' => '首页', //meaning ‘Home page’
]`
Now the case is i also have some over words with Home as the key but have another meaning like thus
'Home' => '主队', //meaning ‘Home team’
how can actualize this using the same key "Home" but giving them different meaning on different part of my project
There are two options how to deal with situation like that.
Using categories
The proper way would be using different categories for different meanings. To do that you have to configure the translations for two (or more) different categories, for example like that.
'components' => [
// ...
'i18n' => [
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'fileMap' => [
'app' => 'app.php',
'app/match' => 'match.php',
],
],
],
],
],
Your app.php translation file will look like this
return [
'Home' => '首页', //meaning ‘Home page’
];
And your match.php translation file will look like this:
return [
'Home' => '主队', //meaning ‘Home team’
];
Then you will call translations like this where the meaning is home page:
Yii::t('app', 'Home');
And like this where the meaning is home team:
Yii::t('app/match', 'Home');
If you are using yii message command to generate translation files you might need to set the fileMap in it's config to make sure all files are generated properly.
Forcing translations for source language
This way is more like hack than proper solution. You can force the i18n component to translate text even if the source and app language are same. You can use that to have two different keys that are same in the english translations but different in other.
The configuration would look like:
'components' => [
// ...
'i18n' => [
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'forceTranslation' => true,
'fileMap' => [
'app' => 'app.php',
],
],
],
],
],
The english app.php translation file:
return [
'Home page' => 'Home',
'Home team' => 'Home',
];
The chinese app.php translation file:
return [
'Home page' => '首页',
'Home team' => '主队',
];
You will use Yii::t('app', 'Home page') or Yii::t('app', 'Home team') in your code.

Yii2 - UrlManager and params with hypens

I have the following URLs:
http://test.local/index.php?r=site%2Findex&slug=blog-detail
http://test.local/index.php?r=site%2Findex&slug=blog
http://test.local/
I want to get:
http://test.local/blog
http://test.local/blog-detail
http://test.local/
I am sending all requests to SiteController::actionIndex($slug), I am using Basic Application Template.
So far, I have been able to hide index.php and site/index.
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<slug\w+>' => 'site/index',
],
]
But it seems \w+ does not match strings with -. Also if slug is empty it should show: http://test.local/.
\w does not match -. You need to use [\w\-] instead. And + requires at least one char in your case. You should use * instead:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<slug:[\w\-]*>' => 'site/index',
],
]
What you are trying to make is make specific url based on GET param. With the following example if the user enters url test.local/Some-nice-article then the SiteController::actionIndex($slug) function will get the param.
'urlManager' => [
'pattern' => '<slug>',
'route' =>'site/index',
'ecnodeParams' => false,
//class => any\custom\UrlRuleClass,
//defaults => []
]
Or you want another url to specify whether it is detailed view? You can do it this way:
'urlManager' => [
'pattern' => '<slug>-detail',
'route' =>'site/detail',
'ecnodeParams' => false,
//class => any\custom\UrlRuleClass,
//defaults => []
]
In this example, if the users puts the string '-detail' at the of the slug, then it will parse the route SiteController::actionDetail($slug) to the request.
Please note that if you did not yet, enable prettyUrls in the config file
You can find a little more about this topic in this answer or in the Yii2 definitive guide

Yii2: disable warning log message "Session is already started"

There are a lot of messages in my 'app.log' file with content:
2018-03-28 12:23:55 [66.70.168.171][-][-][warning][yii\web\Session::init] Session is already started
Question: how to prevent this warning message from writing into log file?
I've tried to add 'except' element in 'targets':
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
'except' => [
'yii\web\Session', //here it is
],
],
But no success.
if your exception does not use a wildcard, it will attempt to match an exact log category name
so you either add exception for an exact name (your case yii\web\Session::init)
or add a wildcard at the end yii\web\Session:* to filter all Session messages
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
'except' => [
'yii\web\Session:*', // Excludes all session messages
// or
'yii\web\Session::init', // Exclude only session init
],
]
another solution:
yours_project -> vendor -> yiisoft -> yii2 -> web -> Session.php
comment out line 107, like this
if ($this->getIsActive()) {
//Yii::warning('Session is already started', __METHOD__);
$this->updateFlashCounters();
}

YII2 How to allow debug/default/toolbar in accesscontrol

I have the following configuration in the web.php file to force users to login first before using the app.
'as access' => [
'class' => \yii\filters\AccessControl::className(), //AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error'],
'allow' => true,
],
[
'actions' => ['logout', 'index'], // add all actions to take guest to login page
'allow' => true,
'roles' => ['#'],
],
],
],
However I get a Forbidden (#403) error in the http://localhost/yii2/debug/default/toolbar?tag=58759099581f2
How to allow in that in the rules?
First of all, this config is incorrect. This part:
[
'actions' => ['logout', 'index'], // add all actions to take guest to login page
'allow' => true,
'roles' => ['#'],
],
will additionally allow only logout and index actions to authenticated users. It needs to be changed to:
[
'allow' => true,
'roles' => ['#'],
],
to allow access to the entire site. Then you can customize access further in AccessControl or actions of specific controllers. So debug is not the only forbidden page in your case.
I think it was copy pasted from this answer to related question here on SO.
And by the way debug is already enabled in application config in basic app:
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
// uncomment the following to add your IP if you are not connecting from localhost.
//'allowedIPs' => ['127.0.0.1', '::1'],
];
// Below Gii is enabled too, code is omitted for brevity
}
So when user is authenticated, you will have access to debug module without any problems.
Note: With this configuration login and error actions of every controller are allowed to non-authenticated users. Be careful with that. There is a chance of actions with similar names exist in other controllers.
Update: Actually you can go further and make this solution more flexible with $matchCallback:
'as access' => [
'class' => \yii\filters\AccessControl::className(),
'rules' => [
[
'matchCallback' => function ($rule, $action) {
$allowedControllers = [
'debug/default',
];
$allowedActions = [
'site/login',
'site/error',
];
$isAllowedController = in_array($action->controller->uniqueId, $allowedControllers);
$isAllowedAction = in_array($action->uniqueId, $allowedActions);
return $isAllowedController || $isAllowedAction;
},
'allow' => true,
],
[
'allow' => true,
'roles' => ['#'],
],
],
],
Place fully allowed controllers in $allowedControllers list (prefix it with module name if it's inside a module) to allow them completetely (allow all actions).
Place allowed actions in $allowedActions list (prefix it with controller name and with module name if it belongs to a module).
That way you can have full access to debug module on local server on every page (including login and error) which can be useful.
Also this prevents from action names coincidence from different modules / controllers.
You have to enable the toolbar action web.php config file:
'rules' => [
[
'actions' => ['login', 'error', 'toolbar'],
'allow' => true,
],