How to override needed messages from core messages in yii2 - internationalization

In yii2, i want to override some core messages translation, example:
in #yii/messages/vi/yii.php, has key => translated message:
'Update' => 'Sửa'
but in my application, i want to change this as:
'Update' => 'Cập nhật'
I have created the file: #app/messages/vi/yii.php, has only one the message which need to override:
return [
'Update' => 'Cập nhật'
];
in my main.php config, I added this to components:
'i18n' => [
'translations' => [
'yii' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#app/messages'
],
],
],
It works but just only for the override messages, others of core don't work.

I think you should copy yii.php from core to #common/messages/<your language>/yii.php and edit it. It should be work stable.

Try somehing like this
'i18n'=>[
'yii'=>[
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => "#vendor/yiisoft/yii2/messages",
'sourceLanguage' => 'en_US', // put your language here
'fileMap' => [
'yii'=>'yii.php',
]
]
]
],

Might be outdated thread but I'm putting this in case someone hits this in a search results as I did.
This works out for me :
// get the default translation file for desired language
$yii = require \Yii::getAlias('#vendor/yiisoft/yii2/messages/az/yii.php');
// just override what ever you want
return yii\helpers\ArrayHelper::merge( $yii,
[
'update' => 'myUpdateString',
// ... and so on
]);

Related

How to start using Slug URLs without breaking old in Yii2

i want to rewrite my URLs from
/view?id=100
to
/view/100-article-title
But the site already has several thousand search pages. As far as I know, such a change can be bad for SEO. Is there a way to keep the old URLs working when switching to the new routing.
*I am creating links as follows:
Url::to(['view', 'id' => $item->id])
Is it possible to create links further through ID?
you can create getLink() function on your model and use it on. Then, when function runs you can check id if id <= 100 then return Url::to(['view', 'id' => $item->id]) else return Url::to(['view', 'id' => $item->id, 'slug' => $this->slug])
And add route with slug on your main.php
Look at my example.
First config urlManager in config.php in 'app/config' folder. In my case look like:
'components' => [
...
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
''=>'home/index',
'<slugm>-<id:\d+>-' => 'home/view',
],
],
.
...
],
and create link as fallow:
Url::to(['/home/view', 'id' => $model->home_id, 'slugm' =>$model->title_sr_latn])
and finaly url look like:
https://primer.izrada-sajta.rs/usluge-izrade-sajtova-1-

Yii2 Restful API - add possibility to auth with session

In my Restful API project I use Bearer Token Authentication.
But now there is need to use Session Authentication in only one Controller to perform special actions with files. But I don't really understand how I should change behaviors method for that. What I have done,
'enableSession' => true
My behaviors method looks like:
public function behaviors() {
$behaviors = parent::behaviors();
unset($behaviors['authenticator']);
$behaviors['authenticator'] = [
'class' => HttpBearerAuth::className(),
'except' => ['options'],
];
$behaviors['access'] = [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'actions' => [
'options',
],
],
[
'actions'=>['content'],
'allow' => true,
'roles' => ['admin'],
]
],
];
return $behaviors;
}
I think there should be some changes in authenticator settings, use something else except HttpBearerAuth or may be something else, please help

CakePHP 3 - How to apply validation conditionally?

Trying to restrict a compareWith to just on create
$validator->add('password', [
'compare' => [
'rule' => ['compareWith', 'password_confirmation']]]);
I cant seem to work out how to do it added 'create' to the end like
$validator->add('password', [
'compare' => [
'rule' => ['compareWith', 'password_confirmation']]],'create');
in fact i have tried the 'create' in many places either build errors or still validating on an edit
Appreciate any help
Try this:
$validator->add("password", "compare", [
"rule" => ["compareWith", "password_confirmation"],
"message" => __("Password and password confirmation fields don't match."),
"on" => "create"
]);
This will certainly work.
Hope this helps.
Peace! xD

Installing user-management for Yii2.0

I've been trying to install user-management for Yii2.0, but getting ReflectionException while loading the page. I have attached the error page and directory structure below.
and the file path is as shown below.
I've searched a lot to find out the reason for this, but nothing worked out. can someone tell me what am I missing here to get it work. looks like the user-management installation documentation has some flaws. It is not clear enough to understand. Hope to get the steps to install. Thanks
Here is my console/web.php
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'gAry7SfUr0oOjNQDqItsobmGBcJajQoW',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
//'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'class' => 'app\webvimark\modules\user-management\components\UserConfig',
// Comment this if you don't want to record user logins
'on afterLogin' => function($event) {
\webvimark\modules\user-management\models\UserVisitLog::newVisitor($event->identity->id);
}
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => require(__DIR__ . '/db.php'),
],
'modules'=>[
'user-management' => [
'class' => 'webvimark\modules\user-management\UserManagementModule',
// 'enableRegistration' => true,
// Here you can set your handler to change layout for any controller or action
// Tip: you can use this event in any module
'on beforeAction'=>function(yii\base\ActionEvent $event) {
if ( $event->action->uniqueId == 'user-management/auth/login' )
{
$event->action->controller->layout = 'loginLayout.php';
};
},
],
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
return $config;
It seems to have a slight difference with the expected configuration for this extension.
Use this
'class' => 'webvimark\modules\UserManagement\components\UserConfig',
ie UserManagement instead of user-management is a configuration path and not a route

How to use DbMessageSource in yii2

i am using yii2 advanced template.and i dont understand how to use DbMessageSource.i read the guide and i created two tables source_message and message and i wrote in my common/config/main.php file this code
'*'=> [
'class' => 'yii\i18n\DbMessageSource',
'sourceMessageTable'=>'{{%source_message}}',
'messageTable'=>'{{%message}}',
'enableCaching' => true,
'cachingDuration' => 3600
],
and what i have to write in brackets when i using <?= Yii::t()?>
P.S. i am also changed language in my config.
P.P.S. i generated models and cruds for this tables
try this:
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\DbMessageSource',
'forceTranslation'=>true,
]
],
],
set parameter forceTranslation as true. This trick helps me.

Resources