Default Locale is not showing as href link in Symfony 3.4 - symfony

I run Symfony 3.4 in my local and added locale in default and routes. Given below:
in app/config.yml
framework:
default_locale: "en"
in Routes I have
homepage:
path: /{_locale}
defaults: { _controller: AcmeBundle:Home:index, _locale: en }
and in twig I have:
{% set en_params = app.request.attributes.get('_route_params') | default([]) | merge(app.request.query) %}
{% set en_params = en_params | merge({'_locale': 'en'}) %}
<span>English</span>
The expected output should be http://localhost/en when I will hover on anchor tag or click
but it is overriding with the default locale and showing http://localhost
Thanking you in advance to provide me the right way or correct me if I am wrong.

if an optional parameter (the {_locale} parameter) is at an optional position (i.e. the end) and is also the default, it can (and apparently will) be omitted. The same is true for {_format} and even for .{_format} (additional dot)
I think, as long as it works, why do you care?
You can maybe force the parameter by explicitly defining the _locale requirement to be [a-z]{2} (or even more explicitly de|en) but I'm not entirely certain.

Related

Symfony forcing translation into different language than the current one

I have the locale set to one language. Let's say german "de" and I want to have a part of the whole text translated into different languages (Ex. This happened 100 years ago -> This happened vor 100 Jahre). For this I am trying the following code:
//this returns 'site.dates.years'
{% set yearsText = yearsCount|displayTimeDivisions() %}
( {{ yearsCount }} {% trans with {'%content%': yearsText } from "messages" into "en" %}%content%{% endtrans %}
after using the trans method i get the string from yearsText ('site.dates.years') and not the translated content. Is this even possible to translate or I should drop it?
This works as expected, what you are trying to translate is %content%, not site.dates.years. Try this:
//this returns 'site.dates.years'
{% set yearsText = yearsCount|displayTimeDivisions() %}
( {{ yearsCount }} {% trans from "messages" into "en" %}{{ yearsText }}{% endtrans %}
Edit
The previous suggestion doesn't work as using trans in that way only works with simple text, not with variables.
This works for me:
{{ yearsText | trans({}, 'messages', 'en') }}
EDIT: clear cache
Sometimes Symfony has a too strong caching mechanism. If you work without locales first, and then modify controllers and twigs to be multi-locale and you also add a new messages file, Symfony loads all new controller/twigs but somehow misses that there are new message files. A simple cache clear, done ONLY ONCE (!) right after you add the message files, solves this problem. And thenceforth you don't need to clear the cache every time you change a translation.
Try this as a last resort. If this doesn't help I'd recommend to read carefully the documentation (link in the last line of this answer):
app/console cache:clear --env=dev
You may try one or all (!) of these, it's impossible to tell what is going wrong.
{{ yearsCount|trans }}
{{ 'site.dates.years'|trans }}
See if these translate. If the second does, you're setting the variable wrong, somehow.
If not, pass the _locale along in the route:
defaults: { _controller: Bundle:Controller:action, _locale: de }
Or have it as a route parameter:
path: /a/path/{_locale}/whatever
defaults: { _controller: Bundle:Controller:action }
requirements:
_locale: en|de
Print the locale to be sure it's properly set in the twig:
{{ app.request.locale }}
{{ app.request.getLocale() }}
If the locale prints out fine but the translation still does not work, it's because Symfony does not find the identifier. Go to the translations file and check that it's written correctly:
site:
dates:
years: Jahre
If you find it in messages.de.yml then maybe it's the wrong placement of the file. The loader searches for these files:
Symfony looks for message files (i.e. translations) in the following default locations:
the app/Resources/translations directory;
the app/Resources/MyAppBundle/translations directory;
the Resources/translations/ directory inside of any bundle.
If fiddling around with these files doesn't yield results, it's a configuration error in config.yml, or many overlapping errors (all of the above?) that work against you.
Here's the source of all pain/info about translations, but you've probably memorized every paragraph, by now: Symfony translations.

Symfony FOSRestBundle enableMaxDepthChecks default to true

Is it possible to set the default value for enableMaxDepthChecks to true?
Currently I have to add the following annotation to every route:
#View(serializerEnableMaxDepthChecks=true)
Is there a way to set this in the config or elsewhere to be the default?
Place this into your config.yml
jms_serializer:
default_context:
serialization:
enable_max_depth_checks: true
BUT! After making these changes, the parameter serializerEnableMaxDepthChecks becomes non-working, so you can't set it to false , and I don't know why...

Symfony2 - FOSUserBundle - Add _locale in url

I would like to overwrite default routing of FOSUserBundle for Symfony2 to get a result like this:
www.mysite.com/en/login
www.mysite.com/es/login
www.mysite.com/fr/login
www.mysite.com/en/profile
www.mysite.com/es/profile
www.mysite.com/fr/profile
Thanks for your help.
Look at this link. I think it is exactly what you need.
[...] to add a prefix before the _locale string (e.g. /admin/en/dashboard), you can add the “i18n_prefix” option.
# app/config/routing.yml
dashboard:
...
options: { i18n_prefix: admin }

Products sorting on indexByTaxon page

I'm trying to add ability to sort products by price and date. Are there any predefined methods to do that, or the only way is to implement them by hand? From sylius.yml we're getting such strange route:
%sylius.model.taxon.class%:
field: permalink
prefix: /t
defaults:
controller: sylius.controller.product:indexByTaxonAction
sylius:
template: SyliusWebBundle:Frontend/Product:indexByTaxon.html.twig
Which can be used like {{ path(taxon) }}. But just adding sorting parameter doesn't work for me. Any ideas?
You need a version, which incorporates the Pull Request #2122. Either the latest master, or you fork the 0.11 branch and cherry pick this fix.
Then you can simply define in your config.yml to only override the required defaults:
sylius_core:
routing:
%sylius.model.taxon.class%:
defaults:
sylius:
sorting:
order: desc

how to use properly locale for dates in symfony2 (2.4 and up)

How to use date locale properly in symfony2 Twig so that:
for GB browser I display format 27/10/2013
for US browser I display format 10/27/2013
First I had to enable Intl twig extension which is by default included in symfony2 framework (not enabled by default)
Enable it by adding this in your config.yml (example src/Project/TestBundle/Resources/config/):
services:
twig.extension.intl:
class: Twig_Extensions_Extension_Intl
tags:
- { name: twig.extension }
Then you use like this:
{{ 'now' | localizeddate('short', 'none') }}
// outputs dd/mm/yyyy or mm/dd/yyyy depending on where browser is coming from
If this does not work automaticaly then you need to force to use Prefered Language by the browser like this (in your controller)
$this->getRequest()->setLocale($this->getRequest()->getPreferredLanguage());
This will use valid date format depending on browser's visitor settings (like Accept-Language header with value en_GB or en_US)
You can force to use certain locale by adding third parameter in localizeddate (example "de-DE"). To get current locale use {{ app.request.locale }}
Make note that twig documentation is a little bit out of date so its better to use what I did above

Resources