From a2b3ff6d63062ed6ac93cacc900af12abae90f07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 5 Feb 2015 17:40:32 +0100 Subject: [PATCH] remove %locale% from cf choice refs #332 --- CustomFields/CustomFieldChoice.php | 17 +++++++++++------ Form/Type/ChoicesListType.php | 12 ++++++------ Resources/config/services.yml | 2 +- .../CustomFieldsRendering/choice.html.twig | 2 +- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/CustomFields/CustomFieldChoice.php b/CustomFields/CustomFieldChoice.php index 61508b36e..acb478978 100644 --- a/CustomFields/CustomFieldChoice.php +++ b/CustomFields/CustomFieldChoice.php @@ -30,6 +30,7 @@ use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer; use Chill\CustomFieldsBundle\Form\Type\ChoiceWithOtherType; use Symfony\Bridge\Twig\TwigEngine; use Chill\MainBundle\Templating\TranslatableStringHelper; +use Symfony\Component\Translation\Translator; /** * @@ -51,7 +52,7 @@ class CustomFieldChoice implements CustomFieldInterface */ private $requestStack; - private $defaultLocale; + private $defaultLocales; /** * @@ -64,11 +65,15 @@ class CustomFieldChoice implements CustomFieldInterface */ private $translatableStringHelper; - public function __construct(RequestStack $requestStack, $defaultLocale, TwigEngine $templating, - TranslatableStringHelper $translatableStringHelper) + public function __construct( + RequestStack $requestStack, + Translator $translator, + TwigEngine $templating, + TranslatableStringHelper $translatableStringHelper + ) { $this->requestStack = $requestStack; - $this->defaultLocale = $defaultLocale; + $this->defaultLocales = $translator->getDefaultLocales(); $this->templating = $templating; $this->translatableStringHelper = $translatableStringHelper; } @@ -157,7 +162,7 @@ class CustomFieldChoice implements CustomFieldInterface ->add(self::OTHER_VALUE_LABEL, 'translatable_string', array( 'label' => 'Other value label (empty if use by default)')) ->add(self::CHOICES, new ChoicesType(), array( - 'type' => new ChoicesListType($this->defaultLocale), + 'type' => new ChoicesListType($this->defaultLocales), 'allow_add' => true )) ; @@ -200,7 +205,7 @@ class CustomFieldChoice implements CustomFieldInterface 'selected' => $selected, 'multiple' => $customField->getOptions()[self::MULTIPLE], 'expanded' => $customField->getOptions()[self::EXPANDED], - 'locale' => $this->defaultLocale + 'locales' => $this->defaultLocales ) ); } diff --git a/Form/Type/ChoicesListType.php b/Form/Type/ChoicesListType.php index 246f14c45..e45e7942d 100644 --- a/Form/Type/ChoicesListType.php +++ b/Form/Type/ChoicesListType.php @@ -9,11 +9,11 @@ use Symfony\Component\Form\FormEvents; class ChoicesListType extends AbstractType { - private $defaultLocale; + private $defaultLocales; - public function __construct($defaultLocale) + public function __construct($defaultLocales) { - $this->defaultLocale = $defaultLocale; + $this->defaultLocales = $defaultLocales; } /* (non-PHPdoc) @@ -21,7 +21,7 @@ class ChoicesListType extends AbstractType */ public function buildForm(FormBuilderInterface $builder, array $options) { - $locale = $this->defaultLocale; + $locales = $this->defaultLocales; $builder->add('name', 'translatable_string') ->add('active', 'checkbox', array( @@ -31,14 +31,14 @@ class ChoicesListType extends AbstractType ->add('slug', 'hidden', array( )) - ->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) use ($locale){ + ->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) use ($locales){ $form = $event->getForm(); $data = $event->getData(); $formData = $form->getData(); if (NULL === $formData['slug']) { - $slug = $form['name'][$locale]->getData(); + $slug = $form['name'][$locales[0]]->getData(); $slug= strtolower($slug); $slug= preg_replace('/[^a-zA-Z0-9 -]/','', $slug); // only take alphanumerical characters, but keep the spaces and dashes too... $slug= str_replace(' ','-', $slug); // replace spaces by dashes diff --git a/Resources/config/services.yml b/Resources/config/services.yml index c483938fd..2b0dee975 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -43,7 +43,7 @@ services: class: Chill\CustomFieldsBundle\CustomFields\CustomFieldChoice arguments: - "@request_stack" - - %locale% + - "@translator.default" - "@templating" - "@chill.main.helper.translatable_string" tags: diff --git a/Resources/views/CustomFieldsRendering/choice.html.twig b/Resources/views/CustomFieldsRendering/choice.html.twig index f5bd582d2..c4dc4328f 100644 --- a/Resources/views/CustomFieldsRendering/choice.html.twig +++ b/Resources/views/CustomFieldsRendering/choice.html.twig @@ -10,7 +10,7 @@   {%- endif -%} {%- if choice['slug'] is not same as('_other') -%} - {{ choice['name'][app.request.locale]|default(choice['name'][locale]|default('Not available in your language'|trans)) }} + {{ choice['name']|localize_translatable_string }} {%- else -%} {{ choice['name'] }} {%- endif -%}