I have added The following annotation routing to my controller.
/**
* #Route("/{_locale}/over-ons", name="_about-us_nl", defaults={"_locale": "nl"}, requirements={"_locale": "nl|en|de"})
* #Route("/{_locale}/about-us", name="_about-us_en", defaults={"_locale": "en"}, requirements={"_locale": "nl|en|de"})
* #Route("/{_locale}/uber-uns", name="_about-us_de", defaults={"_locale": "de"}, requirements={"_locale": "nl|en|de"})
*/
Now I want use the correct router in twig. But therefore I need to combine a string with an app.request. What is the syntax for this?
Following is not working:
{{ path( '_about-us_' ~ app.request.get('_locate') ) }}
I suggest you to use the JMSI18nRoutingBundle (check here the doc).
This bundle able you t o define your route as example:
routing.yml
jms_i18n_routing:
default_locale: en
locales: [en, de]
strategy: prefix
Resulting URLs:
- /de/kontakt
- /en/contact
The bundle provide more knd of customization as described here
The Bundle provide the correct URL without specify the locale. You can speficy the locale for generating the url:
<!-- uses locale of the request context to generate the route -->
Contact
<!-- sometimes it's necessary to generate routes for a locale other than that
of the request context, then you can pass it explicitly -->
Deutsch
English
Hope this help
Use the following :
{{ path('_about-us_', { '_locale': app.request.locale }) }}
Related
In the Symfony2 project, I'm working on, the translations are files in multiple domain files like
foo.en_GB.xlf
bar.en_GB.xlf
buz.en_GB.xlf
...
foo.de_DE.xlf
bar.de_DE.xlf
buz.de_DE.xlf
...
foo.fr_FR.xlf
...
So in the Twig files I have to define the domain, e.g.:
{% trans from 'my_domain' %}my_key{% endtrans %}
Actually I don't need the domains in this project. All the translations are part of one big domain. So, I want (1) to use multiple translation files and (2) in the same time not to care about the domain, so that
{% trans %}my_key{% endtrans %}
should work for the my_key translated in any /.../translations/*.xlf file.
How to use multiple translation files without domains in a Twig template in Symfony2?
This can be achieved by creating a custom loader without break nothing, all you need is to use a different file extension:
namespace AppBundle\Translation\Loader;
use Symfony\Component\Translation\Loader\XliffFileLoader;
class FooFileLoader extends XliffFileLoader
{
/**
* {#inheritdoc}
*/
public function load($resource, $locale, $domain = 'messages')
{
// ignoring $domain var and pass 'messages' instead
return parent::load($resource, $locale, 'messages');
}
}
Register the translation loader:
services:
app.translation.loader.foo:
class: AppBundle\Translation\Loader\FooFileLoader
tags:
- { name: 'translation.loader', alias: 'foo' }
Later, you must name all your files to:
bar.en_GB.foo
baz.en_GB.foo
bar.de_DE.foo
baz.de_DE.foo
...
All your translation files with .foo extension will be merged and included into messages domain.
You can set the translation domain for an entire Twig template with a single tag:
{% trans_default_domain 'app' %}
I have a problem with generating relative URL in symfony 2.7 project
My project is hosted behind a proxy that redirect URL to the target VM, like this - example.com/oblounge/bo/ to vmhostname/
All my URL are generated in relative
Here is my problem
For example when I'm on this page
http://example.com/oblounge/bo/admin/articles/actualite?section=127
All generated likns are malformated, the "/admin/articles" is duplicated
http://example.com/oblounge/bo/admin/articles/admin/articles/actualite?section=127
Note: The problem is occurred only for rendered controllers
The render action
{{ render(controller('AppBundle:Back/Article/Article:sectionsArticle', { 'rubric': 'actualite' })) }}
The rendered twig
{% for section in sections %}
<li><i class="fa fa-circle-o"></i>{{ section.name }}</li>
{% endfor %}
My routing.yml
app:
resource: "#AppBundle/Controller/"
type: annotation
The prefix of my admin controller
/**
* #Route("/admin")
*/
class ArticleController extends BaseArticleController
{
I would suggest you give a look at your routing.yml file that is referenced within your bundle.
If you've already defined a prefix for the route in the routing.yml of you app/config there is no need to repeat it in the routing of your bundle , otherwise there will be a duplication of prefixes as in your case.
Example:
This is the routing.yml in the App\config folder
tutoLexikTestBundle_parents:
resource: "#tutoLexikTestBundle/Resources/config/routing/parents.yml"
prefix: /parents
And this is the one of the bundle
parents_show:
path: /{id}/show
defaults: { _controller: "tutoLexikTestBundle:Parents:show" }
I don't need to repeat the prefix 'Parents' in the path
I'm adding a language switcher on my website.
my parameters are like this :
avc_coming_soon:
resource: "#AVCComingSoonBundle/Controller/"
type: annotation
prefix: /{_locale}
defaults:
_locale: en
requirements:
_locale: |fr
en is set by default
In my twig, I did that:
<div class="languages">
<ul class="lang-menu">
<li class="en"><img src="{{ asset('images/flag_en.gif') }}" alt="EN"></li>
<li class="fr"><img src="{{ asset('images/flag_fr.gif') }}" alt="FR"></li>
</ul>
</div>
But when I click on 'English', my path become www.mysite.com/en or the good route is www.mysite.com/ (without the /en) because in parameters, I have this :
defaults:
_locale: en
How to get the default _locale in twig ?
{{ path(app.request.get('_route'), {'_locale': <<<default>>> }) }}
thank you :)
You need to use app.request.attributes:
{{ path(app.request.get('_route'), {'_locale': app.request.attributes.get('_locale') }) }}
If you have configuration in controller you need to set default value in function parameter:
/**
* #Route("/", name="coming_soon", options={"expose"=true}, requirements={"_locale" = "fr|en"})
*/
public function indexAction($_locale = 'en')
{
...
}
I use this bundle: https://github.com/schmittjoh/JMSI18nRoutingBundle
Thanks to the bundle I never have to mess with locales..
You can configure the bundle to prefix everything except the default locale (which is what you wan't, reading the comments to the other answer).
And for the language switcher you simply set the _locale on any given route.
when using '%locale%' parameter from service.yaml the value in twig file does not change using subscriber. Using this code works for me:
app.request.session.get('_locale')
I try to retrieve the global param that i define in parameters.yml and config.yml
in FOSUserBundle.en.yml under registration, in message i try to pass %myparam% and in my email.txt i try pass %myparam%:param like this
{{ 'registration.email.message'|trans({'%username%': user.username, '%confirmationUrl%': confirmationUrl,'%myparam%':myparam}, 'FOSUserBundle') }}
but it dosen't works.
and can i insert html inside yml and new line?
thanks
In your config.yml under fosuserbundle configuration.
confirmation:
enabled: true
template: MYMailBundle:Registration:email.html.twig
By doing this you are creating a twig template where you can render your variables and format your html how you want.
If you want to create a newline in your yml file I think you could do it by adding a \n.
How can I detect in which bundle am I?
for exemple, when I'm in web.com/participants/list, I want to read "participants".
In order to get the bundle name in the controller:
// Display "AcmeHelloBundle"
echo $this->getRequest()->attributes->get('_template')->get('bundle');
And inside a Twig template:
{{ app.request.get('_template').get('bundle') }}
In order to get the controller name in the controller:
// Display "Default"
echo $this->getRequest()->attributes->get('_template')->get('controller');
And inside a Twig template:
{{ app.request.get('_template').get('controller') }}
In order to get the action name in the controller:
// Displays "index"
echo $this->getRequest()->attributes->get('_template')->get('name');
And inside a Twig template:
{{ app.request.get('_template').get('name') }}
AFAIK it's not yet possible (at least in a easy way). You should use reflection. I wrote a quick and dirty service to do get bundle name ang guess entity/repository/form names based on my conventions. Can be buggy, take a look at: http://pastebin.com/BzeXAduH
It works only when you pass a class that inherits from Controller (Symfony2). Usage:
entity_management_guesser:
class: Acme\HelloBundle\Service\EntityManagementGuesser
In your controller:
$guesser = $this->get('entity_management_guesser')->inizialize($this);
$bundleName = $guesser->getBundleName(); // Acme/HelloBundle
$bundleShort = $guesser->getBundleShortName(); // AcmeHelloBundle
Another possibility would be using kernel to get all bundles: Get a bundle name from an entity
Well you can get the controller of the current route by,
$request->attributes->get('_controller');
You can parse the bundle name from it.
You can get the bundle name in the controller simply like that:
// Display "SybioCoreBundle"
echo $this->getRequest()->attributes->get('_template')->get('bundle');
And inside a Twig template:
{{ app.request.get('_template').get('bundle') }}