Using the Right Translation file in Symfony Manually - php

I do not have access do composer or any command line tool on the environment, so I'm doing it manually.
Problem is: I didn't find any route to the translation file I want do use (mynewBundle/resources/translation/messages.pt.xlf) and it's using the translation file of the original bundle I used as reference (oldBundle/resources/translation/messages.pt.xlf).
Where can I change this setting?
Thanks.

Symfony will collect translations from various sources by default:
app/Resources/translations/
app/Resources/<BundleName>/translations
inside the bundle: Resources/translations
Symfony will load translations in priority from these folders, so you can overwrite the translations inside a bundle by putting the new ones into app/Resources/<BundleName>/translations. If you have both, the old and new bundle, still inside your project than it could very well be that the old translations have a higher priority by means how the bundles are loaded.
If you can't remove the old bundle then you best chance is, to place the new translations into app/Resources/MyNewBundle/translations/messages.pt.xlf, instead of leaving them inside the bundle.

Related

How to refactor path names like `AppBundle:SomeFolder:someFile` in Symfony 2/3 based project to be used in Symfony 4+?

I am working on a Symfony project which was started based on Symfony 2 and currently uses Symfony 3.4. A lot of code in the project specifies paths using the : notation, for example:
$this->render('AppBundle:Products:products_list.html.twig');
// ==> /path/to/project/src/AppBundle/Resources/views/Products/products_list.html.twig
$this->getRepository('AppBundle:Products\Product');
// ==> /path/to/project/src/AppBundle/Entiy/Products/Product.php
The Project should now be updated to Symfony 5. Since Bundles are not (should be) used any more, a lot of these paths will change and need to be updated.
Doing this for entity classes is not a big deal, the ::class notation can be used without specifying any path:
$this->getRepository(Product::class);
But how to update all the other paths which do not represent a path but some asset or config file, etc.? Is there any rule of thumb on how 'AppBundle:Products:products_list.html.twig' should be updated when it is moved to /path/to/project/templates/app/products/products_list.html.twig?
EDIT:
Of course I can manually update all paths, e.g. from AppBundle:Products:products_list.html.twig to app/products/products_list.html.twig. Although some search/replace in all sources files might help, this is a cumbersome process.
Updating the class paths, e.g. from 'AppBundle:Products\Product' to Product::class also need manually work, but this approach has two major advantages:
The search/replace could be done in while still working with Symfony 3.4. The changes could be tested before performing the update. When updated to Symfony 5 no further changes would necessary, the MyEntity::class notation would simply deliver the new paths.
The path is not hard coded. If the file location changes again, no further updated is required.
Of course I am aware that these advantages are a benifit of using the ::class method. The question is, if there is any way in Symfony to achieve (nearly) the same for non-class files, like assets, config files, etc. Or is hard cording the paths the way to go?

Override easyadmin config file in Symfony project

I'm working on a core project A that use EasyAdminBundle. This project is use as a dependency in another project B. I'm looking for a solution to override a part of project A EasyAdmin configuration in project B.
I tried to create same files with same path in project B:
Project B: /config/packages/admin/my-file.yaml
Project A: /vendor/path/project-A/config/packages/admin/my-file.yaml
but it still use project A configuration file.
The goal is to override configuration of an entity edit form.
look at the overriding doc, the path renaming is only possible for files that are in the resource folder.
For overriding a service (or in your case a configuration) you should use service decoration and you won't need to rewrite the whole configuration but just the parameter that you need to change.

Symfony 4 how to use my own Bundle

I have my own Admin Bundle which is being developed in time. I just put it into new project and use it. If I need a new functionality I add it and use it later in another project. I put it in src/ directory. I don't want to store in on public Git repository.
Now as Symfony 4 is bundle-less, how should I easly put it into src/ dir so that it is decoupled from other App code?
I would like to develop the App as I shoulg but I would like to have an opportunity to easly copy Admin code to use it for another project.
Bundles are still perfectly available and useful in Symfony 4. They aren't suggested to be used for the main program code, as it is a little easier to not need that structure.
3rd party bundles are still incredibly useful though, to easily connect functionality, templates and services to an application. Equally, if you have some code that can be used in multiple applications and most easily added as a bundle, you are perfectly able to write your code as its own bundle. You can initially write it within your src/ directory, and then migrate it out to be an external bundle/library that can be pulled in via Composer when it's useful to do so.

Moving default AppBundle under vendor

I have a new symfony project. By default it contains
-/AppBundle
-AppBundle.php
--/Controller
--/Default Controller
Since I am going to have more bundles I would like it to be under a VendorName called MyProject where I have my ApiBundle.
I have tried moving AppBundle manually, then changing namespaces in the files, yml files and AppKernel. But I still get an error
Expected to find class "AppBundle\AppBundle" in file "/Applications/MAMP/htdocs/healthy-living/src/HealthyLiving/AppBundle/AppBundle.php" while importing services from resource "../../src/HealthyLiving/AppBundle/*", but it was not found! Check the namespace prefix used with the resource.' in /Applications/MAMP/htdocs/healthy-living/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php:133
Is there any console commands for doing this, if not what should be the procedures of moving it.
Thanks in advance
There's no console command or procedure to do it because it's not what vendor folder was designed for. vendor folder is meant to store 3rd-party code so keeping your own bundles, which you are developing, in vendor is not a good idea.
Since I am going to have more bundles
There is no reason that you can't keep more than one bundle inside your src folder. In fact, when Symfony introduced Bundle system it was very common that src folder contained a lot of bundles.
(note that vendor folder is almost always added to .gitignore - that's because what I wrote before)
EDIT after clarifying what the question is about:
It looks like command to generate bundles has/had some issues with creating bundles without Vendor folder:
Issue
Pull request
I don't know which version of Symfony are you using but either way creating bundle manually is always a good idea and it solves your problem too. (you can create it without vendor name)
You can upload your API bundle to a repository (Github, Gitlab, Bitbucket, etc.) and then import it as an external dependency with Composer.
Since moving was too complicated because of the config files that has to be changed so I decided to do a workaround and remove it and then install via console under the same parent. Works like a charm. Althoug could be a method in cli for this.

Override parameters.yml in each bundle symfony2

I'm working on a complex Symfony project. In the project, we have a core bundle which uses the parameters.yml located in app/config.
Each other AppBundle will inherit this CoreBundle and will correspond to a website.
What I need is to have specific parameters in each bundle that will override the default parameters one: when I'll use a route that will bring me into a controller's bundle, the parameters of this specific bundle have to override all the other ones.
I've tried the preprend method but it doesn't fit to this need. It only allows me to create new parameters for this bundle, but not to override the other ones.
I think you misunderstand the idea of bundles in Symfony. Bundle by design should be a reusable module, therefore the configuration placed inside a bundle is the default one. Then it is possible to override parameters (and not only!) in configuration in app folder.
The basic idea is:
Bundles don't use application. Application uses bundles.
So it's completely the opposite to what you expect it to be. Acutally it makes no sense to condition whole application configuration depending on current route since bundles can use one another. What if your currenct action will internally (without redirect) call another bundle's service or even controller?
Also it's worth mentioning that the app folder is the final folder for your application, therefore you can override in it not only bundle's configuration but also other things like services, view templates and so on.
Edit: I forgot to suggest a solution for you :)
Since you want to use custom parameters inside bundle, why do you need the default value in first place? Just create separate parameter namespace for each bundle that won't be overridden by application config. Then use it only inside that bundle.
Solution found thanks to dragoste's asking about separated kernels.
To solve my problem, I had to split the kernels : one for each website.
Documentation can be found here :
http://jolicode.com/blog/multiple-applications-with-symfony2

Categories