From 234a3c9a5fb32967733e4d518a1c0acf8686dc32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 17 Nov 2014 00:32:52 +0100 Subject: [PATCH 1/2] translation of countries in form refs #317 --- Form/Type/Select2CountryType.php | 19 ++++++++++++++++++- Resources/config/services.yml | 6 ++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Form/Type/Select2CountryType.php b/Form/Type/Select2CountryType.php index 65daebdb7..a83a30e37 100644 --- a/Form/Type/Select2CountryType.php +++ b/Form/Type/Select2CountryType.php @@ -21,7 +21,10 @@ namespace Chill\MainBundle\Form\Type; use Symfony\Component\Form\AbstractType; +use Chill\MainBundle\Templating\TranslatableStringHelper; use Symfony\Component\OptionsResolver\OptionsResolverInterface; +use Chill\MainBundle\Entity\Country; +use Symfony\Component\HttpFoundation\RequestStack; /** * Extends choice to allow adding select2 library on widget @@ -30,6 +33,17 @@ use Symfony\Component\OptionsResolver\OptionsResolverInterface; */ class Select2CountryType extends AbstractType { + /** + * + * @var RequestStack + */ + private $requestStack; + + public function __construct(RequestStack $requestStack) + { + $this->requestStack = $requestStack; + } + public function getName() { return 'select2_chill_country'; @@ -42,8 +56,11 @@ class Select2CountryType extends AbstractType public function setDefaultOptions(OptionsResolverInterface $resolver) { + $locale = $this->requestStack->getCurrentRequest()->getLocale(); + $resolver->setDefaults(array( - 'class' => 'Chill\MainBundle\Entity\Country' + 'class' => 'Chill\MainBundle\Entity\Country', + 'property' => 'name['.$locale.']' )); } } diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 59e28f1e0..e0719bf15 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -25,6 +25,10 @@ services: - "%locale%" tags: - { name: form.type, alias: translatable_string } + chill.main.helper.translatable_string: + class: Chill\MainBundle\Templating\TranslatableStringHelper + arguments: + - "@request_stack" chill.main.form.type.select2choice: class: Chill\MainBundle\Form\Type\Select2ChoiceType @@ -38,5 +42,7 @@ services: chill.main.form.type.select2country: class: Chill\MainBundle\Form\Type\Select2CountryType + arguments: + - "@request_stack" tags: - { name: form.type, alias: select2_chill_country } From 57f2fa3178cc3663b852468328abab31dd7cc8b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 17 Nov 2014 00:58:13 +0100 Subject: [PATCH 2/2] create an helper + twig filter to show translatable string in current locale. The twig filter is localize_translatable_string Example : {{ person.nationality|localize_translatable_string }} The helper may be called with $container->get('chill.main.helper.translatable_string'). The main function is ->localize(array $strings) Example: $container->get('chill.main.helper.translatable_string')->localize($country->getName()); #return the name in current locale close #299 --- Resources/config/services.yml | 9 +++ Templating/TranslatableStringHelper.php | 80 +++++++++++++++++++++++++ Templating/TranslatableStringTwig.php | 32 ++++++++++ 3 files changed, 121 insertions(+) create mode 100644 Templating/TranslatableStringHelper.php create mode 100644 Templating/TranslatableStringTwig.php diff --git a/Resources/config/services.yml b/Resources/config/services.yml index e0719bf15..3a4766084 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -25,10 +25,19 @@ services: - "%locale%" tags: - { name: form.type, alias: translatable_string } + + chill.main.helper.translatable_string: class: Chill\MainBundle\Templating\TranslatableStringHelper arguments: - "@request_stack" + + chill.main.twig.translatable_string: + class: Chill\MainBundle\Templating\TranslatableStringTwig + calls: + - [ setContainer, ["@service_container"]] + tags: + - { name: twig.extension } chill.main.form.type.select2choice: class: Chill\MainBundle\Form\Type\Select2ChoiceType diff --git a/Templating/TranslatableStringHelper.php b/Templating/TranslatableStringHelper.php new file mode 100644 index 000000000..c94c295a0 --- /dev/null +++ b/Templating/TranslatableStringHelper.php @@ -0,0 +1,80 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\MainBundle\Templating; + +use Symfony\Component\HttpFoundation\RequestStack; + +/** + * + * This helper helps to find the string in current locale from translatable_strings + * + * @author Julien Fastré + * + */ +class TranslatableStringHelper +{ + /** + * + * @var RequestStack + */ + private $requestStack; + + public function __construct(RequestStack $requestStack) + { + $this->requestStack = $requestStack; + } + + /** + * return the string in current locale if it exists. + * + * If it does not exists; return the name in the first language available. + * + * Return a blank string if any strings are available. + * Return NULL if $translatableString is NULL + * + * @param array $translatableStrings + * @return string + */ + public function localize(array $translatableStrings) + { + if (NULL === $translatableStrings) { + return NULL; + } + + $language = $this->requestStack->getCurrentRequest()->getLocale(); + + + if (isset($translatableStrings[$language])) { + return $translatableStrings[$language]; + } else { + foreach ($translatableStrings as $string) { + if (!empty($string)) { + return $string; + } + } + } + + return ''; + + } + +} \ No newline at end of file diff --git a/Templating/TranslatableStringTwig.php b/Templating/TranslatableStringTwig.php new file mode 100644 index 000000000..ba58a412a --- /dev/null +++ b/Templating/TranslatableStringTwig.php @@ -0,0 +1,32 @@ +container->get('chill.main.helper.translatable_string') + ->localize($translatableStrings); + } + + public function getName() + { + return 'chill_main_localize'; + } + +} \ No newline at end of file