How to configure symfony2 to use custom translation file names convention? - php

According to official documentation:
The filename of the translation files is also important: each message file must be named according to the following path: domain.locale.loader:
I must to name all files such as messages.en_US.php, navigation.en_US.php, admin.en_US.php etc. and place in some folder.
i18n\
admin.en.php
messages.en.php
navigation.en.php
admin.fr.php
messages.fr.php
navigation.fr.php
But my project structure should follow the following structure:
i18n\
en_US\
admin.php
messages.php
navigation.php
fr_FR\
admin.php
messages.php
navigation.php
So how to configure symfony2 to support my structure and use translations in common way:
echo $this->get('translator')->trans('hello', array(), 'messages');

It is possible, you have to load resources calling the addResource() method.
$translator->addLoader('php', new PhpFileLoader());
//Inside a loop:
$translator->addResource('php', 'path/to/messages.php', $locale);
Here is the documentation
https://symfony.com/doc/4.1/components/translation.html#loading-message-catalogs

This is not possible. If you want to use Symfony, stick to the conventions from Symfony!
To be correct: It's not possible in a simple way. You would need to write your own translator component and throw the translator component from Symfony away..

Related

How do I read from a yml file in a controller in symfony2?

I have a random name.yml file inside lets say my src/AppBundle/Controller folder. The content inside the YML file is simple:
name: utkarsh
I need to access this from my controller, and have tried to fetch them with
$name = $this->getName('name');
from within my controller file. When I try this, I get this error message:
Attempted to call an undefined method named "getName" of class "AppBundle\Controller\DefaultController".
What is the correct way to do this?
If it's just going to be a general file, and wouldn't better be part of the framework's configuration (for example, in the parameters.yml file), you can parse/read the file using the Yaml component, which is already used by the Symfony framework.
use Symfony\Component\Yaml\Yaml;
$values = Yaml::parse(file_get_contents('/path/to/name.yml'));
echo $values['name'];
If the file you are loading is in the same directory as the code that is running it, you can use the PHP Automagic constants - __DIR__, file_get_contents(__DIR__ . '/name.yml'), or use it as a relative base to work from.
Don't just put your YML file in the Controllers folder. Instead, move your YML file into your bundles' config directory. See below,
AppBundle\Resource\config\abc.yml
Then, import this file in your services.yml file. See below,
imports:
- { resource: abc.yml }
And, make sure you abc.yml file will have a structure like this,
parameters:
name: utkarsh
Finally, you can access this parameter inside the Controller by calling like this,
$this->container->getParameter('name');
I hope this helps :)
Cheers!

Override JSON view in RequestHandler in CakePHP

I want to rewrite JSON View in the RequestHandler. So there's a file project_root/lib/JsonView.php. What I want to do is to
Import the JsonView.php file in another file in project_root/app/View/CustomJsonView.php. (I think I could use App:import, would it be right ?)
Choose this file as the custom in requestHandler like this:
public $components = array('RequestHandler' => array( 'viewClassMap' => array('json' => '/right/way/to/this/file/CustomJsonView', )));
But how do I write the right way for this file ?
I also saw this one https://book.cakephp.org/2.0/en/core-libraries/components/request-handling.html#RequestHandlerComponent::viewClassMap
but there is no explanation about the right paths to the file. My CakePHP version is 2.4.4 .
You are not supposed to pass full paths, but "short classnames", just like shown in the linked example, where ApiKit.MyJson refers to the MyJsonView view class in the ApiKit plugin, which could be located in app/Plugin/ApiKit/View/MyJsonView.php.
If you follow the conventions and place your CustomJsonView class in app/View/CustomJsonView.php as shown in the docs, you then just pass CustomJson as the short classname in the request handlers viewClassMap option.
Whether you use App::import() or just require to include the /lib/JsonView.php file, is up to you, both works. In any way you must make sure that whatever you are importing there doesn't clash with existing class names (JsonView is a kinda reserved name as it already exists in the core), and that it is either following the CakePHP view class naming conventions, or you must extend it.
See also
Cookbook > Views > Creating your own view classes
Cookbook > Core Libraries > General Purpose > App Class> Loading Vendor Files

How to load view from alternative directory in Laravel 4

In my Laravel 4 application's root directory, I have a folder themes. Inside the themes folder, I have default and azure.
How can I access view from this themes/default folder in a specific route.
Route::get('{slug}', function($slug) {
// make view from themes/default here
});
My directory structure:
-app
--themes
---default
---azure
I need to load views from localhost/laravel/app/themes/default folder. Please explain this.
This is entirely possible with Laravel 4. What you're after is actually the view environment.
You can register namespace hints or just extra locations that the finder will cascade too. Take a look here
You'd add a location like so:
View::addLocation('/path/to/your/views');
It might be easier if you namespace them though, just in case you have conflicting file names as your path is appended to the array so it will only cascade so far until it finds an appropriate match. Namespaced views are loaded with the double colon syntax.
View::addNamespace('theme', '/path/to/themes/views');
return View::make('theme::view.name');
You can also give addNamespace an array of view paths instead of a single path.
Here I am not accessing my project from public folder. Instead of this I am accessing from project root itself.
I have seen a forum discussion about Using alternative path for views here. But I am little confused about this.The discussed solution was,
You'd add a location like,
View::addLocation('/path/to/your/views');
Then add namespace for theme,
View::addNamespace('theme', '/path/to/themes/views');
Then render it,
return View::make('theme::view.name');
What will be the value for /path/to/ ?
Can I use the same project in different operating system without changing the path?
Yes, we can do this using the following,
Put the following in app/start/global.php
View::addLocation(app('path').'/themes/default');
View::addNamespace('theme', app('path').'/themes/default');
Then call view like the default way,
return View::make('page');
This will render page.php or page.blade.php file from project_directory/app/themes/defualt folder.
I've developed a theme package for laravel 5 with features like:
Views & Asset seperation in theme folders
Theme inheritence: Extend any theme and create Theme hierarcies
Try it here: igaster/laravel-theme
\View::addLocation($directory); works fine but the new right way to do it is using loadViewsFrom($path, $namespace) (available on any service provider).

Where do I save Zend_Form files?

I'm trying to learn Zend Framework! I'm quite interested in it but I can't find a tutorial which says where it's suppoused to be a Zend_Form class stored! Maybe it's something quite straightforward but I can't get it yet...
I've seen tutorials about this:
<?php
class Form_Example extends Zend_Form
{
public function init()
{
// Great code here
}
}
But none of them said where this code goes????? In a file in which folder in the directory tree?? I've read and I understand and I've done a little example with modules, controllers, actions, layouts and I know the importance about name conventions and the folder structure. So where does this form class must go and how can I call it from a view??
Thanks a lot, I know this must be easy for someone who already knows how to work well with Zend Framework =)
The best way to do this is to let ZF do it for you. ZF ships with a command line interface for both windows and *nix.
At the command line you can type zf create form Example, ZF will then create an empty form named Example.php at it's default application level location.
Typically this will be at application/forms/Example.php and the classname will be Application_Form_Example.
If you need to have a form constructed in a module the command would be similar:
zf create form Example -m admin where -m indicates you want the file created in a module and admin is name of the module.
Forms are one of the predefined resources in Zend Framework and as such have a default location. There are several other resources that are predefined and have defaults.
The Module Resource Autoloader
Zend Framework ships with a concrete implementation of
Zend_Loader_Autoloader_Resource that contains resource type mappings
that cover the default recommended directory structure for Zend
Framework MVC applications. This loader,
Zend_Application_Module_Autoloader, comes with the following mappings:
forms/ => Form
models/ => Model
models/DbTable/ => Model_DbTable
models/mappers/ => Model_Mapper
plugins/ => Plugin
services/ => Service views/
helpers => View_Helper
filters => View_Filter
As an example, if you have a module with the prefix of "Blog_", and attempted to instantiate the class
"Blog_Form_Entry", it would look in the resource directory's "forms/"
subdirectory for a file named "Entry.php". When using module
bootstraps with Zend_Application, an instance of
Zend_Application_Module_Autoloader will be created by default for each
discrete module, allowing you to autoload module resources.
I normally have all my forms in a forms folder, alongside the models, controllers, and views.
So, my file structure looks like:
application ->
configs
layouts
plugins
controllers
models
views
forms ->
form1.php
form2.php
Using them in your application isn't quite so simple. You must instantiate the form class in your controller, then pass the form to your view. So in your controller you want something like:
$form1 = new Application_Form_Form1($options);
$request = $this->getRequest();
if($request->isPost()) {
if($form1->isValid($post)) {
// form is valid, do form processing here
}
}
$this->view->form1 = $form1;
Then inside of your view file, you place the form:
<html>
<body>
<div id="body">
<?php echo $this->form1; ?>
</div>
</body>
</html>
At the heart of your question are the issues of:
autoloading
how the ZF autoloader works in general, and
how the ZF autoloader is configured by default in a standard ZF app
which are actually three distinct, though clearly-related, issues.
Assuming that you have the default ZF installation in which the appnamespace is set to "Application", then name your form class Application_Form_Example and store it in the file application/forms/Example.php.
Then you can instantiate (in a controller, for example) using:
$form = new Application_Form_Example().
Make sure that you have resources.modules[] = in application/configs/application.ini.
For additional discussion about autoloading, see https://stackoverflow.com/a/10933376/131824

Zend Framework Autoloader question

In zend framework I register my namespace like this (in application.php):
'autoloaderNamespaces' => array(
'Cms_'
)
And after this - I'd expect that Zend would always check that path in addition to Zend and ZendX paths if unknown class is called. But for some reason this doesn't work with for example view helpers.
I still have to register a separate path for my view helpers even though view helper scripts are named according to Zend coding standards and are located in:
Cms/View/Helper/
And this is how I register helper path in config file:
view' => array(
'charset' => 'UTF-8',
'doctype' => 'XHTML1_TRANSITIONAL',
'helperPath' => array(
'Cms_View_Helper_' => 'Cms/View/Helper'
)
),
So - I'm not sure why I have to register "Cms" namespace twice first through 'autoloaderNamespaces' and then through View "helperPath"? Shouldn't Cms namespace include Cms/View/Helper namespace?
can someone plz clarify this:)
View Helpers are considered application specific, so in the Recommended Project Directory Structure View Helpers are supposed to be placed in application/views/helpers. Which means, they usually wouldn't be found if ZF would just resolve the conventionalized class name.
When you call helpers with $this->helperName() or $this->getHelper('HelperName') from the View, the View will use the PluginLoader with the configured prefix and path to fetch that helper and inject the current View Instance. See sourcecode for all the details:
http://framework.zend.com/svn/framework/standard/trunk/library/Zend/View/Abstract.php
http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Loader/PluginLoader.php
So in other words, when loading a ViewHelper, you are not using the Autoloader. See:
Loading Plugins in the Zend Framework Reference Guide
This is taken directly from one of my application.ini files.
autoloaderNamespaces.Foo = "Foo"
includePaths.library = APPLICATION_PATH "/../library"
My "Foo" libraries are in the library directory - library/Foo. All I've done up until this point is make the "Foo" library available within the include paths.
I need to add a separate helper path to the default list for my view, otherwise the view won't look in that directory for matching view helpers. I think of loading view helpers as direct discovery. The view needs explicit instructions on where to look for helpers.
I believe it is exactly as you describe, the documentation on custom view helpers is pretty explicit about it:
You may, and should, give the class name a prefix, and it is recommended that you use 'View_Helper' as part of that prefix: "My_View_Helper_SpecialPurpose". (You will need to pass in the prefix, with or without the trailing underscore, to addHelperPath() or setHelperPath()).
This does make some sense to me though. In theory you could build a library of generic view helpers that could be re-used across multiple applications, so binding them to a specific application namespace would be inconvenient, i.e. if all my helpers were prefixed 'MyApp_' I would have to rename them to be able to use them in 'MyOtherApp'.

Categories