Override translation path of module on yii2 - php

Suppose I installed module Foo form a repository with composer. The module structure is like this:
- Foo
|- models
|- controllers
|- views
|- messages
|- config
Messages folder of Foo contains translation files of module. Now I want override some translation strings of Foo. From Yii2 i18n Documentation I tried to use fileMap property on configuration of translation component to map bar category to bar.php (instead of reading from app\modules\Foo\messages), but it does not have any effect on translations. My i18n component configuration is:
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\PhpMessageSource',
'fileMap' => [
'bar' => 'bar.php'
],
],
],
],
How do i achieve my goal?

If you are wanting to have translations for each module contained within the module, then you need to register the translations for that module. It can't be done simply from the config file. You probably already have this in your module file,, I just include for completeness. The code is copied from the documentation, and needs to be in your module file, so in app/modules/Foo.php
<?php
namespace app\modules\foo;
use Yii;
class Module extends \yii\base\Module
{
public $controllerNamespace = 'app\modules\foo\controllers';
public function init()
{
parent::init();
/** Register custom translations for this module **/
$this->registerTranslations();
}
public function registerTranslations()
{
/**This registers translations for the Foo module **/
Yii::$app->i18n->translations['modules/foo/*'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en-US',
'basePath' => '#app/modules/foo/messages',
/**Tells yii where to find the translations for validation and form categories **/
'fileMap' => [
'modules/foo/validation' => 'validation.php',
'modules/foo/form' => 'form.php',
...
],
];
}
public static function t($category, $message, $params = [], $language = null)
{
return Yii::t('modules/users/' . $category, $message, $params, $language);
}
}
In your case it doesn't look like you need to provide file mapping.You could simply use this format for your files
[[basePath]]/LanguageID/CategoryName.php
Unfortunately I can't seem to find a list of the available categories.
If you then want to override some of the module translations you will need to specify the category to be used, like this in your config file. It specifically overrides the modules/foo/bar category.
'i18n' => [
'translations' => [
'modules/foo*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#app/messages',
],
],
],
Your translation file needs to follow a folder structure like that in the translation description, so in the above example it would be
app/messages/ [language code] /modules/foo/bar.php
Otherise, you can use fileMap to map to different locations, like if your bar.php file is in app/messages/[language code]
'fileMap' => [
'modules/foo/bar' => 'bar.php'
]

Related

How to add a custom function to smarty extension yii2

i have a little problem with smarty extension for yii2.
I've created a new smarty function, and i've added the code into this file:
backend/vendor/yiisoft/yii2-smarty/src/Extension.php
public function __construct($viewRenderer, $smarty)
{
//other code
/* CUSTOM FUNCTION REGISTER */
$smarty->registerPlugin('function', 'test', [$this, 'functionTest']);
}
//this is the custom function
public function functionTest($params, $template){
return "Test custom funcion";
}
And i can use this custom function into my template like this {test} and all works fine.
Today i have update the yii2 to the 2.0.20 version, and obviously the Extension.php file was replaced, so i can't access anymore to the custom function.
My question is: How i can add a custom function for smarty in yii2?
I'll set the config array in this way:
//this is in backend/config/main.php
'view' => [
'renderers' => [
'tpl' => [
'class' => 'yii\smarty\ViewRenderer',
'pluginDirs' => ['#backend/saSmartyPlugin'],
'widgets' =>[
'functions' => [['test' => 'test'], ],
],
//'cachePath' => '#runtime/Smarty/cache',
],
],
],
and the into saSmartyPlugin folder i insert my test.php file like this:
<?php
class Test{
function functionTest($params, $template){
return "Test custom funcion";
}
}
But i get this error:
Smarty: Undefined class 'test' in register template class
I agree with Muhammad Omer Aslam, you should extend from backend/vendor/yiisoft/yii2-smarty/src/Extension.php in order to create Any new methods and be able to use them after update. After that you just write in your config file path to your extended class.
I'll find a solution thinking about #MuhammadOmerAslam and #SergheiLeonenco suggest me.
I write this answer for anyone who has this problem.
First i create my php file Test.php and i extend the Extension class of Smarty
namespace common\components;
use yii\smarty\Extension;
class Test extends Extension{
public function __construct($viewRenderer, $smarty){
parent::__construct($viewRenderer, $smarty);// call parent construct
$smarty->registerPlugin('function', 'bread', [$this, 'functionBreadcrumbs']);//register my custom function
}
//My custom function
function functionTest($params, $template){
return "Test custom funcion";
}
And i save this file into common/components/
After that i have modified my config.php file
'view' => [
'renderers' => [
'tpl' => [
'class' => 'yii\smarty\ViewRenderer',
'extensionClass' => 'common\components\Test'
],
],
],
],

Yii2 REST API as Module route configuration

I have an existing Yii2 application and have been trying to implement a REST API as an additional module (Maybe a module isn't the correct way to go about this?) But I'm having some trouble configuring the route structures. It doesn't quite work and doesn't follow the expected results, based of the following guide.
I've built an additional module that looks like this:
module
api
controllers
UserController.php
Module.php
UserController.php
<?php
namespace app\modules\api\controllers;
use yii\rest\ActiveController;
class UserController extends ActiveController
{
public $modelClass = 'app\models\User';
}
Module.php
<?php
namespace app\modules\api;
/**
* onco module definition class
*/
class Module extends \yii\base\Module
{
public $defaultController = 'user';
/**
* #inheritdoc
*/
public $controllerNamespace = 'app\modules\api\controllers';
/**
* #inheritdoc
*/
public function init()
{
parent::init();
// custom initialization code goes here
}
}
In my config file I have the added following:
'request' => [
...
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
],
...
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false, // have tried as true also
'rules' => [
...
['class' => 'yii\rest\UrlRule', 'controller' => '\app\modules\api\controllers\user'],
],
],
...
'modules' => [
...
'api' => [ // module for RESTful API
'class' => 'app\modules\api\Module',
],
]
When I run the following urls through postman I get the following:
http://localhost/site1/web/api/users -> 404
http://localhost/site1/web/api/users/index -> 404
http://localhost/site1/web/api/user/index -> returns json repsonse
http://localhost/site1/web/api/user/2 -> 404
I'm unsure as to why the predicted routes of noted in the docs as:
Trying it Out With the above minimal amount of effort, you have
already finished your task of creating the RESTful APIs for accessing
the user data. The APIs you have created include:
GET /users: list all users page by page;
HEAD /users: show the overview information of user listing;
POST /users: create a new user;
GET /users/123: return the details of the user 123;
HEAD /users/123: show the overview information of user 123;
PATCH /users/123 and PUT /users/123: update the user 123;
DELETE /users/123: delete the user 123;
OPTIONS /users: show the supported verbs regarding endpoint /users;
OPTIONS /users/123: show the supported verbs regarding endpoint /users/123
What have I likely done wrong in this setup? Is there a better way to implement an API into an existing website, whilst maintaining DRY practices?
try this:
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['api/user'],
]
]
],
...
'modules' => [
...
'api' => [
'basePath' => '#app/modules/api',
'class' => 'app\modules\api\Module',
],
]
Also be sure to implement prettyUrl's related server server configs.

translation view for Yii app not found

I set up an i18n page, where I translate messages using yii\i18n\PhpMessageSource with the following config part:
(config/web.php)
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['debug'],
'language' => 'de-DE',
'components' => [
'i18n' => [
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#app/messages',
'fileMap' => [
'app' => 'app.php',
],
'forceTranslation' => true,
],
],
]]
...byt the way: this works fine.
For a kind of static content -like an imprint-, I like to use an complete translated view.
So I added some sub-directories in the views - folder, with the view insight:
#app/views/myController/de-DE/myview.php
#app/views/myController/en-US/myview.php
So my action does the following:
public function actionImpressum() {
\Yii::$app->language = 'en-US';
return $this->render('myview');
}
...which results in an invalid parameter
yii\base\InvalidParamException: The view file does not
exist: /path/to/my/app/views/myCtrl/myview.php
This error is valid, because there is no view at this path. But shouldn't the render() method use the path for the translation views, like:
/path/to/my/app/views/myCtrl/en-US/myview.php ??
Is there something I forgot?
Thank you.
Since there is no sourceLanguage set in your configuration I assume you have not changed it and the source language of your app is en-US (default one).
When the source language is the same as target language view is not translated.
See documentation about this:
Note: If the target language is the same as source language original view will be rendered regardless of presence of translated view.
So for en-US it looks for /path/to/my/app/views/myCtrl/myview.php file.

Yii2: $loadedModules does not show all modules

Created module "forum" - exactly as written here. Then created nested module "admin":
//"Module.php" in '#app/modules/forum'
namespace app\modules\forum;
class Module extends \yii\base\Module {
public function init() {
parent::init();
\Yii::configure($this, require(__DIR__ . '/config.php'));
$this->modules = [
'admin' => [
// here is my nested module
'class' => 'app\modules\forum\modules\admin\Module',
],
];
}
}
Also created a non-nested module "games" (in the same way) and wrote in "web.php" (main config-file):
'modules' => [
'forum' => [
'class' => 'app\modules\forum\Module',
],
'games' => [
'class' => 'app\modules\games\Module',
],
'admin' => [
'class' => 'app\modules\forum\modules\admin\Module',
],
],
But when I tried to output:
// codeline is written in application view, not in module view
var_dump(array_keys(\Yii::$app->loadedModules));
I saw only these modules:
array(4) {
string(19) "yii\web\Application"
string(16) "yii\debug\Module"
string(14) "yii\gii\Module"
string(24) "app\modules\forum\Module"
}
"Games" and nested "admin" modules are absent! Although doc says:
$loadedModules property keeps a list of loaded modules, including both direct children and nested ones, indexed by their class names.
But I could get only "forum" myself-created module. What am I understanding wrong?
As documentation says:
$loadedModules property keeps a list of loaded modules
which mean that it keeps modules that loaded in current request. E.g. if you are on module's page example.com/forum/ it will contain app\modules\forum\Module but if you on example.com/site where site is controller's name $loadedModules will contain only modules that are set in $bootstrap config property.
To get list of all modules call Yii::$app->modules. Note that $loadedModules contains app itself since it extends Module class. Yii::$app->modules contains all modules from app config modules property.

Override Yii2 assetManager config in controller

I use yii-jui to add some UI elements in the views such as datePicker. In the frontend\config\main-local.php I set the following to change the theme used by the JqueryUI:
$config = [
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'gjhgjhghjg87hjh8878878',
],
'assetManager' => [
'bundles' => [
'yii\jui\JuiAsset' => [
'css' =>
['themes/flick/jquery-ui.css'],
],
],
],
],
];
I tried the following to override this configuration item in the controller actions method:
public function actions() {
Yii::$app->components['assetManager'] = [
'bundles' => [
'yii\jui\JuiAsset' => [
'css' =>
['themes/dot-luv/jquery-ui.css'],
],
],
];
return parent::actions();
}
Also I tried to set the value of Yii::$app->components['assetManager'] shown above to the view itself (it is partial view of form _form.php) and to the action that calls this view (updateAction). However, all this trying doesn't be succeeded to change the theme. Is there in Yii2 a method like that found in CakePHP such as Configure::write($key, $value);?
You should modify Yii::$app->assetManager->bundles (Yii::$app->assetManager is an object, not an array), e.g.
Yii::$app->assetManager->bundles = [
'yii\jui\JuiAsset' => [
'css' => ['themes/dot-luv/jquery-ui.css'],
],
];
Or if you want to keep other bundles config :
Yii::$app->assetManager->bundles['yii\jui\JuiAsset'] = [
'css' => ['themes/dot-luv/jquery-ui.css'],
];
You are going about this all wrong, you want to change the JUI theme for 1 controller alone because of a few controls. You are applying 2 css files to different parts of the website that have the potential to change styles in the layouts too. The solution you found works but it is incredibly bad practice.
If you want to change just some controls do it the proper way by using JUI scopes.
Here are some links that will help you:
http://www.filamentgroup.com/lab/using-multiple-jquery-ui-themes-on-a-single-page.html
http://jqueryui.com/download/
In this way you are making the website easier to maintain and you do not create a bigger problem for the future than you what solve.

Categories