Default locale doesn't change in symfony 4 - symfony

I'm trying to change locale from 'en' to 'lt' as a default, but not changing at all it only adds as a "Fallback locale", but not as a main. So following that problem I'm getting another one, that routes by language is incorrect. So the first problem should solve all others.
services.yaml
parameters:
locale: 'lt'
framework.yaml
framework:
default_locale: '%locale%'
and results:
So by that my Routes using EN language routes instead LT
Any solutions? tried without %locale% variables, clearing cache etc..

If you are using translations, have a look in translations.yaml :
framework:
default_locale: '%locale%'
translator:
default_path: '%kernel.project_dir%/translations'
fallbacks:
- '%locale%'
The %locale% variable is defined in services.yaml.
If you're using an event subscriber to track a user defined locale you might want to check that event subscriber's config in services.yaml :
App\EventSubscriber\LocaleSubscriber:
arguments: ['%kernel.default_locale%']

Related

Translatation failing for CSRF token

I have a problem. In my form the text The CSRF token is invalid. Please try to resubmit the form. is not translating to French. I have the following in my translations/validator.fr.yml file:
The CSRF token is invalid. Please try to resubmit the form.: 'Actualisez le formulaire svp'
I am using Symfony 2.7.
After reading your comments, if you want your translations to be in French you can set a default locale for your application, and you must also enable the translator in your configuration. In your app/config/config.yml you would put:
parameters:
locale: fr
framework:
translator: { fallbacks: ["%locale%"] }
default_locale: "%locale%"
The translator line is commented out when you first install Symfony (see the master app/config/config.yml file).

Symfony2 translations loaded but not translated

Im using FOS RestBundle and JMSSerializer (maybe its doing something i dont know) and want to returns a translated message in json. I wanted to use the translator service however the trans() method not working.
$this->get('translator')->trans('translator.key.here')
The translations seem to be loaded correctly as i dumped translator and found there the translated message under the current locale and 'translator.key.here' key. However the trans function simply gives back the key string.
Any idea?
Edit: In twig its working perfectly.
In my messages.hu yml file:
translator:
key:
here: some message
In the config also set fallbacks:
translator: { fallbacks: [hu] }
default_locale: "%locale%"
When i dumped translator in the object i found under en locale the right message key pair:
"translator.key.here" => "some message"
Try this in parameters.yml:
locale: hu
and in config.yml:
framework:
translator: { fallbacks: ["%locale%"] }
default_locale: "%locale%"
and in translation folder create file: messages.hu.yml
Well i found a solution.. still dont know why not working simply but it works if i set directly the translation domain.
translator->trans('key.goes.here', array(), 'messages); if somebody has the some issue in the future.

symfony2 locale and validation messages

locale: pl in my parameters.yml but my validation messages are in english not polish,
I have also try locale: pl_PL but this also didn't work
Each time I cleared cache
You have to uncommented the translator service of the framework component in config.yml to activate translations, also for other symfony components. Changing the paramater in parameters.yml isn't enough.
framework:
translator: { fallback: %locale% }
Take a look at the official documentation.

How to define/pass the entity manager for sonata-admin

I followed this tutorial to install SonataAdmin with FOSUserBundle.
Now i keep getting this Error Message:
No entity manager defined for class Application\Sonata\UserBundle\Entity\User
But how do i set/pass the EntityManager?
I haven't found anything about configuring it or any hint on what this error means.
Any help anyone?
Edit #1:
As asked for, here is what i have in my config.yml for sonata so far:
sonata_block:
default_contexts: [cms]
blocks:
sonata.admin.block.admin_list:
contexts: [admin]
sonata.block.service.text:
sonata.block.service.rss:
Edit #2:
I added the entity manager configuration part for Doctrine2 ORM Admin thou it is mentioned in the Documentation that if left null, it should just use the default. Still, it doesn't solve my problem.
sonata_doctrine_orm_admin:
# default value is null, so doctrine uses the value defined in the configuration
entity_manager: '#doctrine.orm.entity_manager'
Edit #3:
I did set auto_mapping to true as well, even thou that also is true by default.
Still no Solution to this Problem.
When you use
php app/console sonata:admin:generate
it requests for fully qualified model class, so you'd write something like:
\Acme\DemoBundle\Entity\DemoEntity
the problem is, that when generator creates service for you it adds line:
arguments: [~, \Acme\DemoBundle\Entity\DemoEntity, AcmeDemoBundle:DemoEntityAdmin]
That breaks it.
It works with:
arguments: [~, Acme\DemoBundle\Entity\DemoEntity, AcmeDemoBundle:DemoEntityAdmin]
Notice missing "\" before Acme\Demo....
What was missing in my config.yml was:
doctrine:
[...]
orm:
[...]
entity_managers:
default:
mappings:
ApplicationSonataUserBundle: ~
SonataUserBundle: ~
Just had this problem.
Make sure you easy-extended the SonataUserBundle:
php app/console sonata:easy-extends:generate SonataUserBundle
and are loading it in AppKernel.php
new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),
See documentation here:
https://github.com/sonata-project/SonataUserBundle/blob/master/Resources/doc/reference/installation.rst
Then the automapping is enough and you don't need to define manual mapping.

Symfony2 translation: locale is ignored

Some time ago, I started translating the Symfony 2.1 website I'm working on, using the "correct way" through /{ _locale } in the URL. Now the translation worked fine for most routes, but some of them (/login, /register, ...) kept coming in the default language (fr in this instance), because they didn't have the { _locale } part in their url.
Through the debug panel I realised that the locale was not actually changed through url navigation (unlike this had me believe).
So I then went there, where I found information and link to set up a listener in order to change the session locale in function of the path that is called.
All defaults locale declarations (both in config.yml and the listener itself) are "fr".
When I switch languages, I can actually see on the debug panel that the session locale changes. However, it would appear that the translation has somehow been broken : every translation bit is made in english, even though the default language is french, and despite the session locale being "fr".
Where could that come from? How can I fix it?
Thanks in advance.
More technical information:
routing.yml:
AuraeLCUserBundle:
resource: "#AuraeLCUserBundle/Resources/config/routing.yml"
prefix: /{_locale}/
defaults: { _locale: fr }
requirements:
_locale: en|fr
a twig example:
<p>{{ 'layout.greeting.welcome'|trans({}, 'AuraeLCUserBundle') }}</p>
3 things here are critical.
default lang (setted in config) and if your using translatable doctrine extension:
# app/config/config.yml
framework:
session: { default_locale: 'fr_FR' }
translator: { fallback: 'en' }
.....
stof_doctrine_extensions:
default_locale: 'fr_FR'
They must be different because of circular reference exception appears when the default locale won't go.
Translator is looking for files yaml|xml|gettext with sufix fr_FR ex:
messages.fr_FR.yml
messages.en.yml
yourcatalogue.fr_FR.yml
yourcatalogue.en.yml
Read this for more details:
Change default locale in Symfony2
http://symfony.com/doc/2.0/book/translation.html
http://0hlsson.se/2011/07/27/symfony2-and-translatable-example/

Resources