I was able to setup Sonata Admin with translated entities using Gedmo Doctrine Extensions:
# Doctrine Extensions Configuration
stof_doctrine_extensions:
default_locale: '%locale%'
orm:
default:
timestampable: true
blameable: true
translatable: true
# Sonata Translation Configuration
sonata_translation:
locales: [en, fr, it]
default_locale: '%locale%'
gedmo:
enabled: true
However every time I create a new entity, the translatable fields in the other languages starts empty.
English language selected:
Italian language selected:
It becomes very difficult to translate items if I don't know what they are in English.
Is there an option so that when I create an entity in English it populates also the entities in the other languages with the same content?
You should add fallback option to fields you need:
/**
* #var string
*
* #Assert\NotBlank()
*
* #Gedmo\Translatable(fallback=true)
*
* #ORM\Column(type="string", length=255)
*/
private $title;
I guess You forget to load the event listener service.
For each Gedmo extension (Timestampable, Blameable, Translatable etc. ) you have to register service in your service container.
For Translatable:
## app/config/services.yml
services:
## ...
gedmo.listener.translatable:
class: Gedmo\Translatable\TranslatableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ #annotation_reader ] ]
- [ setDefaultLocale, [ %locale% ] ]
- [ setTranslationFallback, [ false ] ]
Related
i have website in Symfony 4.2 with 3 prefixed locales:
annotations.yaml
controllers:
resource: ../../src/Controller/
type: annotation
prefix:
en: '/en'
de: '/de'
cz: '/cz'
and with route annotations in controllers:
/**
* #Route({"en": "references"}, name="reference")
* #Route({"cz": "reference"}, name="reference")
* #Route({"de": "referenzen"}, name="reference")
*/
Now i need cancel deutch locale and all /de/route adresses redirect to /en/route.
Is here any easy way to do it?
I want to use nelmio for symfony-project, but it doesn't work.
It always says: No operations defined in spec!
I also try the example on https://symfony.com/doc/current/bundles/NelmioApiDocBundle/index.html
Whats's wrong? Any ideas?
routing.yml
app.swagger_ui:
path: /api/doc
methods: GET
defaults: { _controller: nelmio_api_doc.controller.swagger_ui }
config.yml
nelmio_api_doc:
areas:
path_patterns: # an array of regexps
- ^/api(?!/doc$)
host_patterns:
- ^api\.
Controller
/**
* #Route("/api/test", methods={"GET"})
* #SWG\Response(
* response=200,
* description="Returns the rewards of an user"
* )
* #SWG\Parameter(
* name="order",
* in="query",
* type="string",
* description="The field used to order rewards"
* )
*/
public function testAction()
{
}
composer.json
"symfony/symfony": "3.4.*",
"nelmio/api-doc-bundle": "3.2.1",
Just remove
host_patterns:
- ^api\.
and set your virtual host in
documentation:
host: symfony.localhost
The assets normally are installed by composer if any command event (usually post-install-cmd or post-update-cmd) triggers the ScriptHandler::installAssets script. If you have not set up this script, you can manually execute this command:
php bin/console assets:install --symlink
The problem is in config.yml path patterns. If you remove the config(all nelmio_api_doc) or change the path patterns will work. Example:
nelmio_api_doc:
areas:
default:
path_patterns: [ /api/ ]
Hy,
For some time know I have an issue with my test scenario.
I use behat/mink ~2.0 and Nelmio/Alice ^2.x in my Symfony project.
On some of my project I use the doctrine extension translatable to manage i18n on my buisness entity.
When alice persist my fixture he seems to ignore my default_locale parameter and will always create them with en as locale value.
In order to test my locale switcher I need a way to choose my locale for the project in test environnement or/and persist fixture with different locale.
My fixture are wright as follow
AcmmeBundle\CoreBundle\Entity\Universe:
universe1:
title: <word()>
templates: [#template1]
collections: [#collection1, #collection2]
categories: [#category1, #category2]
Load in my test like this
#alice(User)
#alice(Category)
#alice(Collection)
#alice(Universe)
#alice(Page)
#alice(HomePage)
#reset-schema
#javascript
Feature: I test api GET /api/someEntity
And my behat.yml.dist
default:
autoload:
'': features/bootstrap
suites:
default:
contexts:
- Victoire\Tests\Features\Context\FeatureContext
- Victoire\Tests\Features\Context\JavascriptContext
- Victoire\Tests\Features\Context\VictoireContext
- Knp\FriendlyContexts\Context\MinkContext
- Knp\FriendlyContexts\Context\AliceContext
- Knp\FriendlyContexts\Context\EntityContext
- Knp\FriendlyContexts\Context\TableContext
- FeatureContext
- PageContext
- ApiContext
formatters:
html:
output_path: %paths.base%/web/build/html/behat
pretty:
output_path:
extensions:
emuse\BehatHTMLFormatter\BehatHTMLFormatterExtension:
name: html
renderer: Twig,Behat2
file_name: Index
print_args: true
print_outp: true
loop_break: true
jarnaiz\JUnitFormatter\JUnitFormatterExtension:
filename: report.xml
outputDir: %paths.base%/test-reports/
Behat\Symfony2Extension:
kernel:
path: app/AppKernel.php
debug: true
Behat\MinkExtension\ServiceContainer\MinkExtension:
base_url: 'http://127.0.0.1/app_test.php'
selenium2:
wd_host: 127.0.0.1:4444/wd/hub
capabilities: { "browser": "firefox"}
goutte: ~
symfony2: ~
default_session: symfony2
browser_name: firefox
Knp\FriendlyContexts\Extension:
entities:
namespaces:
- Acme
- Victoire
smartTag: smartStep
alice:
locale: fr_FR
fixtures:
Media: features/fixtures/media.yml
Template: features/fixtures/template.yml
User: features/fixtures/user.yml
Survey: features/fixtures/survey.yml
Tag: features/fixtures/tag.yml
Collection: features/fixtures/collection.yml
Category: features/fixtures/category.yml
Universe: features/fixtures/universe.yml
Page: features/fixtures/Victoire/page.yml
HomePage: features/fixtures/Victoire/Pages/home.yml
dependencies:
Template: [Media]
User: [Template]
Survey: [Tag]
HomePage: [Page]
According to http://docs.behat.org/en/v2.5/guides/7.config.html use
export BEHAT_PARAMS="formatter[name]=progress&context[parameters][base_url]=http://localhost"
and override "_locale" parameter in route
Well actually the answer was realy simple.
Just by using fixture for Translation class
universeTranslation1:
title: <word()>
locale: fr
translatable: #universe1
universeTranslation2:
title: <word()>
locale: en
translatable: #universe1
universeTranslation3:
title: <word()>
locale: fr
translatable: #universe2
universeTranslation4:
title: <word()>
locale: en
translatable: #universe2
I'm using Symfony 2.7. I followed documentation on Symfony2 about How to Work with multiple Entity Managers and Connections. I tried every solutions on this site but without success. I have always the same error :
[Doctrine\Common\Persistence\Mapping\MappingException] The class
'Spot\OfferBundle\Entity\Offer' was not found in the chain configured
namespaces Dashboard\ProjectBundle\Entity
In my project, I have 2 bundles. Every bundle works with a different entity manager. Here my config file :
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: %database2_driver%
host: %database2_host%
port: %database2_port%
dbname: %database2_name%
user: %database2_user%
password: %database2_password%
charset: UTF8
spot:
driver: %database3_driver%
host: %database3_host%
port: %database3_port%
dbname: %database3_name%
user: %database3_user%
password: %database3_password%
charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
DashboardProjectBundle: ~
spot:
connection: spot
mappings:
SpotOfferBundle: ~
I have two entities with relations across bundles
The first :
namespace Dashboard\ProjectBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Project
*
* #ORM\Table(name="project")
* #ORM\Entity
*/
class Project
{
/**
* #var Spot\OfferBundle\Entity\Offer
*
* #ORM\ManyToOne(targetEntity="Spot\OfferBundle\Entity\Offer", inversedBy="projects")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="offer_id", referencedColumnName="id")
* })
*/
private $offer;
And the second :
namespace Spot\OfferBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Offer
*
* #ORM\Table(name="offer")
* #ORM\Entity(repositoryClass="Spot\OfferBundle\Entity\OfferRepository")
*/
class Offer {
/**
*
* #ORM\OneToMany(targetEntity="Dashboard\ProjectBundle\Entity\Project", mappedBy="offer")
*/
private $projects;
I try with use statement, I check AppKernel and Bundles are defined. I try with leading backslashes. But nothig works.
Make sure that you have set this in the config file:
assetic:
...
bundles: [ DashboardProjectBundle, SportOfferBundle]
UPDATE:
Also, try to set orm config like this:
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
DashboardProjectBundle: ~
SpotOfferBundle: ~
spot:
connection: spot
mappings:
SpotOfferBundle: ~
I've extended SonataUserBundle and I'm trying to put french translations in it.
Here is my admin service definition:
sonata.admin.user:
class: Application\Sonata\UserBundle\Admin\Entity\UserAdmin
tags:
- { name: sonata.admin, manager_type: orm, group: user, label: users }
arguments:
- null
- Application\Sonata\UserBundle\Entity\User
- SonataAdminBundle:CRUD
calls:
- [setTranslationDomain, [SonataUserBundle]]
- [setUserManager, [#fos_user.user_manager]]
- [setSecurityContext, [#security.context]]
As you can see, the translation domain is set to SonataUserBundle.
I have set some labels in src/Application/Sonata/UserBundle/Resources/translations/SonataUserBundle.fr.yml
#...
list:
label_firstname: Prénom
label_username: Nom d'utilisateur
#...
But they are not taken into account (cache cleared)
However, if I remove this file, it insults me with
The file ".../src/Application/Sonata/UserBundle/Resources/translations/SonataUserBundle.fr.yml" must contain a YAML array.
Modifying the translation domain has no effect at all.
What am I doing wrong ?
You have add label_translator_strategy: sonata.admin.label.strategy.underscore in the service definition:
sonata.admin.user:
class: Application\Sonata\UserBundle\Admin\TestAdmin
tags:
- { name: sonata.admin, manager_type: orm, group: users, label: users, label_translator_strategy: sonata.admin.label.strategy.underscore }
arguments:
- null
- Application\Sonata\UserBundle\Entity\User
- SonataAdminBundle:CRUD
calls:
- [setTranslationDomain, [SonataUserBundle]]
- [setUserManager, [#fos_user.user_manager]]
- [setSecurityContext, [#security.context]]
I have tested in a new project, and it works fine.