How to add a new domain for translations in Symfony - php

I'm using symfony and in my bundle I need to create some translations, but I rather do it in a different domain than "messages", like FOS User Bundle does (they use the FOSUserBundle domain).
So I created a MyBundle.en.yml file with my translations, but they are not loaded. I've read in the documentation I need to do this:
$translator->addLoader('xlf', new XliffFileLoader());
$translator->addResource('xlf', 'messages.fr.xlf', 'fr_FR');
$translator->addResource('xlf', 'admin.fr.xlf', 'fr_FR', 'admin');
$translator->addResource(
'xlf',
'navigation.fr.xlf',
'fr_FR',
'navigation'
);
http://symfony.com/doc/current/components/translation.html#using-message-domains
But where should I do that?
Update
After some investigation, if I run the debug for the translations it says that all the translation I'm using for my domain in the template are missing.
My translation file is located at
src/Acme/Bundle/MyBundle/resources/translations/MyDomain.yml
I tried to located in app/Resources/translation/MyDomain.yml but same result.
I also tried to delete the cache (rm -rf app/cache) but still not working

Symfony will automatically do this for you if you place your files in the correct location(s). You can find a full description of the conventions assumed by Symfony in the documentation: https://symfony.com/doc/current/translation.html#translation-resource-file-names-and-locations
The Best Practices-guide recommends storing them in app/Resources/translations/. Alternatively you can put them in your bundle's translations folder: src/MyBundle/Resources/translations.
Please be aware that you have to specify your domain when using the translator, e.g. in your twig templates. See:
https://symfony.com/doc/current/components/translation.html#using-message-domains
https://symfony.com/doc/current/translation.html#twig-templates

you have to specify the domain name. for example in twig:
{{ some_things | trans({}, 'my_domaine_name') }}

If you have something like x.en.yaml and you want to use it inside a twing template you can do something like this:
{% trans from 'x' %}word_to_translate{% endtrans %}

Related

symfony 3 url issue

Hi I'm coming for a very simple question (I think) but i didn't found the answer or a similar case.
I'm using symfony 3 and trying to build a second menu for my administration pannel.
However, I have a problem about how I have to declare the relative url in my "href", For my main menu i used to do like this
{{ url ( 'admin' ) }}
and it worked. The fact is that now I have subfolders and many levels in my url.
The url i try to reach is myapp/admin/gameadmin, this url work when I'm going on it but when i try to put it in 'href' I have an error message which says that the route is not working.
i declared it like that ->
{{ url(admin/gameadmin) }}
I tried with different character -> admin:gameadmin, admin\gameadmin ... etc and with path instead of url i don't know if it's not the good way to declare it or if I have a problem with my controllers.
In my bundle it's organised like that :
->Controllers(folder)
->admin(folder) (You can also find my main controllers on this level)
->admingamecontroller (Where the page I try to reach is routed)
I hope i gave you all the informations, thank you for your help and sorry for my english !
The url parameter is not the the url per se (ie: admin/gameadmin), this is the route name, defined in your routing.yaml file, or in your controller annotation.
If your action is something like this:
/**
* #Route("/admin/gameadmin", name="gameadmin")
*/
public function gameAdminAction()
{
...
}
Then, to generate the route, you have to do this:
{{ url('gameadmin') }}
By doing this, all the links on your website will be up to date if you change the gameadmin url, as long as you don't change the route name.
I suggest you to read this documentation on the Symfony website: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html
Edit: As pointed by user254319, if you're not using annotations, you'll have to edit your routing.yaml config file.
gameadmin:
path: /admin/gameadmin
controller: App\Controller\Admin\AdminGameController::gameadminAction
The route name is the yaml key: gameadmin.
Related Symfony documentation: https://symfony.com/doc/current/routing.html

Creating URLs containing the locale in Symfony 4

I am creating a Symfony 4 web app that supports multiple languages. The routing is done, so I can have /en/suchpage or /de/suchpage. I am now looking for a way to link to "suchpage" using the right locale.
Routing looks as follows:
suchpage:
path: /{_locale}/suchpage/
controller: App\Controller\SimplePageController::such
requirements:
maw: '%app.locales%'
defaults:
_locale: '%locale%'
I am using Twig, and it is from Twig templates that I need to be able to link to the right version of a page. This "works":
{{ path('suchpage', {'_locale': app.request.locale}) }}
It has two issues though:
I do not want to be repeating this all over the place. Something like {{ page('suchpage') }} would be much better. Does a function like this exist? Can path() be made to behave like this?
This fails if the locale was not specified in the request, as can happen, since it is not required on all pages (since some have a default).
There is a maintened bundle, JMSI18nRoutingBundle, to achieve what you need.
composer require jms/i18n-routing-bundle
Some usefull links about this bundle :
Packagist : https://packagist.org/packages/jms/i18n-routing-bundle
GitHub : https://github.com/schmittjoh/JMSI18nRoutingBundle

Perform merge between views of two or more modules

I have a Phalcon PHP modular application. I am making an administrative interface to control which modules should be used in the system.
One module controls the application's default interface, while the other modules add functionalities.
I have the problem: when another module to enabled, it can add the HTML content to the other interface control module. In this way I would like to merge two or more views. I am using Volt as template engine.
Is this possible in Phalcon?
Note: This was asked on the official Phalcon forums. I answered it over there and it got accepted. I am just mirroring my answer so future readers can get an answer here without being redirected from StackOverflow. Phalcon forum mirror: https://forum.phalconphp.com/discussion/15891/perform-merge-between-views-of-two-or-more-modules
config.php
You will need to define your modules in the app/config/config.php file like so;
return new \Phalcon\Config([
// ...
'modules' => [
'module01',
'module02',
...
'moduleN',
],
// ...
]);
*Controller.php
Then, in your controller, you'd set a view property to store the active modules like so;
$this->view->modules_enabled = $this->di->get("config")->modules;
*.volt
And finally in your volt file, just check the module is in the array holding active modules, and if so, display the view using partials.
{% if module01 in modules_enabled %}
<div id="module">{{ partial("partials/module01") }}</div>
{% endif %}

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

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..

trans_default_domain Symfony2

I change my translation file name (messages.en.yml => ProjectMainBundle.en.yml)
Is there a way to change the default translation domain for all the project and not add trans_default_domain in all my twig pages ?
Thanks for your help
I think you would need to override
vendor\symfony\symfony\src\Symfony\Component\Translation\MessageCatalogue
vendor\symfony\symfony\src\Symfony\Component\Translation\MessageCatalogueInterface
The default translation domain messages is hardcoded in there.

Categories